Unity - ResolveAll by name with a condition

I was wondering if it is possible to resolve all dependencies in Unity by some condition of the name that they registered.

For example: Overriding all registered interfaces where the registered name begins with "ProcessA".

And if there is no way to do this, perhaps how can I extend Unity to allow this.

+3
source share
2 answers

You can use Registrationsfor this, and I would recommend using the extension method, rather than directly distributing Unity:

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

}
+4
source

, , , , , .

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

+1

All Articles