ASP.NET MVC + Unity 2.0:延迟加载依赖项属性?

问题描述

| 我希望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中进行的。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...