Factories

Factories usages

  • Create entities
  • Create aggregates
  • Create value objects

Basically factories enable you to create Domain Objects

An example

Let’s say we want to open a bank account. We need lots of software objects associated with each other. A factory can handle the creation of all these objects associated with the bank account.

 

What the factory generates is important, but how it happens has no relevance to your domain. But the result of the factories does matter to the domain. 

Why use factories?

  • Factories provide encapsulation
  • Objects should not be responsible for their own creation (SRP, cohesion)
  • An important layer of abstraction

Domain Events

An event is something that has happened in the past.




A domain event is, something that happened in the domain that you want other parts of the same domain to be aware of.

An example

In the eShopOnContainers application, when an order is created, the user becomes a buyer, so an OrderStartedDomainEvent is raised and handled in the ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler

Event Notifications vs

Domain Events

With event notifications, a message is always sent asynchronously and communicated across processes and machines. 

However, with domain events, you want to raise an event from the domain operation you are currently running, but you want any side effects to occur within the same domain.

Made with Slides.com