如何解决ASP.NET MVC + Unity 2.0:延迟加载依赖项属性?
| 我希望Unity 2.0要做的是通过始终从配置中获取新属性来实例化我所需的内容,这有点难以解释。 基本上这就是我想要做的: global.asaxcontainer.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中进行的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。