Using NHibernate in Asp.Net MVC
HI All,
What are the steps to use NHibernate from the mode开发者_如何学Gol component of MVC?
the Models folder in ASP.NET MVC should be for View Models (return complex objects to the controler=>view), NHibernate ISessionFactory should be started in the Application_Start event in your global.asax for the MVC project. The Mapping and Entities themselves I would recommend to live in a different project, normally with a combination of folders called Mappings/Domain/Persistence to keep it simple. For a more structured solution I would look at the DDD layers, but that is alot of complexity for small implementations and doesn't works for every project.
Nothing is different you add the hibernate.cfg.xml in the root of the web project and add your mapping files and you are good to go
Have a look at SharpArchitecture, this is best practice for MVC with NHIbernate.
http://code.google.com/p/sharp-architecture/
I guess, the only thing specific to nHibernate in MVC is the way you handle the session life cycle. Most people prefer the session-per-request implementation pattern where for each http request a new session is created (sessions are cheap to create in nHibernate). In contrast, session factories are rather expensive in terms of creation and should be created once for the whole application.
... and I strongly suggest you read ASP.NET MVC 2 in Action by Jeffrey Palermo. It handles specifically nHibernate with Asp.net MVC.
精彩评论