{{announcement.body}}
{{announcement.title}}

Building Microservices Through Event-Driven Architecture, Part 2: Domain Objects and Business Rules

DZone 's Guide to

Building Microservices Through Event-Driven Architecture, Part 2: Domain Objects and Business Rules

In this tutorial, an architect explores how microservices teams can work with domain objects while creating an event-driven architecture for their app.

· Microservices Zone ·
Free Resource

This tutorial is the second part of a series: Building microservices through Event Driven Architecture.

During this journey, I will implement the domain model: EduSync.Speech.Domain,

This is the innermost layer that holds the core domain. It contains our domain objects and business rules. and defines our External Interfaces.

Databases, network connections, filesystem, UI or special frameworks are not allowed.

The core domain has no knowledge of anything outside of itself.

Those dependencies and their implementations are injected into our core domain using interfaces.

At the end of the previous step, we ended up with an anemic domain model. So let's start by enriching it.

Rich Domain Model

An anemic domain model is an antipattern in the world of DDD, so, in this section, I will decouple the domain model from data contracts using Value Object.

An anemic domain model is a domain model where data and operations upon that data are separated from each other. In other words, a class with only properties and methods that treat these properties are located in other classes.

As a result, these other classes can read the data but also modify it. So the domain class must have public setters. This is a lack of encapsulation antipattern:

Let us begin by validation the title.

My first test will be: The title length must be greater than 10 characters and less than 60 characters

.

The test will fail, so let us implement title validation.

Title Value Object

The main difference between the entity and value object is how to identify them.

An Entity is identified by Reference Equality and Identifier Equality.

A Value Object is identified by Reference Equality and Structural Equality

  • Reference Equality: Two objects are equal if they reference the same object in memory.
  • Identifier Equality: Two objects are equal if they have the same identity.
  • Structural Equality: Two objects are equal if all their members are equals.

An Entity has an Id field and is mutable while a Value Object have no Id Field and is immutable.

A Value Object does not make sense without an Entity, as it must belong to an Entity.

Consider the following situation:

  • Two vehicles of the same model, that have the same colors, are the same age, etc. are always two different vehicles because each vehicle has its own identifier: 'Vehicle' is an Entity.
  • Two addresses that have all their fields equals (same street number, same city, same country, etc.) are exactly the same address: 'adress' is a Value Object.

My first implementation of Title looks like this:

Let us fix the test:

Remember that a Value Object is identified by Reference Equality and Structural Equality.

So right-click the Title class and select Generate Equals and GetHashCode.

Title has only one value, so select it and click OK

The Title is now a value object and it's final implementation will look like this

Here is the unit test of the Title Value Object. I should verify that two titles are equal if they have the same values, and different if not.

URL Value Object

All the logic to validate a URL is implemented inside a Value Object whose name is UrlValue.

Type Value Object

All the logic to validate a speech type is implemented inside a Value Object whose name is SpeechType.

And the Speech Domain Object looks like this:

Entities and Aggregates

Remember that an Entity is identified by Reference Equality and Identifier Equality and has an Id field. So let us create a base Entity class : Entity<T>, and generate Equals and GetHashCode on the Id Field. If two entities, E1 and E2, have the same id, then E1==E2 should return true

"A DDD aggregate is a cluster of domain objects that can be treated as a single unit. An example may be an order and its line-items, these will be separate objects, but it's useful to treat the order (together with its line items) as a single aggregate." - Martin Fowler

An aggregate should be always in a valid state and each aggregate should have a root which is an entity, and classes that do not belong to this aggregate can only reference the aggregate root.

So, let us create a base class, AggregateRoot<T>, that inherits from Entity<T>. I make it generic because T is the type of the Id field. And it can vary according to these entities:

Domain Events

Domain Events enable communication between bounded contexts by avoiding direct calls. So a bounded context, B1, raises an event and one or more bounded contexts, B2...Bn subcribers to this event, should handle the event to consume it.

So let us create a base class called DomainEvent:

But here, because of my strategy of implementing event sourcing, all the events produced by my bounded context will be saved in my eventstore. And other bounded contexts, services or other programs interested in these events will have to subscribe to a service bus.

For example, each time I create a new Speech, then I will create a SpeechCreatedEvent event:

The SpeechCreatedEvent class must inherit from the  DomainEvent base class:

The final implementation of my aggregateroot will look like this :

And because Speech Entity is the aggregate root, let us go ahead and inherit it from AggregateRoot<Guid>, use the Id field of Speech Entity as a guide:

Let us add a few tests to cover domain events:

LogCorner.EduSync.Speech.Application and LogCorner.EduSync.Speech.Domain yield 100 percent code coverage:

Thank you for reading!

The source code is availabe here: RegisterSpeechDomain.

Topics:
microservices ,microservices tutorial java ,event-driven architechture ,microservices architecture

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}