我希望Unity 2.0所做的是通过一直从配置中获取新属性来实例化我需要的东西,这有点难以解释。
基本上这就是我想要做的:
global.asax
container.RegisterType<IBackendWrapper, BackendWrapper>(new InjectionProperty("UserIdent", (HttpContext.Current.Session == null ? new UserIdent() : HttpContext.Current.Session["UserIdent"] as UserIdent))); 我希望这样做的是,每当有人需要IBackendWrapper时,unity就应该获取会话“UserIdent”,并用该信息填充BackendWrapper。
现在unity只加载此信息一次,即使我在会话中存储了一个用户标识,它也总是返回一个新的UserIdent。在Unity 2.0中有没有办法实现这种行为?或者它被另一个像NInject这样的IoC框架支持?
发布于 2011-05-30 22:41:32
是的,Unity支持它。您需要在InjectionFactory中注册UserIdent,以便在每次解析时对其进行评估。
container
.RegisterType<UserIdent>(new InjectionFactory(c =>
{
return HttpContext.Current.Session == null
? new UserIdent()
: HttpContext.Current.Session["UserIdent"] as UserIdent;
}));
container
.RegisterType<IBackendWrapper, BackendWrapper>(
new InjectionProperty("UserIdent", new ResolvedParameter<UserIdent>())
);按照你注册的方式,在你注册的时候HttpContext.Current.Session已经被评估过了,大概是在你的会话建立之前的Global.asax中。
https://stackoverflow.com/questions/6177347
复制相似问题