2

Due do LinqToSql not being appropriate for Many To Many relationships I am in the process of deciding to move to NHibernate (Fluent NHibernate) unless convinced otherwise...

Project Structure: UI (Mvc2 app with Ninject wiring up all services to controllers, and repositories to services), DomainServiceLayer (all util, helpers, services, domain model etc) and my Repository Layer for persistence. I have a another project call Model which basically exposes the entities, which all projects reference.

Basically I am creating my mappings within the Repository Layer with references to NHIbernate and Fluent NHIibernate, I hope to expose the interfaces to the Domain Service for querying and persisting data. How do I wire up the iSession, where do I wire it up? Any example code, what project should I put it in? Ideally I want to keep this within the Repository Layer... Is it worth learning NHibernate and going through all this?

Haroon
  • 3,402
  • 6
  • 43
  • 74
  • http://www.sharparchitecture.net/ – Mauricio Scheffer Jan 31 '11 at 00:14
  • I do not really want to hook up sharparchitecture, I have most of my project in place, plus I am happy with the level of control I have already. I only wanted to add NHibernate 3.0 within the repository layer not mess around with my entire Asp.net MVC UI project. – Haroon Feb 07 '11 at 08:16
  • Right, but you can learn and take code from there. – queen3 Feb 13 '11 at 18:35

3 Answers3

6

I recommend looking at the blog posts of Bob. He describes in detail how to use the repository pattern in Ninject using NHibernate. I planned adding an example in the near future to the sample application comming with the MVC exptension as this question comes up again and again.

http://blog.bobcravens.com/2010/06/the-repository-pattern-with-linq-to-fluent-nhibernate-and-mysql/

http://blog.bobcravens.com/2010/07/using-nhibernate-in-asp-net-mvc/

http://blog.bobcravens.com/2010/09/the-repository-pattern-part-2/

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Looks very good! You have almost won my vote, I will need to look through thoroughly then I will accept! – Haroon Feb 09 '11 at 11:59
3

Typically I have an NHibernateSessionFactory which is a singleton that has an OpenSession method and I bind ISession typically like this.

Bind<ISession>().ToMethod(context => 
NHibernateSessionFactory.Instance.OpenSession()).InRequestScope();

This method just calls through to ISessionFactory.OpenSession You can put this into a NinjectModule in your repository layer, which your app can load when it creates the Kernel.

Vadim
  • 17,897
  • 4
  • 38
  • 62
  • Lets say I have a repository layer, a seperate project to the rest of my app, ideally I would inject the ISession to the repository class and use the session to fulfill its role. Would that mean I still need to hook anything up inside the Global.asax? – Haroon Feb 07 '11 at 08:41
  • @Haroon Yes because you need to create the NInjnect Kernel and load all of your modules on Application_Start – Vadim Feb 07 '11 at 14:15
2

I do the configuration in the Application Layer (i.e. the top layer) as the configuration differs between applications. But it can be useful to break out some of the configuration into classes stored in the Repository Layer.

I open and close the session with an HttpModule.

cbp
  • 25,252
  • 29
  • 125
  • 205
  • Could you please give me an example of codeor point me to the correct direction? Are you exposing NHibernate to your Web App? – Haroon Jan 31 '11 at 08:09
  • NHibernate is exposed to my web app a little bit, yes. Look at Sharp Architecture or read a good book like Sanderson's 'Pro Asp.NET MVC Framework' – cbp Feb 04 '11 at 05:34
  • Pro Asp.net MVC framework unfortunately does not cover this, I already have the book. It talks briefly about the domain layer but it does not go into any detail. I can expose an ISession within my repository layer and hook everthing up cleanly, via DI but I need to set up filters such as Transactional and I think my Global.asax needs to handle some events too... trying to figure out how they wire up.. – Haroon Feb 07 '11 at 08:19