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

后端 未结 2 1420
时光取名叫无心
时光取名叫无心 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:49

    I've not used structure map but maybee I can still help guide you in the right direction. Fluent nHibernate is awsome good choice over the hbm files.

    As for the http request, you do not need to ensure that you close the session when the http request ends. If you don't you'll end up leaking nHibernate session. I'm not sure if structure map will handle this for you, what I've done is I have an http module which closes the session.

    One thing to note though that bite me, is that you will make to sure you wrap all your data access in a transaction and ensure nHibernate actually commits its changes. If you do this as part of your session close, you could miss the chance to handle errors. I'm curious to hear what you ended up having to do to get this workign.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题