如何验证需要访问 xVal 中存储库的属性?
我正在尝试使用 xVal 来验证新用户的注册。当我尝试实现检查新用户尝试注册的用户名是否已被占用的逻辑时,我遇到了麻烦。如果我的用户实体不依赖于我的 UsersRepository,我似乎无法找到一种方法来完成此任务。这是我需要找到一种在我的用户实体中实现的方法:
public IEnumerable<ErrorInfo> ValidateUniqueUserName(string username)
{
if(usersRepository.Users.Exists(m => (m.UserName == username)))
yield return new ErrorInfo("UserName", "User name already exists");
}
关于如何在这种情况下继续使用 xVal 并使我的用户实体与我的 UsersRepository 解耦,有什么想法吗?
I'm trying to use xVal to validate the registration of a new user. I've ran into a buzz saw when trying to implement the logic that checks to see if the user name that the new user is trying to register with is already taken. I can't seem to find a way to accomplish this without having my User entity have a dependence on my UsersRepository. Here's the method I need to find a way to implement in the my User Entity:
public IEnumerable<ErrorInfo> ValidateUniqueUserName(string username)
{
if(usersRepository.Users.Exists(m => (m.UserName == username)))
yield return new ErrorInfo("UserName", "User name already exists");
}
Any ideas on how I can continue to use xVal for this scenario and keep my User entity decoupled from my UsersRepository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DDD 建议您可能有一个域服务来从域验证中抽象用户存储库(不变强制)。
我想知道上面示例中的代码驻留在哪里(验证服务)?但我建议你确保它在域中。 这是一个聪明的方法 将复杂的验证附加到仍然支持
IDataErrorInfo
接口的实体。我建议在您的验证方法中使用域服务,该服务返回您的
Users.Exists
查询。类似于:在上面的示例中,我将使用 DI 将适当的依赖项注入到 UserService 中以访问存储库/数据,但如果您愿意,可以使用工厂或手动 DI 对象创建方法:
注意:上述方法 要求将
validator
属性设置为false
以指示无效状态(以防不清楚)。DDD would suggest that you might have a Domain Service to abstract the Users repo from the domain validation (invariant enforcing).
I'd like to know where the code from your example above resides (validation service)? But i'd suggest you make sure it is in the domain. Here is a clever way to attach complex validation to entities that still support the
IDataErrorInfo
interface.What i would suggest is an Domain Service within your validation method that returns your
Users.Exists
query. Something like:In the example above i will be using DI to inject the appropriate dependencies into the
UserService
for accessing of the repo/data but you could use a factory or manual DI object creation method if you like:NOTE: The above method requires the
validator
property to be set tofalse
to indicate an invalid state (in case that wasn't clear).