'Does exposing some methods of a class through an Interface break any SOLID design principle? [closed]
I understand interface segregation and single responsibility principles guide against a class implementing methods or property it doesn't need. However, for the scenario described below where I have a User service with Login and Register methods, my Login Controller needs access to a single method (the login method). I have tried to hide the Register method by injecting a UserService object exposed through the ILogin interface.
Interfaces
public interface ILogin
{
Task Login();
}
public interface IRegister
{
Task Register();
}
and the UserService class;
public class UserService: IRegister, ILogin
{
public Task Login()
{
//implementation logic here
}
public Task Register()
{
//implementation logic here
}
}
the Login Controller client
public class LoginController : Controller
{
private ILogin login;
public Login Controller(ILogin login)
{
this.login = login;
}
[HttpPost]
public IActionResult Login()
{
//implementation logic here for login.post
}
}
I would like to know if this implementation breaks any design principle or if it is an anti-pattern.
Solution 1:[1]
If you want to use just one method from your service in Controller, then it is okay to use interface ILogin
.
ISP is how interface can be consumed. It means don’t depend on more than you need. That is there is no need LoginController
to consume the following methods like Register()
.
ISP's goal is to hide unnecessary information. Why? To minimize coupling and dependency.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |