问题
I was wondering if it is possible to resolve all dependancies in Unity by some condition on the name that they were registered.
For example: Resloving all interfaces registered where the name registered starts with "ProcessA".
And if there is no way to do this then perhaps how can i extend Unity to allow this.
回答1:
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);
}
}
}
回答2:
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
来源:https://stackoverflow.com/questions/5926525/unity-resolveall-by-name-with-condition