Unity - ResolveAll by name with condition

时间:2011-05-08 09:03:34

标签: c# unity-container ioc-container

我想知道是否可以通过注册名称的某些条件解决Unity中的所有依赖项。

例如: 重新注册注册名称的所有接口以“ProcessA”开头。

如果没有办法做到这一点,那么我怎么能扩展Unity以允许这个。

2 个答案:

答案 0 :(得分:4)

您应该可以使用Registrations来执行此操作,我建议使用扩展方法,而不是直接扩展Unity:

var matches = c.Resolve<IMyService>(name => name.StartsWith("ProcessA"));

使用此扩展方法:

public static class MyUnityExtensions
{
    public static IEnumerable<T> Resolve<T>(this IUnityContainer c, Func<string, bool> match)
    {
        var matches = c.Registrations.Where(r => match(r.Name));

        foreach (var registration in matches)
        {
            yield return c.Resolve<T>(registration.Name);
        }
    }

}

答案 1 :(得分:1)

只是添加迈克尔的答案,我扩展了它,允许您注册相同的名称,但使用全部解决方案仅解析那些注册该名称的人。

public struct ScopedName<T>
{
    private const string Separator = "|";

    private readonly string _name;
    private readonly string _registrationName;

    public ScopedName(string name)
        : this()
    {
        _name = name;
        _registrationName = name + Separator + typeof(T).FullName;
    }

    public static implicit operator string(ScopedName<T> scopedName)
    {
        return scopedName._registrationName;
    }

    public bool IsMatach(string other)
    {
        if (string.IsNullOrWhiteSpace(other))
        {
            return false;
        }

        var i = other.IndexOf(Separator, StringComparison.InvariantCulture);
        if (i < 0)
        {
            return false;
        }

        return string.Equals(_name, other.Substring(0, i), StringComparison.InvariantCulture);
    }
}

public static class UnityEx 
{
    public static IUnityContainer RegisterType<TFrom, TTo>(
        this IUnityContainer container,
        ScopedName<TTo> scopedName,
        LifetimeManager lifetimeManager,
        params InjectionMember[] injectionMembers) where TTo : TFrom
    {
        return container.RegisterType(typeof(TFrom), typeof(TTo), scopedName, lifetimeManager, injectionMembers);
    }

    public static IEnumerable<T> ResolveAll<T>(this IUnityContainer container, ScopedName<T> name, params ResolverOverride[] resolverOverrides)
    {
        var matches = container.Registrations.Where(r => name.IsMatach(r.Name));

        foreach (var registration in matches)
        {
            yield return container.Resolve<T>(registration.Name, resolverOverrides);
        }
    }
}

允许注册和解决方案如下:

        container.RegisterType<IFoo, Foo1>(new ScopedName<Foo1>("Scope1"), new HierarchicalLifetimeManager());
        container.RegisterType<IFoo, Foo2>(new ScopedName<Foo2>("Scope1"), new HierarchicalLifetimeManager());

        container.RegisterType<IFoo, Foo3>(new ScopedName<Foo3>("Scope2"), new HierarchicalLifetimeManager());
        container.RegisterType<IFoo, Foo4>(new ScopedName<Foo4>("Scope2"), new HierarchicalLifetimeManager());

        var scope1Foos = container.ResolveAll(new ScopedName<IFoo>("Scope1"));
        var scope2Foos = container.ResolveAll(new ScopedName<IFoo>("Scope2"));

scope1Foos将容器Foo1和Foo2,scope2Foos将包含Foo3和Foo4