如何验证需要访问 xVal 中存储库的属性?

发布于 2024-08-15 08:57:05 字数 459 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2024-08-22 08:57:05

DDD 建议您可能有一个域服务来从域验证中抽象用户存储库(不变强制)。

我想知道上面示例中的代码驻留在哪里(验证服务)?但我建议你确保它在域中。 这是一个聪明的方法 将复杂的验证附加到仍然支持 IDataErrorInfo 接口的实体。

我建议在您的验证方法中使用域服务,该服务返回您的 Users.Exists 查询。类似于:

        base.AddRule(new ValidationRule()
        {
            Properties = "username",
            Description = "User name already exists",
            validator = () => !(new UserService()).Users.Exists(m => (m.UserName == username))
        });

在上面的示例中,我将使用 DI 将适当的依赖项注入到 UserService 中以访问存储库/数据,但如果您愿意,可以使用工厂或手动 DI 对象创建方法:

        base.AddRule(new ValidationRule()
        {
            Properties = "username",
            Description = "User name already exists",
            validator = () =>
                {
                    UserService us = ObjectFactory.GetInstance<UserService>();
                    return !us.Users.Exists(m => (m.UserName == username));
                }
        });

注意:上述方法 要求将 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:

        base.AddRule(new ValidationRule()
        {
            Properties = "username",
            Description = "User name already exists",
            validator = () => !(new UserService()).Users.Exists(m => (m.UserName == username))
        });

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:

        base.AddRule(new ValidationRule()
        {
            Properties = "username",
            Description = "User name already exists",
            validator = () =>
                {
                    UserService us = ObjectFactory.GetInstance<UserService>();
                    return !us.Users.Exists(m => (m.UserName == username));
                }
        });

NOTE: The above method requires the validator property to be set to false to indicate an invalid state (in case that wasn't clear).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文