开发者

ASP MVC + Castle + nHibernate how to connect?

What is the best way to connect asp mvc + castle windsor + nhibernate?

I have repository which is injected into my controller constructor.

Is this a good and correct way of doing this :

Component.For<ISessionFactory>().UsingFactoryMethod开发者_StackOverflow(()=> CreateSessionFactory() ).LifeStyle.Singleton,

Component.For<ISession>().UsingFactoryMethod ( k=>k.Resolve<ISessionFactory>().OpenSession()).LifeStyle.PerWebRequest

and then just inject ISession into repository constructor ?

What about transactions and disposing session ?

Thanks for help!


MVC + Castle + NHibernate? The best way would be to use S#arp Architecture instead of reinventing the wheel.

Pronounced "Sharp Architecture," this is a solid architectural foundation for rapidly building maintainable web applications leveraging the ASP.NET MVC framework with NHibernate. The primary advantage to be sought in using any architectural framework is to decrease the code one has to write while increasing the quality of the end product. A framework should enable developers to spend little time on infrastructure details while allowing them to focus their attentions on the domain and user experience. Accordingly, S#arp Architecture adheres to the following key principles:

  • Focused on Domain Driven Design
  • Loosely coupled
  • Preconfigured Infrastructure
  • Open Ended Presentation

The overall goal of this is to allow developers to worry less about application "plumbing" and to spend most of their time on adding value for the client by focusing on the business logic and developing a rich user experience.

A full example can be found here.


I created a facility for you and anyone else who wants to do this.

https://github.com/haf/Castle.Facilities.NHibernate/wiki/NHibernate-Facility---Quick-Start

Nuget: Castle.Facilities.NHibernate

Tell me if you find it worthwhile. It's very minimalistic. Just put a [Transaction] around the command handler that is handling the current unit of work. I don't recommend multiple services with disparate transactions. As such, I created a new lifestyle in Windsor called per transaction.

I can give you more help on the castle mailing list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜