我认为@Atomaras可能对您的抽象有一个很好的观点,尽管我认为只有在构图词根中使用此信息时会很好,因为您的构图根已经意识到系统中的每个实现。
我认为有几种方法可以获取此信息:
使用
DecoratorPredicateContext
提供给的信息RegisterDecorator
扩展方法:var typeMapping = new Dictionary<Type, Type>(); container.RegisterDecorator(typeof(IInstructionHandler<>), typeof(FakeDecorator<>), c => { typeMapping[c.ServiceType] = c.ImplementationType; // or perhaps even use c.AppliedDecorators property to see which decorators // are applied. // return false to prevent the decorator from being applied. return false; });
您可以进行虚假注册,简单喷油器会要求每个注册
IInstructionHandler<T>
在系统中,但是您可以通过提供始终返回的谓词来防止将其应用false
. 。您可以使用简单喷油器提供的信息DecoratorPredicateContext
找出实际实现类型是什么。
或者,您可以注入 DecoratorContext
实例(v2.6及以上)进入最高装饰器(如解释 这里)。这 DecoratorContext
包含与 DecoratorPredicateContext
确实,但是这个对象将通过简单喷油器自动注入依赖的装饰器中。它使您可以在装饰器内做出决定,这在您的情况下可能非常方便。
添加一个
IDecorator
对系统的抽象允许穿越装饰链。通过让每个装饰器实施
IDecorator
允许访问装饰的接口(就像完成一样 这里)您可以穿越装饰链并找到实际实施类型:public interface IDecorator { object Decoratee { get; } } public static class DecoratorHelpers { public static IEnumerable<object> GetDecoratorChain(IDecorator decorator) { while (decorator != null) { yield return decorator; decorator = decorator.Decoratee as IDecorator; } } }
您可以使用以下界面实现装饰器:
public class SomeDecorator<T> : IInstructionHandler<T>, IDecorator { private readonly IInstructionHandler<T> decoratee; public SomeDecorator(IInstructionHandler<T> decoratee) { this.decoratee = decoratee; } object IDecorator.Decoratee { get { return this.decoratee; } } }
当您在所有装饰器上实现此界面时,您将能够做到这一点:
var implementationTypes = from handler in container.GetAllInstances<IInstructionHandler<RenderWord>>() let mostInnerDecorator = DecoratorHelpers.GetDecoratorChain(handler as IDecorator).LastOrDefault() let implementation = mostInnerDecorator != null ? mostInnerDecorator.Decoratee : handler select implementation.GetType()
注册
Registration
一个实例RegisterAll
超载,因为Registration
对象知道实际植入类型。但是,您不妨使用用于创建这些注册的植入类型的列表,而不是第3点:
typeMapping[serviceType] = implementationTypes; container.RegisterAll(serviceType, implementationTypes);
简单的喷油器将始终以与注册相同的顺序解决注册实现(保证)。因此,当您解决事物集合时,您已经有了以相同顺序列出的实现列表。