2014-10-19 58 views
0

我正在制作网络服务,并希望将域模式应用到它。我遇到了问题,使我的域实体有更多的行为。我愿做这样的事情域事件如何返回数据?

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; } 
} 

所有方法各有利弊,但我喜欢使用域名事件最多。但是,如何通过域事件返回值?如果该方法处于事务中,如何处理回滚?

或许域事件实际上只是通知(发射后不管)...

+0

能否给出一个更具体的例子,给定一个设计良好的系统,你不需要这样做。一个工作单位应该只影响一个总体(理想情况下)。如果事件需要(回滚),请尝试查看补偿事件。 – Sudarshan 2014-10-20 05:56:35

+1

我建议2大最佳做法。不要将域对象暴露在Web服务上,并将消息对象变平和简化(就像使用精心设计的视图模型一样)。 – 2014-10-20 08:22:19

+0

我想到的情况下,有一个角色和ACtion,例如Customer.PlaceOrder(订单),这是我想对客户类,而不是像OrderService服务的代码。 另外,这些实体不是通过线路发送的 - 这是一个简单的例子,但在实际应用中,实体被转换为DTO – user80855 2014-10-20 14:28:49

回答

2

从我如何理解和实践它,域事件是指已经发生了。 (在我们的惯例中,事件名称总是过去式)。发布域名事件只是“让世界知道”发生了什么事情。任何事件监听者都会根据事件的原因采取相应的行动。事件携带发生了什么的信息它不会返回任何东西。

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

编辑:正如你所说 - 火和忘记。