I am having a Repository, Interface, Controller …. that looks something like this..
Interface – IUSer
public interface IUserRepositories
{
bool CreateUser(User newUser);
bool CheckUniqueEmail(string Email);
bool CheckUserLoginDetails(string email, string password);
}
User Repository
=============
which is implementing all the methods ..
1) CreateUSer
2) CheckLoginDetails
3) CheckUniqueEmail
Controller – Not sure what it does…
private IUserRepositories _userRepo;
public UserController() : this(new UserRepositories())
{
}
public UserController(IUserRepositories userRepo)
{
this._userRepo = userRepo;
}
Is this looks OK…if yes… what benefit i can get by implementing this way…
Please explain… confusion from last 3 hours….
Thanks…
Almost. What you have is often referred to as poor man’s DI. The correct way is the following (notice that the default constructor has been removed):
The benefit you have by using inversion of control that way is that your controller is weakly coupled to the repository making it easier to unit test in isolation and making different layers of your application more reusable and maintainable.
By removing the default constructor you are clearly indicating to the consumers of a given class that this class depends and requires a repository which must implement a given contract (interface). This makes your code clearly auto document itself to the consumer.