DDD会导致很多添加/更改方法和构造函数重载吗? [英] Is lots of add/change methods and constructor overloads a consequence of DDD?

查看:109
本文介绍了DDD会导致很多添加/更改方法和构造函数重载吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个课:

    public class Person
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public string Email { get; private set; }
    public string Telephone { get; private set; }
    public Address Address { get; private set; }

    public Person(string firstName, string lastName)
    {
        //do null-checks
        FirstName = firstName;
        LastName = lastName;
        Address = new Address();
    }

    public void AddOrChangeEmail(string email)
    {
        //Check if e-mail is a valid e-mail here
        Email = email;
    }

    public void AddOrChangeTelephone(string telephone)
    {
        //Check if thelephone has correct format and valid symbols
        Telephone = telephone;
    }

    public void AddOrChangeAdress(Address address)
    {
        Address = address;
    }

构造函数中不存在的属性是可选的,即人没有需要电子邮件,地址或电话。但是,我想给班级用户一个创建对象的机会,而不必先提供所需的信息,然后再找出使用什么方法添加信息。

The properties that are not in the constructor are optional, i.e. the person does not need an e-mail, address or telephone. However, I want to give the user of the class an opportunity to create the object without having to first provide the required information and then afterwards have to find out what methods to use to add the information.

问题:


  1. 是否另外创建3个重载来为他们提供选择?

  2. 我应该允许可选属性上的公共设置器并在其中进行验证吗?

  3. 如果此人结婚并更改了姓氏,我是否需要更改姓氏的其他方法,还是我也应该公开此setter,并只在构造函数中要求它们?


推荐答案


  1. 否。


  2. 将其公开。

假设您将在 AddOrChange 方法中拥有更多代码,例如格式化逻辑或验证,那么我会请执行下列操作。否则,我将完全摆脱 AddOrChange 方法:

Assuming you are going to have more code in the AddOrChange methods such as formatting logic or validation then I'd do the following. Otherwise, I'd completely get rid of the AddOrChange methods:

public class Person
{
    private string _email = string.empty;
    private string _telephone = string.empty;
    private Address _address = new Address();

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { 
        get { return _email }
        set { AddOrChangeEmail(value); }
    }
    public string Telephone { 
        get { return _telephone;}
        set { AddOrChangeTelephone(value); }
     }
    public Address Address { 
        get { return _address;  }
        set { AddOrChangeAddress(value); }
    }

    public Person(string firstName, string lastName)
    {
        //do null-checks
        FirstName = firstName;
        LastName = lastName;
    }

    private void AddOrChangeEmail(string email)
    {
        //Check if e-mail is a valid e-mail here
        _email = email;
    }

    private void AddOrChangeTelephone(string telephone)
    {
        //Check if thelephone has correct format and valid symbols
        _telephone = telephone;
    }

    private void AddOrChangeAddress(Address address)
    {
        _address = address;
    }
}






要使用此类,您可以执行以下任一操作:


To work with this class you could do any of the following:

Person p = new Person("Tom", "Jones");
p.Telephone = "9995551111";

Person p = new Person("Tom", "Jones") { Telephone = "9995551111", Email="spamme@ms.com" }

这篇关于DDD会导致很多添加/更改方法和构造函数重载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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