DDD Implementing Domain Model

DDD Implementing Domain Model

Domain Model

“An object model of the domain that incorporates both behavior and data.”

models represent some artifact of the real world, but with a narrow purpose. It does not intend to replicate real-life but represents some particular aspects of real-life at a certain level of detail, depending on the purpose of the model.

There are two types of models: -

Anemic models:

The anemic domain model is nothing more than entities represented by classes containing only data and connections to other entities.

An example

???public class Product : Entity<Guid>

???{
???????public string Name { get; set; }
???????public decimal Price { get; set; }
???????public int Balance { get; set; }
???}        

Rich models:

The model itself contains behavior to ensure applying domain business logic on the domain itself. the services will execute the domain layer to apply action on the domain and then the domain model will manage.

Example

the product model can be converted to a rich model by adding the behavior as below

public class Product :Entity<Guid>

???{

???????public string Name { get;}
???????public decimal Price { get;}
???????public int Balance { get;}
???????public?Product(string _Name, decimal _Price, int _Balance)

???????{

???????????if(string.IsNullOrEmpty(_Name)) throw new ArgumentNullException("name","Product name should be has value");
???????????if (_Price <= 0) throw new ArgumentOutOfRangeException("Price", "Product price should e greater than zero");
???????????if (_Balance <= 0) throw new ArgumentOutOfRangeException("Price", "Product balance should e greater than zero");
???????????Name = _Name;
???????????Price = _Price; 
???????????Balance = _Balance; 

???????}

???????public void SetProductName(string _Name)

???????{

???????????if (string.IsNullOrEmpty(_Name)) throw new ArgumentNullException("name", "Product name should be has value");
???????????Name = _Name;

???????}
???????public void ChangeProductPrice(decimal _Price)

???????{

???????????if (_Price <= 0) throw new ArgumentOutOfRangeException("Price", "Product price should e greater than zero");
???????????Price = _Price;

???????}

???????public void IncreaseProductQauntity(int _Quantity)

???????{
???????????if (_Quantity <= 0) throw new ArgumentOutOfRangeException ("Quantity", "The Quantity should e greater than zero");
???????????Balance += _Quantity;
???????}

???????public void DecreaseProductQauntity(int _Quantity)

???????{
???????????if (_Quantity <= 0) throw new ArgumentOutOfRangeException ("Quantity ", "The Quantity should e greater than zero");
???????????Balance -= _Quantity;
???????}
???}         

Bounded Context

A bounded context is simply the boundary within a domain where a particular domain model applies. Different contexts may have completely different models of common concepts with mechanisms to map between them. It gives team members a clear and shared understanding of what has to be consistent and what can develop independently.

No alt text provided for this image

Entity and Value object

An entity object is fundamentally defined not by its attributes, but by a thread of continuity and identity. A unique thing that has a life cycle and can change state.

While value objects an immutable object that describes some characteristic or attribute but carries no concept of identity.

No alt text provided for this image

something is an entity while in another it is just a value object but in a different context.

public class ProductName : ValueObject<ProductName

??{
???????public string Name { get; }
???????public ProductName(string name)

???????{
???????????if(string.IsNullOrWhiteSpace(name))

???????????????throw new ArgumentNullException("name");
???????????Name = name;

???????}

???}        

Aggregate

A domain object that can be treated as a single unit to provide specific functionality and for data changes. An aggregate will have one of its component objects be the aggregate root.

Aggregate root

the domain’s only entry point for data access. A heart of your domain. The job of an Aggregate Root is to control and encapsulate access to its members in such a way as to protect its invariants. Any references from outside the aggregate should only go to the aggregate root. The root can thus ensure the integrity of the aggregate as a whole.

public class Product : AggregateRoot<Guid>

???{
???????public string Name { get;}
???????public decimal Price { get;}
???????public int Balance { get;}
???????public List<ProductImage> ProductImages { get;}

}         

In this example, we converted Product to an aggregate root that holds product attributes and a list of product images, and here product images consider as an entity.

Domain Service

Communicates to perform complex use cases, and cross aggregates transactions. An operation is offered as an interface that stands alone in the model, with no encapsulated state.

Event Sourcing

ensuring every change to the state of an application is captured in an event object, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application state itself.

?public void SetProductName(string _Name

???????{

???????????if (string.IsNullOrEmpty(_Name)) throw new ArgumentNullException("name", "Product name should be has value");
???????????Name = _Name;
???????????IsValidModel();
???????????RaiseEvent(new ProductNameChangedEvent

???????????{

???????????????Id = Id,
???????????????Data = _Name
???????????});
???????})        

References

https://blog.pragmatists.com/domain-driven-design-vs-anemic-model-how-do-they-differ-ffdee9371a86

https://blog.pragmatists.com/refactoring-from-anemic-model-to-ddd-880d3dd3d45f

https://docs.microsoft.com/en-us/azure/architecture/microservices/model/domain-analysis#:~:text=A%20bounded%20context%20is%20simply,necessarily%20isolated%20from%20one%20another .

S. Millett, N. Tune, and A. Denyer, “Patterns, principles, and practices of domain-driven design,” 2015.

A. Zimarev, “Hands-On Domain-Driven Design with .NET Core,” p. 446, 2019.

https://docs.microsoft.com/en-us/azure/architecture/microservices/model/domain-analysis#:~:text=A%20bounded%20context%20is%20simply,necessarily%20isolated%20from%20one%20another .

Mohamed gaber

Web Developer at TAFEEL IT Co.

2 年

???? ?????? ?? ????

Mohamed Fawzy Yahya

Technical Operations Leader | OutSystems platform administration and Support | Driving System Performance and Team Growth | Championing Operational Excellence and System Reliability

2 年

????? ???? ??? ???? ? ???? ???? ????? ??? ?????? ????? ??? ???? ???? ?? ???? ??

Mohamed Dabbour

Software Engineer at Abu Dhabi Department of Municipalities and Transport

2 年

????? ???????? ?? ????? ??? ??? ???? ????? ????? ???? ???? ?????? ??????? ? ???? ???? ??? ?????? ?????? ????? ??? ???? ???? ????? ?????? ?????? ??? ?? anemic , rich model ??? ?? ?????? ?? ?????

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了