Mapping The Classes And Session Source Configuration
As mentioned in my previous
this series will no longer be a talk about DDD as a blog app is too
trivial to show ( learn ) the power of it. I will be using some of the
patterns of DDD though. So I have changed the name of the app I am
building. It will be called MyPersonalBlog ( MPBlog ) henceforth.
The project structure has changed a bit from what I had shown
There is no longer separate layers for infrastructure and domain. I have
clubbed these 2 layers into a single Core layer. The other layers are
just the same.
Lets get started by creating our domain objects.
The domain objects
So far we have created a entity base class from which all our domain (
models ) will inherit from. In a blog application, we’ll have posts,
tags and comments. So lets create classes for each of these. I have
created these classes as shown in the class diagram below.
From the diagram you can see that there is a many-to-many relation
between Tag and Post class..
I am using Fluent NHibernate to
configure the mappings of these classes to the database. Although you
can map all the classes using the
which has a convention based API, I will use the mapping per class
feature. Lets look at the mappings.
We create a PostMap class which inherits from the ClassMap<T>. The
constructor of this PostMap class is where all out mappings go. As can
be seen, first we specify the table name for our Posts, Then we specify
the Id property which is mapped to the PostId column in our database.
The Id in our case is the id that we had declared in the Entity base
class ( I have renamed the Key property to Id ). If you recall from my
it had only the getter property. The value is assigned by NHibernate. We
specify a many to many relation between posts and tags through an
intermediate table PostToTags. We have also specified that a post has
many comments thought the Comments table The other mappings are self
Out Tag and Comment mapping classes look similar to the one above.
Following are the two mapping files for these.
Now that our mappings are done, lets create a SessionSource to access
the NHibernate Session.
We can create a session through the ISessionSource interface in Fluent
NHibernate. Lets create an interface for the SessionSourceConfiguration.
Note: The following is based on the FubuMVC contrib
ISessionSourceConfiguration and *SessionSourceConfiguration
The ResetDatabase property is used to either create a new database from
our mappings or reset the database. We have a method called
GetProperties from which we generate the connection properties.
As you can see in the CreateSessionSource method, we are passing a
PersistenceModel object. This object is used to add the mapping from a
specified assembly, in our case, where we have defined the Post, Tag and
Comment maps. Lets call it MPBlogPersistenceModel. Here is what it looks
The PresistenceModel object, connectionString, and resetDB parameters
are injected using an IoC container which we’ll have a look in a later
Following are the tests I have written to check my mapping are correct.
I am using an in memory SQLite database and the
PersistenceSpecification<T> provided by Fluent NHibernate to check my
That’s it for now. In my next post, I’ll discuss about the repository
and the unit of work implementations. Any sort of feedback will be