using (Fluent) NHibernate with StructureMap (or any IoCC)

后端 未结 2 1419
时光取名叫无心
时光取名叫无心 2021-02-04 20:29

On my quest to learn NHibernate I have reached the next hurdle; how should I go about integrating it with StructureMap?

Although code examples are very welcome, I\'m mor

2条回答
  •  生来不讨喜
    2021-02-04 20:51

    I use StructureMap with fluent-nhibernate (and NH Validator) in 3 of my current projects. 2 of those are ASP MVC apps and the third is a WCF web service.

    Your general strategy sounds about right (except you won't be making your own Session or SessionFactory, as was already pointed out in comments). For details, snag my configuration code from here:

    http://brendanjerwin.github.com/development/dotnet/2009/03/11/using-nhibernate-validator-with-fluent-nhibernate.html

    The post is really about integrating NH Validator and Fluent-NHibernate but you can see exactly how I register the session factory and ISession with StructureMap in the "Bonus" section of the post.

    RE: Tidy up: You should try and always work within a transaction and either commit or roll-back the transaction at the end of your unit of work. NH only utilizes SQL Connections when it needs them and will take care of the cleanup of that limited resource for you. Normal garbage collection will take care of your sessions themselves.

    The Session Factory is a very expensive object that you will want to only initialize once and keep around for the life of your app.

提交回复
热议问题