在哪里查看用户电子邮件尚不存在? [英] Where to check user email does not already exist?

查看:195
本文介绍了在哪里查看用户电子邮件尚不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个帐户对象可以像这样创建用户;

I have an account object that creates a user like so;

public class Account
{
    public ICollection<User> Users { get; set; }

    public User CreateUser(string email)
    {
        User user = new User(email);
        user.Account = this;
        Users.Add(user);
    }
}

在我创建新用户时,在服务层中这种方法。但是,有一个规则,即用户电子邮件对于该帐户必须是唯一的,那么该去哪里呢?对我来说,它应该进入CreateUser方法,并带有额外的一行,该行仅检查该电子邮件对于该帐户而言是唯一的。

In my service layer when creating a new user I call this method. However there is a rule that the users email MUST be unique to the account, so where does this go? To me it should go in the CreateUser method with an extra line that just checks that the email is unique to the account.

但是,如果要执行此操作,则所有该帐户的用户需要加载,这对我来说似乎有些负担。最好在数据库中查询用户的电子邮件-但是用该方法进行操作将需要在account对象中建立一个存储库,不是吗?也许答案就是从存储库加载帐户而不是这样做;

However if it were to do this then ALL the users for the account would need to be loaded in and that seems like a bit of an overhead to me. It would be better to query the database for the users email - but doing that in the method would require a repository in the account object wouldn't it? Maybe the answer then is when loading the account from the repository instead of doing;

var accountRepository.Get(12);
//instead do
var accountRepository.GetWithUserLoadedOnEmail(12, "someone@example.com");

然后,帐户对象仍可以检查Users集合中的电子邮件,并且该邮件已被急切地装入如果找到。

Then the account object could still check the Users collection for the email and it would have been eagerly loaded in if found.

这有用吗?你会怎么做?

Does this work? What would you do?

我正在使用NHibernate作为ORM。

I'm using NHibernate as an ORM.

推荐答案

首先,我认为您不应该使用异常来处理正常业务逻辑,例如检查重复的电子邮件地址。这是一个很好的文档反模式,最好避免。保留对数据库的约束,并处理所有重复的异常,因为它们无法避免,但是请尝试通过检查将其降至最低。我不建议您锁定表格。

First off, I do not think you should use exceptions to handle "normal" business logic like checking for duplicate email addresses. This is a well document anti-pattern and is best avoided. Keep the constraint on the DB and handle any duplicate exceptions because they cannot be avoid, but try to keep them to a minimum by checking. I would not recommend locking the table.

其次,您已经在这个问题上加上了DDD标签,所以我将以DDD方式回答。在我看来,您需要域服务或工厂。在域服务或工厂中移动此代码后,可以将UserRepository注入其中并对其进行调用,以查看该电子邮件地址是否已存在用户。

Secondly, you've put the DDD tag on this questions, so I'll answer it in a DDD way. It looks to me like you need a domain service or factory. Once you have moved this code in a domain service or factory, you can then inject a UserRepository into it and make a call to it to see if a user already exists with that email address.

类似这样的东西:

public class CreateUserService
{
private readonly IUserRepository userRepository;

public CreateUserService(IUserRepository userRepository)
{
    this.userRepository = userRepository;
}

public bool CreateUser(Account account, string emailAddress)
{
    // Check if there is already a user with this email address
    User userWithSameEmailAddress = userRepository.GetUserByEmailAddress(emailAddress);
    if (userWithSameEmailAddress != null)
    {
        return false;
    }

    // Create the new user, depending on you aggregates this could be a factory method on Account
    User newUser = new User(emailAddress);
    account.AddUser(newUser);
    return true;
}
}

这可让您将职责略微分开并使用域服务来协调事物。希望有帮助!

This allows you to separate the responsiblities a little and use the domain service to coordinate things. Hope that helps!

这篇关于在哪里查看用户电子邮件尚不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆