洋葱架构中的登录过程
Login process within Onion Architecture
我想在洋葱架构中实现一个登录过程。我不知道如何正确地去做。下面是我的存储库 class,它将与数据库对话。我将如何检查电子邮件是否尚未输入 table。似乎我可以传入的唯一参数是我的 BaseClass 中的模型对象或实体。我的 Base class 只包含一个 Id 字符串。
public class RentalsRepository<T> : IRentalsRepository<T> where T : BaseClass
{
private readonly RentalsDBContext _Context;
private DbSet<T> entities;
string errorMessage = string.Empty;
public RentalsRepository(RentalsDBContext _Context)
{
this._Context = _Context;
entities = _Context.Set<T>();
}
public T Get(string Id)
{
return entities.SingleOrDefault(e => e.Id == Id);
}
目前我能想到的就是返回所有用户条目然后搜索列表,但我认为这不是很有效。谢谢!
基本上你首先会用提到的 GetByPredicate
方法扩展你的存储库,该方法基本上只是 SingleOrDefault
或 FirstOrDefault
的包装器(或其他采用 lambda 的 LINQ 方法)表达式/谓词)。您的回购协议将类似于此:
public class RentalsRepository<T> : IRentalsRepository<T> where T : BaseClass
{
private readonly RentalsDBContext _Context;
private DbSet<T> entities;
string errorMessage = string.Empty;
public RentalsRepository(RentalsDBContext _Context)
{
this._Context = _Context;
entities = _Context.Set<T>();
}
public T Get(string Id)
{
return entities.SingleOrDefault(e => e.Id == Id);
}
public T GetByPredicate(Func<T, bool> predicate)
{
return entities.FirstOrDefault(predicate);
}
}
在您的业务逻辑中,您将像这样调用此方法:
public void PerformLogin(string username, string hashedPassword)
{
var user = _repository.GetByPredicate(x => x.Username == username);
if(user != null)
{
if(user.HashedPassword == hashedPassword)
{
// Login succeeded do all you need to set usersession here
return;
}
}
// If we´ve come this far either there is no user or password was incorrect. We need to inform the user that login hasn´t succeeded
throw new LoginFailedException("Login failed. Username does not exist or password is incorrect.);
}
基本上,您可以随心所欲地称呼它 GetByPredicate
。请注意,每次调用 GetByPredicat
都会产生一个 SQL 表达式,因此不要使用复杂的条件。只使用像我上面展示的那样的简单条件。
我想在洋葱架构中实现一个登录过程。我不知道如何正确地去做。下面是我的存储库 class,它将与数据库对话。我将如何检查电子邮件是否尚未输入 table。似乎我可以传入的唯一参数是我的 BaseClass 中的模型对象或实体。我的 Base class 只包含一个 Id 字符串。
public class RentalsRepository<T> : IRentalsRepository<T> where T : BaseClass
{
private readonly RentalsDBContext _Context;
private DbSet<T> entities;
string errorMessage = string.Empty;
public RentalsRepository(RentalsDBContext _Context)
{
this._Context = _Context;
entities = _Context.Set<T>();
}
public T Get(string Id)
{
return entities.SingleOrDefault(e => e.Id == Id);
}
目前我能想到的就是返回所有用户条目然后搜索列表,但我认为这不是很有效。谢谢!
基本上你首先会用提到的 GetByPredicate
方法扩展你的存储库,该方法基本上只是 SingleOrDefault
或 FirstOrDefault
的包装器(或其他采用 lambda 的 LINQ 方法)表达式/谓词)。您的回购协议将类似于此:
public class RentalsRepository<T> : IRentalsRepository<T> where T : BaseClass
{
private readonly RentalsDBContext _Context;
private DbSet<T> entities;
string errorMessage = string.Empty;
public RentalsRepository(RentalsDBContext _Context)
{
this._Context = _Context;
entities = _Context.Set<T>();
}
public T Get(string Id)
{
return entities.SingleOrDefault(e => e.Id == Id);
}
public T GetByPredicate(Func<T, bool> predicate)
{
return entities.FirstOrDefault(predicate);
}
}
在您的业务逻辑中,您将像这样调用此方法:
public void PerformLogin(string username, string hashedPassword)
{
var user = _repository.GetByPredicate(x => x.Username == username);
if(user != null)
{
if(user.HashedPassword == hashedPassword)
{
// Login succeeded do all you need to set usersession here
return;
}
}
// If we´ve come this far either there is no user or password was incorrect. We need to inform the user that login hasn´t succeeded
throw new LoginFailedException("Login failed. Username does not exist or password is incorrect.);
}
基本上,您可以随心所欲地称呼它 GetByPredicate
。请注意,每次调用 GetByPredicat
都会产生一个 SQL 表达式,因此不要使用复杂的条件。只使用像我上面展示的那样的简单条件。