Unity - ResolveAll by name with condition

痞子三分冷 提交于 2019-12-06 00:42:12

You should be able to use Registrations to do this and I would recommend an extension method rather than extending Unity directly:

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

Using this extension method:

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);
        }
    }

}

Just adding to Michael's answer, I extended that to allow you to register with the same names yet use resolve all to only resolve those registered against that name.

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);
        }
    }
}

Allows for registration and resolution like this:

        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 will container both Foo1 and Foo2, scope2Foos will contain both Foo3 and Foo4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!