George George - 3 months ago 20
C# Question

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?

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