首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET MVC +Unity2.0:延迟加载依赖属性?

ASP.NET MVC +Unity2.0:延迟加载依赖属性?
EN

Stack Overflow用户
提问于 2011-05-30 22:08:52
回答 1查看 1.3K关注 0票数 3

我希望Unity 2.0所做的是通过一直从配置中获取新属性来实例化我需要的东西,这有点难以解释。

基本上这就是我想要做的:

global.asax

代码语言:javascript
复制
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框架支持?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-30 22:41:32

是的,Unity支持它。您需要在InjectionFactory中注册UserIdent,以便在每次解析时对其进行评估。

代码语言:javascript
复制
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中。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6177347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档