IOC寄存器实例与问题MVC3,团结和放大器; NHibernate的 - (由codePLEX Unity.MVC3 LIB) [英] IoC Register Instance Issue with MVC3, Unity & NHibernate - (Unity.MVC3 lib from codeplex)

查看:154
本文介绍了IOC寄存器实例与问题MVC3,团结和放大器; NHibernate的 - (由codePLEX Unity.MVC3 LIB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是从 codePLEX 的Unity.MVC3.dll并在尝试为每个请求注册的NHibernate的ISession的实例。按照统一MVC3文档,我应该只需要使用HierarchicalLifetimeManager做到这一点:

I'm using the Unity.MVC3.dll from codeplex and am attempting to register an instance of a NHibernate ISession per request. As per the Unity MVC3 docs I should just have to use the HierarchicalLifetimeManager to accomplish this:

var container = new UnityContainer();
container.RegisterInstance<ISession>(SessionFactory.OpenSession(), new HierarchicalLifetimeManager());
container.RegisterControllers();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

SessionFactory的是我的Global.asax静态对象。当我添加的Isession 来我的控制器之一,我收到以下错误信息:

SessionFactory is a static object in my Global.asax. When I add ISession to one of my controllers I receive the following error message:

当前类型,NHibernate.ISession,是一个接口,并且不能
  建。是否缺少类型映射?

The current type, NHibernate.ISession, is an interface and cannot be constructed. Are you missing a type mapping?

我想我没必要 RegisterType 时,只注册一个实例?

I thought I didn't need to RegisterType when just registering an instance?

在我的控制器我有:

[Dependency]
public ISession session { get; set; }

我也尝试添加的Isession作为参数传递给我的ActionResult功能之一

I also tried adding ISession as an argument to one of my ActionResult functions

我要补充一点,这个问题是不是NHibernate的结束。配置是成功的,我可以用它来查询从我的数据库信息。

I'll add that the issue is not on NHibernate's end. The configuration is successful and I can use it to query info from my database.

更新

当我不指定一辈子经理它的工作原理,但是,这不会让我我的每个请求一个实例,我所需要的。

When I do not specify the lifetime manager it works, however, this isn't going to get me my "one instance per request" that I need.

我可能要探索Ninject:P

I might have to explore Ninject :p

推荐答案

尝试使用registertype用注塑厂,而不是registerinstance。

Try using registertype with an injection factory rather than registerinstance.

是这样的:

Container.RegisterType<ISession>(new InjectionFactory(c => SessionFectory.OpenSession()), new HierarcicalLifetimeManager());

这篇关于IOC寄存器实例与问题MVC3,团结和放大器; NHibernate的 - (由codePLEX Unity.MVC3 LIB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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