Search code examples
c#design-patternsrepository3-tier

Check user Login using Repository pattern c#


I'm new at design patterns and I'm trying to make some example using repository pattern (Maybe I'll ask some stupid question using stupid example and if it's so please tell me).

I have this repository in my BusinessLogicLayer:

public interface IUserRepository
{
    LogIn GetByUsernameAndPassword(LogIn user);
}

and in my data access layer

class UserRepository : IUserRepository
{
    ChatAppDBContext _db = new ChatAppDBContext();
    public LogIn GetByUsernameAndPassword(LogIn login)
    {
        return _db.Users.Where(u => u.Email == login.Email & u.Password == login.UserPassword).FirstOrDefault();
    }
}

but it throws an error that Error:

Cannot implicitly convert type 'DataAccessLayer.User' to 'BusinessLogicLayer.Model.LogIn'

How can I solve that?


Solution

  • class UserRepository : IUserRepository
    {
        ChatAppDBContext _db = new ChatAppDBContext();
        public Login GetByUsernameAndPassword(LogIn login)
        {
             var userResult = _db.Users.FirstOrDefault(u => u.Email == login.Email & u.Password == login.UserPassword);
    
             if (userResult == null)
                 // throw new Exception() or return new Login();
    
             Login loginResult = new Login();
             loginResult.Email = userResult.Email;
             return loginResult;                    
        }
    }