如何在 Unity 和 DependencyResolver 中使用会话值 [英] How to use session values with Unity and DependencyResolver

查看:24
本文介绍了如何在 Unity 和 DependencyResolver 中使用会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 MVC4 和 Unity 2.1.我的服务需要基于从会话状态检索到的凭据的服务密钥.

I'm using MVC4 and Unity 2.1. My services require a service key based on credentials retrieved from session state.

我像这样注册我的服务:

I register my service(s) like so:

container.RegisterType<IInventoryService, InventoryService>();

InventoryService 的构造函数同样简单:

The constructor for InventoryService is equally simple:

public InventoryService(ServiceKey serviceKey) {  ...  }

在我的网站中,当我需要一项服务时,我会使用服务定位器,该定位器使用会话中的凭据自动组合服务密钥.

In my website when I've needed a service I use a service locator that automatically composes the service key using credentials from session.

public static T Resolve<T>(ServiceKey serviceKey = null)
    {
        if (serviceKey == null)
        {
            serviceKey = SessionManager.ServiceKey;
        }

        var parameterOverride = new ParameterOverride(SERVICEKEY_PARAMETERNAME, serviceKey);

        return Resolve<T>(null, parameterOverride);
    }

效果很好.问题是我现在正在将我的网站转换为 MVC 并尝试使用一个简单的依赖解析器将服务注入控制器,该依赖解析器使用我现有的服务定位器(依赖工厂):

This has worked well. The problem is that I'm now converting my site to MVC and attempting to inject services into controllers using a simple dependency resolver that uses my exiting service locator (dependency factory):

public class CustomDependencyResolver : IDependencyResolver
{
    public object GetService(Type serviceType)
    {
        return MvcDependencyFactory.Resolve(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return MvcDependencyFactory.ResolveAll(serviceType);
    }
}

我的控制器看起来像:

public InventoryController(IInventoryService inventoryService) { ... }

问题是MVC在尝试实例化库存控制器时仍然抱怨没有找到无参数的构造函数.我认为这是因为我还没有在 Unity 中注册服务密钥.但是,如果我尝试这样做,我会发现 MVC 正在尝试在构建会话之前解析控制器,以及随后的服务.

The problem is that MVC still complains about not finding a parameterless constructor when trying to instantiate the inventory controller. I think this is because I haven't registered a service key in Unity. But if I try doing so, I find that MVC is trying to resolve the controllers, and subsequently the services, before session has even been constructed.

我是不是没有正确考虑这个问题?每一步都感觉非常合理——在服务中使用会话凭证,在控制器中使用服务,使用解析器来帮助构建控制器——但我一直在努力使其工作.

Am I not thinking about this correctly? Each step feels pretty reasonable -- using session credentials in a service, using a service in a controller, using a resolver to help build the controller -- but I've been beating my head against the wall getting this to work.

推荐答案

您可以使用 Unity 中的 InjectionFactory (Microsoft.Practices.Unity.InjectionFactory) 指定一个函数来处理您的依赖项的解析.只有在解决依赖关系时才会执行此函数.在下面的示例中,c"是作为参数传递的 Unity 容器,以便您可以在函数中执行其他解析.

You can use the InjectionFactory in Unity (Microsoft.Practices.Unity.InjectionFactory) to specify a function to handle the resolution of your dependency. This function will only be executed when the dependency is resolved. In the below example, "c" is your Unity container passed as a argument so that you can do additional resolves within your function.

替换:

container.RegisterType<IInventoryService, InventoryService>();

与:

container.RegisterType<IInventoryService>(new InjectionFactory(c =>
    new InventoryService(SessionManager.ServiceKey)));

这篇关于如何在 Unity 和 DependencyResolver 中使用会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆