In my previous post, we looked at setting up the project structure and
the tools and libraries that we’ll need to build the application. In
this post we’ll look at creating a few base classes.
Layer SuperType: The Entity Base Class
Before we start of creating the domain objects, lets first create the
base entity object. As discussed before, an
entity
is something that has an identity. This would be our layer supertype
because this class will be inherited by all our domain objects that
needs to have an identity.
The Entity base class is defined in the Infrastructure layer as the
definition of entity is not part of the domain itself but it will be
used by the domain. As you can see the Entity class has a key property
which would be used as an identity. Also I am taking the key to be
integer for simplicity. I have also implemented the equality comparison
on the entity object. This is used to compare the entities.
Entity Tests
Now lets see some tests on the Entity class. A thing to remember is that
in TDD, we would have to write the tests firsts and then come at the
object.
AggregateRoot And IRepository<T>
Along with the Entity base class, we also need a repository base, and a
way to mark an entity as an aggregate root. These are shown in the
following snippets
As you can see our Aggregate root base is just an interface that
inherits from IEntity. This is done because a repository is ( should be
) provided only on the Aggregate root. You can read a bit on Aggregates
and Repositories
here.
Now that our base classes are ready, we can look at the domain objects
of our sample application, which I will be talking about in the next
post.
You must know that, I am by no means an expert in DDD, TDD. The code
shown above might be wrong as I am just stepping into this part of the
world. Please leave a comment/ suggestion if you find something to be
wrong or if it could be done better.
Comments