Nhibernate ,Fluent nhibernate and Repository Pattern works great together

As I am trying to take a deep dive in Nhibernate world since last month , I have read a lot of articles and blogs .All were very good and some of them were really helpful. Well though all the stuff i have looked over internet was for sure interesting but i hardly find any of them who can talk about a running sample application(at least i didn’t find that ). So i have decided to try to write something which is simple and you can actually see it working immediately. I hope this will help to all those folks who are making their mind to have a look at this wonderful ‘state of art’ as i call it 😉

You can get details of how to download all required details to start form here

So lets start ..


When we start developing any business application , we talk about so many things .But i thing we never talk about how we will make our application speak with the database ???? One answer comes in  my mind for this question , if i ask it , is that “You stupid … is this your first time developing some application !!! humm..We off course will do it the same way , we are doing it since last so long time ..WRITE DATA ACCESS LAYER AND SCRIPTS“. So we really do not want to spend a lot of unnecessary time on discussing this obvious part of application.

Well when project start the UI and other modules are ready but….  We say GUYS LETS FIRST CREATE THE PROTOTYPE . Well needless to say in most of the cases, the project continues on the application created for the prototype.  Most of the time in developing an application is in writing Data Access layer and Scripts and offcourse to synch it with our so call Prototyped application.  It would be nice to have something which helps me to concentrate on writing my business application rather worrying how i will save this data in to the database..  ORM (Object Relational Model)is the answer .. Off course there are many ORM’s available on the web but i Nhibernate is one of the most popular one.


In this sample application we will implement the very basic scenario . I have Employee and Department entities .We also have a value object Address. The difference between Entity and Value object is that , value object do not have any identity but entities have identity. Employee and Department object shares many-to-one relationship . one department can have many employees.


In this sample we are going to implement a base repository so that we do not have to write similiar methods in for all the objects. I have created an iRepository interface of generic type and implemented in a base repository. With this approach we implement the  Repository Pattern

also, in our small application .

Nhibernate mapping

Nhibernate is an ORM and we need to map object with the data context properties.Generally Nhibernate provide support to do Mapping in an XML file xx.hbm.xml. This way we decouple our mapping from the business logic and data context.We can easily change the mapping in the xml mapping file.

Why Fluent Nhibernate

Well if Nhibernate already support mapping in xml file than why we need Fluent nhibernate  for mapping. Its not mendatory to use Fluent nhibernate in your application but its really helpfull to avoid  unnecessary changes/missing mapping.We  do mapping in c# code  so that if there is any change in the mapping than we get immediately a compilation error and its easy to fix the problem . Needless to say , compiler does not compile xml  so if there is any problem in mapping , we cannot find it in compile time.. and it can really take long time some time to identify the problem.

Component mapping

In Nhibernate we have <bag> node and it has its property. Fluent  nhibernate has similiar functionality and we call it component.

i.e Employee has Address properties but  we will save it the employee table so we need to map it as component in the EmployeeMap class.  All the mapping files are inheriting ClassMap which helps Fluent Nibernate to identify the mapping files .

public EmployeeMap()


Id(x => x.Id);

Map(x => x.Name);

References(x => x.Department);

Component<Address>(x => x.HomeAddress,

a =>


a.Map(x => x.Line1, “HomeAddress_Line1”);

a.Map(x => x.Line2, “HomeAddress_Line2”);


Component<Address>(x => x.WorkAddress,

a =>


a.Map(x => x.Line1, “WorkAddress_Line1”);

a.Map(x => x.Line2, “WorkAddress_Line2”);




Sample Application solution structure:
You can download  source code from here
Note : please change connection setting to tun the sample

13 thoughts on “Nhibernate ,Fluent nhibernate and Repository Pattern works great together

  1. Hi,

    I am trying to use this and everything is good until i try using the “include” method and nothing happens. The following is what I am trying:

    public IQueryable FindByIncluding(Expression<Func> predicate, params string[] includeProperties)
    IQueryable query = _entities.Set().AsExpandable().Where(predicate);

    foreach (var includeProperty in includeProperties)
    query = query.Include(includeProperty);

    return query;

    Are you able to tell me why this is not working?

    1. Hello Aruna
      I believe your sample code in comment is coming from
      EF and not from Nhibernate ORM ( just a guess as i do not know the real implementation).

      In Nhibernate we do not have IncludeProperty but trick is you can use its ExcludeProperty It will do the work for you .

      See here for more accurate sample code and details


      Let me know if you still have troubles with it .
      Have a nice day 🙂

  2. Hello excellent blog! Does running a blog similar to this require a
    large amount of work? I have absolutely no
    understanding of computer programming but I was hoping to start my own blog in
    the near future. Anyhow, should you have any suggestions or tips for
    new blog owners please share. I understand this is off subject but I just had to ask.

    Many thanks!

    1. Hello Barbara
      Thanks for the appreciation.
      I believe its a good idea to have a blog so you can share your knowledge and can interact with people from all over the world.

      I am using WordPress.com and this is the best blog hosting portal. It does not take a lot of work and it takes only one minute to start.
      All is free.

      Please let me know if you want any further help on it .
      have a nice day.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s