团结 - 按名称解决所有条件

本文关键字:解决 有条件 团结 | 更新日期: 2023-09-27 17:55:58

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

例如:重新创建注册名称以"ProcessA"开头的所有注册接口。

如果没有办法做到这一点,那么也许我该如何扩展 Unity 以允许这样做。

团结 - 按名称解决所有条件

您应该能够使用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);
        }
    }
}

只是补充迈克尔的答案,我扩展了它,以允许您使用相同的名称注册,但使用 resolve all 来仅解析针对该名称注册的名称。

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