Domain driven design CQR的部门域实体上有哪些属性

Domain driven design CQR的部门域实体上有哪些属性,domain-driven-design,cqrs,Domain Driven Design,Cqrs,我第一次在CQRS试用,需要一些建议 在我的业务中,可以重命名部门。为此,我调度一个重命名的partment命令,由命令处理程序捕获。然后,该处理程序将调用Department实体上的rename方法,如果这对实体有任何作用,即更新name属性??由于该名称仅用于查看目的,我想我应该做的就是发送一个DepartmentRename事件并更新相应的ViewModels,我的想法正确吗 提前感谢CQRS是将读取和写入的责任分为不同部分的地方: 写入存储、事件存储以持久化域中发生的所有事件 读取存储,

我第一次在CQRS试用,需要一些建议

在我的业务中,可以重命名部门。为此,我调度一个重命名的partment命令,由命令处理程序捕获。然后,该处理程序将调用Department实体上的rename方法,如果这对实体有任何作用,即更新name属性??由于该名称仅用于查看目的,我想我应该做的就是发送一个DepartmentRename事件并更新相应的ViewModels,我的想法正确吗


提前感谢

CQRS是将读取和写入的责任分为不同部分的地方:

  • 写入存储、事件存储以持久化域中发生的所有事件
  • 读取存储,以持久化从写入模型的事件存储中产生的事件流增量生成的投影
  • 您的操作是正确的,因为
    RenameDepartmentCommandHandler
    将处理命令
    RenameDepartmentCommand

    public void RenameDepartmentCommandHandler(RenameDepartmentCommand command)
    {
        // repository is injected in for us during command handler instantiation
        var dept = this.repository.GetByDeptName(command.DepartmentName);
        dept.RenameDepartment(command.NewDepartmentName);
        this.repository.Save(dept);
    
        // the repository will save all of the events, once committed
        // a separate piece of code running in the background will
        // listen for new events from the the event-store and dispatch
        // them to anyone who may be listening
    }
    
    实际的实体需要在写模型中通过持久化事件来更新,这是唯一的真相来源。您不仅要更新读取模型,还要始终更新写入模型

    在写模型持久化事件之后,它将被分派(在将来的某个时间点)到读模型

    读模型是写模型的从属模型。因为它是一个从机,所以它最终将是一致的(对于使用独立基础设施且不使用分布式事务的读写模型)。。。最终,一致性并不意味着有问题,通常大多数问题空间都可以接受延迟(10毫秒到秒甚至分钟)

    因此,您的实体将如下所示:

    public class Department : AggregateBase
    {
        // this class derives from jOliver's CommonDomain project
        // only useful if using C# - there will be similar
        // event sourcing infrastructure projects for different
        // languages
    
        public string Name { get; protected set; }
        // all other properties go here
    
        public void RenameDepartment(public newName)
        {
            // the DepartmentRenamed is an immutable class taking the
            // current name and new name as constructor parameters          
            RaiseEvent(new DepartmentRenamed(this.Name, newName));
        }
    
        protected void Apply(DepartmentRenamed evnt)
        {
            this.Name = event.NewName;
        }
    }
    
    public class DeptNameListProjection : IEventHandler<DepartmentRenamed>
    {
        public void Handle(DepartmentRenamed evntFromEventStore)
        {
            var department = this.readStore.Find(evntFromEventStore.OldName);
            // write it, depends on your infrastructure
            // normally a SqlCommand or similar is fine here, I try and avoid
            // using an ORM as imho flat tables make more sense for read models
        }
    }
    
    你的阅读模型投影是这样的:

    public class Department : AggregateBase
    {
        // this class derives from jOliver's CommonDomain project
        // only useful if using C# - there will be similar
        // event sourcing infrastructure projects for different
        // languages
    
        public string Name { get; protected set; }
        // all other properties go here
    
        public void RenameDepartment(public newName)
        {
            // the DepartmentRenamed is an immutable class taking the
            // current name and new name as constructor parameters          
            RaiseEvent(new DepartmentRenamed(this.Name, newName));
        }
    
        protected void Apply(DepartmentRenamed evnt)
        {
            this.Name = event.NewName;
        }
    }
    
    public class DeptNameListProjection : IEventHandler<DepartmentRenamed>
    {
        public void Handle(DepartmentRenamed evntFromEventStore)
        {
            var department = this.readStore.Find(evntFromEventStore.OldName);
            // write it, depends on your infrastructure
            // normally a SqlCommand or similar is fine here, I try and avoid
            // using an ORM as imho flat tables make more sense for read models
        }
    }
    
    公共类DeptNameListProjection:IEventHandler
    {
    公共无效句柄(部门重命名为evntFromEventStore)
    {
    var department=this.readStore.Find(evntFromEventStore.OldName);
    //写它,取决于你的基础设施
    //通常情况下,SqlCommand或类似命令在这里很好,我会尽量避免
    //使用ORM作为imho平面表对于读取模型更有意义
    }
    }
    
    谢谢这真的很有帮助:-)