问题描述
|
我希望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时,团结都应获取Session [\“ UserIdent \”]并用该信息填充BackendWrapper。
现在unity仅加载一次此信息,即使我在会话中存储了用户标识,它也总是返回一个新的UserIdent。有没有办法在Unity 2.0中获得这种行为?还是由另一个IoC框架(如NInject)支持?
解决方法
是的,它在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>())
);
注册时,正在注册时正在评估ѭ4,这大概是在建立会话之前在Global.asax中进行的。