简体   繁体   English

DDD:更新实体的多个属性的指南

[英]DDD: guidance on updating multiple properties of entities

So, i decided to learn DDD as it seems to solve some architectural problems i have been facing. 所以,我决定学习DDD,因为它似乎解决了我一直面临的一些架构问题。 While there are lots of videos and sample blogs, i have not encountered one that guides me to solve the following scenario: 虽然有很多视频和示例博客,但我还没有遇到一个指导我解决以下场景的方法:

Suppose i have the entity 假设我有实体

public class EventOrganizer : IEntity
{
    public Guid Id { get; }

    public string Name { get; }

    public PhoneNumber PrimaryPhone { get; }

    public PhoneNumber AlternatePhone { get; private set; }

    public Email Email { get; private set; }

    public EventOrganizer(string name, PhoneNumber primaryPhoneNr)
    {
        #region validations

        if (primaryPhoneNr == null) throw new ArgumentNullException(nameof(primaryPhoneNr));

        //validates minimum length, nullity and special characters
        Validator.AsPersonName(name);

        #endregion

        Id = new Guid();
        Name = name;
        PrimaryPhone = primaryPhoneNr;
    }
}    

My problem is: suppose this will be converted and fed to a MVC view and the user wants to update the AlternatePhone, the Email and a lot of other properties that make sense to exist within this entity for the given bounded context (not shown for brevity) 我的问题是:假设这将被转换并提供给MVC视图,并且用户想要更新AlternatePhone,电子邮件以及对于给定的有界上下文在该实体中存在有意义的许多其他属性(为简洁起见未示出) )

I understand that the correct guidance is to have a method for each operation, but (AND I KNOW ITS KINDA OF ANTI-PATTERN) i cant help but wonder if this wont end up triggering multiple update calls on the database. 我知道正确的指导是为每个操作设置一个方法,但是(我知道它的反模式的KINDA)我无法帮助但不知道这是否会最终触发数据库上的多个更新调用。

How is this handled ? 这是怎么处理的? somewhere down the line, will there be something that maps my EventOrganizer to something - say DbEventOrganizer and gathers all changes made to the domain entity and apply those in a single go? 在某个地方,是否会有一些东西可以将我的EventOrganizer映射到某些东西 - 例如DbEventOrganizer并收集对域实体所做的所有更改并一次性应用这些更改?

DDD is better suited for task-based UIs. DDD更适合基于任务的UI。 What you describe is very CRUD-oriented. 你所描述的是非常CRUD的。 In your case, individual properties are treated as independent data fields where one or many of these can be updated by a single generic business operation (update). 在您的情况下,单个属性被视为独立的数据字段,其中一个或多个可以通过单个通用业务操作(更新)进行更新。

You will have to perform a deeper analysis of your domain than this if you want to be successfull with DDD. 如果您想成功使用DDD,则必须对域进行更深入的分析。

Why would someone update all those fields together? 为什么有人会一起更新所有这些字段? What implicit business operation is the user trying to achieve by doing that? 用户试图实现的隐式业务操作是什么? Is there a more concrete business process that is expressed by changing PrimaryPhone , AlternatePhone and Email together? 是否有更具体的业务流程通过将PrimaryPhoneAlternatePhoneEmail一起更改来表达?

Perhaps that is changing the ContactInformation of an EventOrganizer ? 也许这会改变EventOrganizerContactInformation If that's the case then you could model a single ChangeContactInformation operation on EventOrganizer . 如果是这种情况,那么您可以在EventOrganizer上建模单个ChangeContactInformation操作。 Your UI would then send a ChangeContactInformation command rather than an update command. 然后,您的UI将发送ChangeContactInformation命令而不是update命令。

As for the persistence of your aggregate roots (AR), this is usually handled by an ORM like NHibernate if you are using a RDBMS. 至于聚合根(AR)的持久性,如果你使用的是RDBMS,这通常由像NHibernate这样的ORM来处理。 However, there are other ways to persist your ARs like Event Sourcing, NoSQL DBs and even storing JSON or any other data inter-change formats in a RDBMS. 但是,还有其他方法可以保留您的AR,如Event Sourcing,NoSQL DB,甚至在RDBMS中存储JSON或任何其他数据交互格式。

You question is quite broad! 你的问题很广泛!

EventOrganizer itself should not be updating anything. EventOrganizer本身不应该更新任何东西。 You should keep your update code quite separate from the entity. 您应该将更新代码与实体完全分开。 A different class would take an EventOrganizer object and update the DB. 另一个类将采用EventOrganizer对象并更新数据库。 This is called 'persistence ignorance' and makes the code a lot more modular and cohesive. 这称为“持久性无知”,使代码更具模块性和内聚性。

It would be common to create a View Model - a class whose purpose is to provide the View with the exact data it needs in the exact form it needs. 创建一个视图模型是很常见的 - 一个类的目的是以所需的确切形式为View提供所需的确切数据。 You would need to create the View Model from your EventOrganizer, after which the View can update it - programmatically or with binding. 您需要从EventOrganizer创建视图模型,之后View可以以编程方式或绑定方式更新它。 When you're ready to save the changes, you'll need to update your EventOrganizer from the View Model and pass it onto the updater. 当您准备保存更改时,您需要从View Model更新EventOrganizer并将其传递给更新程序。 This seems like a layer you don't need when the project is small and simple, but it is becomes invaluable as the complexity builds. 这似乎是项目小而简单时不需要的层,但随着复杂性的增加,它变得非常宝贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM