如何中止对象初始化

本文关键字:初始化 对象 何中止 | 更新日期: 2023-09-27 18:33:27

我正在编写一个必须始终具有特定值的对象。最值得注意的是,它必须始终具有Name属性的值。

public class User
{
    public string Name { get; set; }
    public User(string name)
    {
        Name = name;
    }
}

现在,我需要在本课程中实现一些业务规则。其中之一是Name属性必须是唯一的名称。所以,我认为这个对象的初始值设定项看起来像这样:

    public User(string name, IQueryable<User> allUsers)
    {
        var matches = allUsers.Where(q => q.Name == name).ToList();
        if(matches.Any())
        {
            // abort object initialization
        }
        Name = name;
    }

但我不确定如何中止对象初始化。事实上,这可能吗?

有没有办法中止对象初始化(即:将对象设置为 null),或者有更好的方法来实现这一点?

如何中止对象初始化

好吧,你只会抛出一个异常。但我根本不喜欢这种处理这个问题的方式。相反,您应该通过服务创建用户,并让服务检查名称是否有效。

我想您可以在对象的构造函数或名称设置器中检查并抛出异常,但是 eeeehh 这可能会带来许多问题和混合问题。 我说通过执行此检查并返回 null(或名称良好的异常)的工厂创建对象。 或者创建 POCO 对象并通过单独的类/方法进行验证。

中止对象的初始化是通过在构造函数中引发异常来完成的,建议拒绝无效输入。

public class User
{
    public User(String name) {
        if (String.IsNullOrWhiteSpace(name)) {
            if (name == null) {
                throw new System.ArgumentNullException("Cannot be null.", "name");
            }
            else {
                throw new System.ArgumentException("Cannot be empty.", "name");
            }
        }
    }
}

您希望在构造函数中定义的业务逻辑不适合那里。构造函数应该是轻量级的,并且仅实例化。查询某些数据源对于构造函数来说成本太高。因此,应改用工厂模式。使用工厂模式时,调用方可能期望对象创建涉及一些繁重的工作。

public class User
{
    private User(String name) {
        if (String.IsNullOrWhiteSpace(name)) {
            if (name == null) {
                throw new System.ArgumentNullException("Cannot be null.", "name");
            }
            else {
                throw new System.ArgumentException("Cannot be empty.", "name");
            }
        }
    }
    public static User CreateUser(String name) {
        User user = new User(name); // Lightweight instantiation, basic validation
        var matches = allUsers.Where(q => q.Name == name).ToList();
        if(matches.Any())           
        {           
            throw new System.ArgumentException("User with the specified name already exists.", "name");         
        }     
        Name = name;
    }
    public String Name {
        get;
        private set; // Optionally public if needed
    }
}

您可以看到工厂模式更适合,并且由于它是一种方法,调用方可能希望通过调用它进行一些工作。而对于构造函数,人们会期望它是轻量级的。

如果要采用构造函数路由,则需要尝试其他一些强制实施业务规则的方法,例如尝试实际插入数据源时。

public class User
{
    public User(String name) {
        if (String.IsNullOrWhiteSpace(name)) {
            if (name == null) {
                throw new System.ArgumentNullException("Cannot be null.", "name");
            }
            else {
                throw new System.ArgumentException("Cannot be empty.", "name");
            }
        }
    }
}
public class SomeDataSource {
    public void AddUser(User user) {
        // Do your business validation here, and either throw or possibly return a value
        // If business rules pass, then add the user
        Users.Add(user);
    }
}

与其使用公共构造函数,不如使用这样的方法和私有构造函数:

public static User CreateUser(string name)
{
      // Check whether user already exists, if so, throw exception / return null
      // If name didn't exist, record that this user name is now taken.
      // Construct and return the user object
      return new User(name);
}
private User(string name)
{
       this.Name = name;
}

然后,您的调用代码可以使用User myUser = User.CreateUser("Steve");并相应地处理空返回/异常。

值得补充的是,无论您使用什么方法来存储所采用的用户名,都应该更新为在 CreateUser 方法中采用此名称。否则,如果您等待一段时间再将此对象存储在数据库中或其他东西中,您仍然会遇到问题。我已经更新了上面的代码以使其更清晰。

在创建用户之前,您可能应该检查重复的名称。

就个人而言,我在实例化之前运行逻辑检查。例如:

if(UserLogic.PreInsertValidation(string username)){
   User newUser = new User(username);
}
else{
  // Handling - maybe show message on client "The username is already in use."
}

PreInsertValidation将根据您的要求进行所有业务逻辑检查。

你要找的是身份映射模式,或者类似的东西。将此责任保留在对象本身可能是错误的,它应该在创建实体的组件中完成。当然,如果需要,地图应该是线程安全的,以避免竞争条件。

我会在将用户放入集合的提交中处理这个问题。 例如,如果您正在编辑用户集合,然后将其持久化到数据库,则持久性层将负责验证。 我一直认为让一个对象负责维护所有其他类似的对象是一种不好的做法。 当没有父子关系时,它引入了与对象本身的父子关系。 我建议实现某种验证引擎来处理这个问题。

不要在

对象本身中进行此验证,而是将此实体的创建、验证和保存放在服务中。当用户名不唯一时,此服务可以引发ValidationException,甚至可以开始事务以确保不会发生争用条件。我使用的一个很好的模型是命令/处理程序模式。下面是一个示例:

public class CreateNewUserCommand
{
    public string UserName { get; set; }
}
internal class CreateNewUserCommandHandler
    : ICommandHandler<CreateNewUserCommand>
{
    private readonly IUnitOfWork uow;
    public CreateNewUserCommandHandler(
        IUnitOfWork uow)
    {
        this.uow = uow;
    }
    public void Handle(CreateNewUserCommand command)
    {
        // TODO Validation
        var user = new User { Name = command.Name };
        this.uow.Users.InsertOnSubmit(user);
    }
}

您甚至可以将验证附加到其自己的类中。