Dns 域事件如何返回数据?

Dns 域事件如何返回数据?,dns,domain-driven-design,Dns,Domain Driven Design,我正在制作一个web服务,并希望将域模式应用于它。我在使域实体具有更多行为方面遇到问题。我想做这样的事情 public void DoSomethingApi() { CustomerRepository customerRepository = new CustomerRepository(); Customer customer = customerRepository.GetCustomer("myId"); customer

我正在制作一个web服务,并希望将域模式应用于它。我在使域实体具有更多行为方面遇到问题。我想做这样的事情

    public void DoSomethingApi()
    {
        CustomerRepository customerRepository = new CustomerRepository();
        Customer customer = customerRepository.GetCustomer("myId");
        customer.DoSomething();
    }
为了使我的客户实体有更多的行为,我尝试了以下方法:

public class Customer
{
    public void DoSomething()
    {
        // how do I do this?
        // I need a repository and a bunch of services todo work here
    }   

    // using double dispatch
    public void DoSomething1(DoSomethingService service)
    {
        service.DoSomething();
    }

    // using domain services directly 
    public void DoSomething2()
    {
        new DoSomethingService().DoSomething();
    }

    // using event broker and domain events
    public void DoSomething3()
    {
        EventBroker.FireEvent<DoSomethingEvent>();
    }

    // using Actions
    public Action DoSomethingAction4 { get; set; }
}
公共类客户
{
公共无效剂量测定法()
{
//我该怎么做?
//我需要一个存储库和一堆服务来完成这项工作
}   
//使用双重分派
公共无效DoSomething1(DoSomethingService服务)
{
服务。DoSomething();
}
//直接使用域服务
公共无效DoSomething2()
{
新的DoSomethingService().DoSomething();
}
//使用事件代理和域事件
公共无效DoSomething3()
{
EventBroker.firevent();
}
//使用动作
公共操作DoSomethingAction4{get;set;}
}
所有方法都有优缺点,但我最喜欢使用域事件。但是,如何使用域事件返回值?如果方法在事务中,如何处理回滚


或者可能域事件实际上只是为了通知(触发并忘记)

从我的理解和实践来看,域事件是已经发生的事情。(在我们的惯例中,事件名称总是用过去时)。发布域事件只是“让世界知道”发生了什么。然后,任何事件侦听器都会相应地独立于事件的起因进行操作。事件包含所发生事件的信息,但不返回任何信息

因此,无法回滚域事件。监听器中被触发的事务可以回滚,但不能回滚事件本身


编辑:正如你所提到的-开火然后忘记

你能举一个更具体的例子吗?如果有一个设计良好的系统,你不需要这样做。一个工作单元只能影响一个聚合(理想情况下)。如果需要(回滚)事件,请尝试查看补偿事件。我建议两大最佳实践。不要通过web服务公开您的域对象,不要扁平化和简化您的消息对象(就像您使用精心制作的视图模型一样)。我正在考虑一种情况,即exmaple Customer有一个角色和一个操作。PlaceOrder(Order),这是我希望在customer类中使用的代码,而不是像OrderService这样的服务中使用的代码。此外,实体不用于通过线路发送-这是一个简单的示例,但在实际应用程序中,实体被转换为DTO