使用(流利)NHibernate的与StructureMap(或任何IOCC) [英] using (Fluent) NHibernate with StructureMap (or any IoCC)

查看:115
本文介绍了使用(流利)NHibernate的与StructureMap(或任何IOCC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的追求,了解NHibernate的我已经达到了下一个障碍;我应该如何去与StructureMap整合呢?

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 more interested in the general procedure.

我正打算做了...


  • 使用功能NHibernate创建我使用的类映射在NHibs配置

  • 执行的ISession和ISessionFactory

  • 自引导我ISessionFactory到StructureMap实例作为一个单身

  • 与StructureMap ISession的注册,每-的HttpRequest缓存

  • Use Fluent NHibernate to create my class mappings for use in NHibs Configuration
  • Implement ISession and ISessionFactory
  • Bootstrap an instance of my ISessionFactory into StructureMap as a singleton
  • Register ISession with StructureMap, with per-HttpRequest caching

但是,不要我需要调用我的会话各种整洁行动方法例如在的HttpRequest结束时(因为这就是它的生命结束)?

However, don't I need to call various tidy-up methods on my session instance at the end of the HttpRequest (because thats the end of its life)?

如果我做的Dispose()整洁行动,将structuremap照顾这我?

If i do the tidy-up in Dispose(), will structuremap take care of this for me?

如果不是,那我该怎么办?

If not, what am I supposed to do?

感谢

安德鲁

推荐答案

我用StructureMap在3流利,NHibernate的(和NH验证)我目前的项目。这2人是ASP MVC应用程序和第三个是一个WCF Web服务。

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.

您总体战略听起来是正确的(除非你不会让自己的会话或SessionFactory的作为已经在评论中指出)。有关详细信息,从这里钩住我的配置代码:

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

该职位实际上是关于整合NH验证和流利,NHibernate的,但你可以清楚地看到我如何注册会话工厂,并与的ISession在StructureMap 。后的红利栏目

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:整理一下:你应该尝试在一个事务中总是工作并提交或在年底回滚事务你的工作单位。 NH只利用SQL连接时,它需要他们,并会采取你有限的资源的清理照顾。普通垃圾收集会照顾你的会话自己。

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.

这篇关于使用(流利)NHibernate的与StructureMap(或任何IOCC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆