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 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.
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 .
Id(x => x.Id);
Map(x => x.Name);
References(x => x.Department);
Component<Address>(x => x.HomeAddress,
a.Map(x => x.Line1, “HomeAddress_Line1”);
a.Map(x => x.Line2, “HomeAddress_Line2”);
Component<Address>(x => x.WorkAddress,
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