0

I have a Order entity.

public class Order{
    public string OrderNumber{get;set;}
    public string CreateDate{get;set;}
    ......
}

Now, I want to add a getxxx method to it.

public class Order{
    public string OrderNumber{get;set;}
    public string CreateDate{get;set;}
    ......
    public Order GetOrderDetail(string orderNumber){
        .....
    }
}

But I do not want to instantiate it every time. So, I want to add static to this method.

Whether this is in compliance with the DDD specification?

BD-Joy
  • 115
  • 8

3 Answers3

2

There's no reason to have static methods in domain objects except the occasional helper (construction method, property that returns a remarkable specific instance of a Value Object, etc).

If you feel you need it, it's probably a smell that the method is trying to reach for data that is not encapsulated in the entity. It will most likely try to get it from an external source outside the Domain layer and from a tightly coupled dependency, which is problematic in terms of design and testability.

guillaume31
  • 13,738
  • 1
  • 32
  • 51
  • If I don't use the `static` method, the call will be very cumbersome.For example: ```var o = new Order{OrderNumber = "123456"}; o.GetOrderDetail();``` – BD-Joy Mar 02 '17 at 09:23
  • Should I use dependency injection? – BD-Joy Mar 02 '17 at 09:26
  • `void` method names shouldn't begin with Get, since you expect from Get... methods to return something. Can you rephrase the method name to reflect exactly what it does? It's still unclear. – guillaume31 Mar 02 '17 at 09:28
  • I'm sorry, I have just written wrong. The method should be `public Order GetOrderDetail(string orderNumber)`. If `static` method is disabled, I will write it like that `public Order GetOrderDetail()` – BD-Joy Mar 02 '17 at 09:35
  • I just want to query the order detail information by order number. – BD-Joy Mar 02 '17 at 09:36
  • 1
    If it's the `Order` you need, that method should be in a Repository, not in the entity. – guillaume31 Mar 02 '17 at 10:03
  • If there is some sort of `OrderDetail` that is different from Order, either it is contained in the `Order` aggregate and you **cannot** get it via a static method (since it is encapsulated in an Order instance). Or, `OrderDetail` is a full-fledged aggregate and needs its own Repository with a `GetOrderDetail` method – guillaume31 Mar 02 '17 at 10:04
  • Yes, GetOrderDetail should be in a Repository. But how to expose it if the application layer will use it's returning value? So I call it in the domain entity. – BD-Joy Mar 02 '17 at 13:53
  • Not sure what you mean by that, but application layer services have access to repositories. – guillaume31 Mar 02 '17 at 14:22
1

In your case the answer is NO.

Reason:

You wont be able to access the non-static public properties of Order class in GetOrderDetail(string orderNumber) method. More likely you will use or prefered to use Dependency Injection

for your Data Access Layer and that DAL object will be a private instance field which wont be accecible in static method.

As the comment from Evk suggests that you wont be able to unit test it.

Here is more information on static methods When to use static classes in C#

Community
  • 1
  • 1
Yawar Murtaza
  • 3,655
  • 5
  • 34
  • 40
1

Whether this is in compliance with the DDD specification?

No, it is not.

In DDD we have Repositories that load Aggregate roots, so, considering that Order is an Aggregate root, then you will have to create another class, OrderRepository that has the responsability to load an Order from persistence:

public class OrderRepository{
//    ......
    public Order loadOrderById(string orderId){
//        .....
    }
}

P.S. I don't use C#, I hope that this is correct.

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54