Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for setting default values for model properties in Domain Driven Design?

What's the best way to set default properties for new entities in DDD? Also, what's the best way to set default states for complex properties (eg. collections)?

My feeling is that default values should be in the models themselves as they are a form of business rule ("by default, we want X's to be Y & Z"), and the domain represents the business. With this approach, maybe a static "GetNew()" method on the model itself would work:

public class Person {
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
    public bool IsAlive { get; set; }
    public List Limbs { get; set; }

    public static Person GetNew() {
        return new Person() { 
            IsAlive = true,
            Limbs = new List() { RightArm, LeftArm, RightLeg, LeftLeg }
        }
    }
}

Unfortunately in our case, we need the collection property to be set to all members of another list, and as this model is decoupled from its Repository/DbContext it doesn't have any way of loading them all.

Crappy solution would be to pass as parameter :

public static Person GetNew(List<Limb> allLimbs) {
    return new Person() { 
        IsAlive = true,
        Limbs = allLimbs
    }
}

Alternatively is there some better way of setting default values for simple & complex model properties?

like image 630
Brendan Hill Avatar asked Jan 31 '13 00:01

Brendan Hill


People also ask

What are the strategies in extracting and formulating domain specific design principles?

Thus, we have developed three strategies for extracting and formulating domain-specific design principles: (1) analyze the best hand-designed visualizations in the domain, (2) examine prior research on the perception and cognition of visualizations, and, when necessary, (3) conduct new user studies that investigate how ...

Is domain driven design still relevant?

Domain Driven Design (DDD) has recently gained additional popularity, as evidenced by new books, conference talks, and even complete conferences dedicated to it), and lots of trainings – including some by our very own colleagues here at INNOQ.

What are domain driven design principles?

Domain-Driven Design(DDD) is a collection of principles and patterns that help developers craft elegant object systems. Properly applied it can lead to software abstractions called domain models. These models encapsulate complex business logic, closing the gap between business reality and code.


1 Answers

This is an instance of the factory pattern in DDD. It can either be a dedicated class, such as PersonFactory, or a static method, as in your example. I prefer the static method because I see no need to create a whole new class.

As far as initializing the collection, the GetNew method with the collection as a parameter is something I would go with. It states an important constraint - to create a new person entity you need that collection. The collection instance would be provided by an application service hosting the specific use case where it is needed. More generally, default values could be stored in the database, in which case the application service would call out to a repository to obtain the required values.

like image 125
eulerfx Avatar answered Oct 17 '22 00:10

eulerfx