Tuesday 4 December 2012

NHibernate, Ninject 3, MVC 4

Recently I have started to use Ninject more and more, its my favourite of all the IOC frameworks I have used so far. Also I wanted to give MVC 4 a dabble just to keep myself in the loop with what is going on in the MVC world after only using MVC 2 in the past. So I thought I would try to wire up something nice between MVC4, Ninject 3 and NHibernate and came up with the following located on github here.

So in a previous post with NHibernate, Unit of Work, Repository and Structuremap, I carped on about a Unit of Work and a Repository.


With regards to the repository...

I think I have gone off the idea of using the Repository pattern with NHibernate after reading lots of posts on Ayende's site, various other sites and from previous experience I feel it is limiting NHibernate to make optimal calls to the database. I believe NHibernate's Session is the repository. Here are a couple of links:
Architecting in the pit of doom: The evils of the repository abstraction layer
Repository is the new singleton pattern


With regards to the Unit of Work...

Here I just set up a filter to wrap up the work done across a transaction. So now any of the work in the domain model/business logic that is done via a particular controller action should all be wrapped up in a transaction. I wired up the filter using the Ninject.MVC3 extension and the Kernel.BindFilter.


Github project...

Its been a while since I looked at this project and what I was doing, so I will need to refresh myself with it as well. I just created the default MVC 4 app and added a new controller, the ProductController. I just wanted to be able to show a list of Products that are stored in the database and also be able to add new products and render the change.

Libraries I used:
MVC 4
Ninject 3
Ninject.MVC 3 extension
NHibernate
Automapper

The project isn't finished yet, I would like to try to wire up NServiceBus and RavenDB in here too (but these are just distance thoughts for the moment), and really push all the business logic down into the domain model.

Some points on what I did.
  • Created the standard ASP.NET MVC 4 application. 
  • Added NHibernate and created a simple database relationship for stores, products and employees, I may have taken these relationships from somewhere else but I can't remember.
  • Added Ninject and its Ninject.MVC 3 extension, this works for MVC 4.
That's it for now... I will come back and update this some more when I get a chance...

No comments:

Post a Comment