I have an interface "IUser" and a class "User" implementing "IUser". Also I have an interface for the repository "IUserRepository".
I'm between these two options:
public interface IUserRepository
{
List<User> getAll();
void addUser(User user);
void alterUser(User user);
void deleteUser(string ID);
bool validateLogin(string session_user, string session_pass);
User getUserByID(int ID);
User getUserByNombre(string name);
User getUserBySessionUser(string session_user);
}
and this
public interface IUserRepository
{
List<IUser> getAll();
void addUser(IUser user);
void alterUser(IUser user);
void deleteUser(string ID);
bool validateLogin(string session_user, string session_pass);
IUser getUserByID(int ID);
IUser getUserByNombre(string name);
IUser getUserBySessionUser(string session_user);
}
Here is my confusion. Should my methods in the repository interface return real implementations or interfaces? as you can see in my first option I'm returning "User" instances. What I should return to loosely couple the code?
But if I choose the second one in my real implementation of the user repository I will only have access to the methods and properties that I had declared in my IUser interface.
for example if I call addUser method from a service passing it an instance of User class:
myRepositoryImplementation.addUser(UserInstance);
then my real repository is going to like this
private readonly IUser user;
public void addUser(IUser _user)
{
this.user=_user;
}
Nice valid catch! but then I will only have access to the method and properties declared initially in the IUser interface and not of any new method or property declared in my User instance.
also if I call getUserByID method I will receive an IUser instance.
So to resume this
1). Are these valid approaches?
2). If these are valid approaches which one should I use to preserve or keep the code decoupled?
3). If I choose the second one also if it is valid then should I declare in the interface all I'm going to use later? I mean properties and methods?
3). Have a better idea?