Unity Decorator扩展失败,有多个实现

时间:2013-07-04 22:12:05

标签: c# .net-4.0 dependency-injection unity-container decorator

我几天来一直在努力解决这个问题,我仍然不确定如何解决它。

我为Unity容器创建了一个容器扩展,使我能够轻松地在容器中注册装饰器类。这是我目前的实现,几乎与this文章中的实现相同:

public class DecoratorExtension : UnityContainerExtension
{
    private int m_order;
    private Dictionary<Type, IList<DecoratorRegistration>> m_typeStacks;

    protected override void Initialize()
    {
        m_typeStacks = new Dictionary<Type, IList<DecoratorRegistration>>();
        Context.Registering += AddRegistration;
        Context.Strategies.Add(new DecoratorBuildStrategy(m_typeStacks), UnityBuildStage.PreCreation);
    }

    private void AddRegistration(object _sender, RegisterEventArgs _e)
    {
        if (_e.TypeFrom == null || !_e.TypeFrom.IsInterface)
            return;

        GetStack(_e.TypeFrom)
            .Add(new DecoratorRegistration {Order = m_order++, Type = _e.TypeTo});
    }

    private IList<DecoratorRegistration> GetStack(Type _type)
    {
        if (!m_typeStacks.ContainsKey(_type))
            m_typeStacks.Add(_type, new List<DecoratorRegistration>());

        return m_typeStacks[_type];
    }
}

这样做是为每种类型使用一个列表,以存储相同目标类型的所有类型注册,以便在调用Resolve时使用此构建策略重新组合它:

internal class DecoratorBuildStrategy : BuilderStrategy
{
    private readonly Dictionary<Type, IList<DecoratorRegistration>> m_typeStacks;

    internal DecoratorBuildStrategy(Dictionary<Type, IList<DecoratorRegistration>> _typeStacks)
    {
        m_typeStacks = _typeStacks;
    }

    public override void PreBuildUp(IBuilderContext _context)
    {
        var key = _context.OriginalBuildKey;
        if (_context.GetOverriddenResolver(key.Type) != null)
            return;

        // Only interfaces can use decorators.
        if (!key.Type.IsInterface)
            return;

        // Gets the list of types required to build the 'decorated' instance.
        // The list is reversed so that the least dependent types are built first.
        var decoratorTypes = GetDecoratorTypes(key.Type).Reverse().ToList();
        if (!decoratorTypes.Any())
            return;

        object value = null;
        foreach (var type in decoratorTypes)
        {
            Type typeToBuild = type;
            if (typeToBuild.IsGenericTypeDefinition)
            {
                Type[] genericArgumentTypes = key.Type.GetGenericArguments();
                typeToBuild = typeToBuild.MakeGenericType(genericArgumentTypes);
            }

            value = _context.NewBuildUp(new NamedTypeBuildKey(typeToBuild, key.Name));

            // An Override is created so that in the next BuildUp the already 
            // built object gets used instead of doing the BuildUp again and 
            // entering an infinite loop
            _context.AddResolverOverrides(new DependencyOverride(key.Type, value));
        }

        _context.Existing = value;
        _context.BuildComplete = true;
    }

    private IEnumerable<Type> GetDecoratorTypes(Type _type)
    {
        var typeList = m_typeStacks.GetValueOrDefault(_type) ?? new List<DecoratorRegistration>(0);
        if (!_type.IsGenericType)
            return typeList.Select(_reg => _reg.Type);

        // If the type is a generic type, we need to get all open generic registrations
        // alongside the closed ones
        var openGenericList = m_typeStacks
                .GetValueOrDefault(_type.GetGenericTypeDefinition()) ?? 
                new List<DecoratorRegistration>(0);

        // The final result is an ordered concatenation of the closed and open registrations 
        // that should be used for the type
        return typeList
            .Concat(openGenericList)
            .OrderBy(_registration => _registration.Order)
            .Select(_reg => _reg.Type);
    }
}

这是使用DecoratorRegistration模型的地方。它只是一对类型/ int,表示注册的顺序。我创建它是为了能够正确混合开放和封闭的通用注册:

internal struct DecoratorRegistration
{
    public int Order { get; set; }
    public Type Type { get; set; }
}

这大部分都是奇迹。问题开始于我有一个实现两个接口的类,一个是装饰的,另一个不是。

这是我正在努力工作的当前测试案例:

private interface IAny<T> {}
private interface IAnotherInterface {}
private class Base<T> : IAnotherInterface, IAny<T> {}   
private class Decorator1<T> : IAny<T>
{
    internal readonly IAny<T> Decorated;

    public Decorator1(IAny<T> _decorated)
    {
        Decorated = _decorated;
    }
}

[TestMethod]
public void DecoratorExtensionDoesNotInterfereWithNormalRegistrations()
{
    // Arrange
    var container = new UnityContainer()
        .AddNewExtension<DecoratorExtension>()
        .RegisterType<Base<string>>(new ContainerControlledLifetimeManager())
        .RegisterType<IAny<string>, Decorator1<string>>()
        .RegisterType<IAny<string>, Base<string>>()
        .RegisterType<IAnotherInterface, Base<string>>();

    // Act
    var decorated = container.Resolve<IAny<string>>();
    var normal = container.Resolve<IAnotherInterface>();
    var anotherDecorated = container.Resolve<IAny<string>>();
    var anotherNormal = container.Resolve<IAnotherInterface>();

    // Assert
    Assert.IsInstanceOfType(normal, typeof (IAnotherInterface));
    Assert.IsInstanceOfType(decorated, typeof (Decorator1<string>));
    Assert.AreSame(normal, anotherNormal);
    Assert.AreSame(decorated, anotherDecorated);
}

这个测试应该让我的意图明确。我想要单例类,但第一次调用Resolve,IAnotherInterfaceIAny<string>会导致每次后续调用返回相同的内容。因此,我得到一个例外:

System.InvalidCastException: Unable to cast object of type 'Decorator1`1[System.String]' to type 'IAnotherInterface'.

在这一行:

    var normal = container.Resolve<IAnotherInterface>();

我不知道该怎么做。我不得不暂时禁用我们项目中的单身人士,以便这可以按预期工作。我想要的是Base<string>实例是一个sintleton,但当我请求一个IAny<string>时,它会创建一个新的实例,其中SAME基础被装饰。

这仍然使用.Net 4.0,所以我在这里坚持使用Unity 2.1(尽管在这种情况下应该不重要)。

2 个答案:

答案 0 :(得分:2)

自从我解决了这个问题已经有一段时间了,所以我认为复制Randi Levy从EntLib团队那里得到的答案会很好。

它基本上归结为我用来注册装饰器实例的构建键。使用我的代码,实例实际上是使用基类类型注册的,而我需要使用实际的装饰器类型注册它。

This post针对该问题提供了建议的解决方法,该方法在我们的最终工作得非常好。

答案 1 :(得分:1)

我不确定这是否是您正在寻找的,但我认为这在您的测试中的特定情况下可以解决问题:

container.RegisterType<IAny<string>, Base<string>>(
    new ContainerControlledLifetimeManager(), "Inner");

container.RegisterType<IAny<string>, Decorator1<string>>(
    new InjectionConstructor(
        new ResolvedParameter(typeof(IAny<string>), "Inner")));

container.Register<IAnotherInterface>(new InjectionFactory(
    c => c.Resolve<IAny<string>>("Inner")));

你不需要那个扩展名。