派生类中的统一拦截

时间:2011-09-01 17:11:28

标签: inheritance unity-container policy-injection

我遇到的情况是,当我使用派生类时,策略注入不再有效。

所涉及的类看起来像这样(基本上是接口,抽象基类和实现类):

public interface IRepository<T>
{
    void Create(T iItem);
}

public abstract class ElmtRepository<T> : IRepository<T>
{
    protected List<T> Items { get; set; }

    public ElmtRepository()
    {
        Items = new List<T>();
    }

    public void Create(T iItem)
    {
        Items.Add(iItem);
    }
}

public class AcctPgmRepository : ElmtRepository<AcctPgm>
{
}

配置如下所示:

  <container>
    <extension type="Interception"/>
    <register type="IRepository[AcctPgm]" mapTo="AcctPgmRepository">
      <interceptor type="InterfaceInterceptor"/>
      <interceptionBehavior type="PolicyInjectionBehavior"/>
    </register>
    <interception>
      <policy name="policy-create">
        <matchingRule name="create-rule1" type="TypeMatchingRule">
          <constructor>
            <param name="typeName">
              <value value="AcctPgmRepository"/>
            </param>
          </constructor>
        </matchingRule>
        <matchingRule name="create-rule2" type="MemberNameMatchingRule">
          <constructor>
            <param name="namesToMatch">
              <array type="string[]">
                <value value="Create"/>
              </array>
            </param>
          </constructor>
        </matchingRule>
        <callHandler name="create-handler1" type="AcctPgmAuthorizationHandler">
          <lifetime type="singleton"/>
          <constructor>
            <param name="allowedRoles">
              <array type="string[]">
                <value value="GroupController"/>
              </array>
            </param>
          </constructor>
        </callHandler>
      </policy>
    </interception>
  </container>

如果删除ElmtRepository基类,它将按预期工作。使用基类,注入不会发生。没有错误消息,但也没有策略。即使我在派生类中实现Create()方法,也会发生这种情况。

有没有办法让这种类层次结构与Unity策略注入一起工作?

谢谢, 吉姆

1 个答案:

答案 0 :(得分:2)

这种类继承Unity通常没有问题。但是,配置泛型是无穷无尽的令人头疼的错误消息。我打赌那是你真正的问题。但是,由于您没有发布错误消息(或AcctPgm类或AcctPgmAuthorizationHandler),我无法确定。

我将包含的类型更改为int,并使您的代码版本正常运行:

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace UnityTest
{
    public interface IRepository<T>
    {
        void Create(T iItem);
    }

    public abstract class ElmtRepository<T> : IRepository<T>
    {
        protected List<T> Items { get; set; }

        public ElmtRepository()
        {
            Items = new List<T>();
        }

        public void Create(T iItem)
        {
            System.Diagnostics.Debug.WriteLine("Creating...");
            Items.Add(iItem);
        }
    }

    public class AcctPgmRepository : ElmtRepository<int> { }

    public class LogHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            System.Diagnostics.Debug.WriteLine("Begin");
            IMethodReturn result = getNext().Invoke(input, getNext);
            System.Diagnostics.Debug.WriteLine("End");
            return result;
        }
        public int Order { get; set; }
    }

    [TestClass]
    public class InheritenceGenericsTests
    {
        [TestMethod]
        public void CreateTest()
        {
            IUnityContainer container = new UnityContainer().LoadConfiguration("Inheritence");
            IRepository<int> r2 = container.Resolve<IRepository<int>>();
            Assert.IsNotNull(r2);
            r2.Create(2);
        }
    }
}

with config:

          

    

<alias alias="IRepository" type="UnityTest.IRepository`1, UnityTest"/>
<alias alias="IRepositoryClosed" type="UnityTest.IRepository`1[System.Int32], UnityTest"/>
<alias alias="AcctPgmRepository" type="UnityTest.AcctPgmRepository, UnityTest"/>

<container name="Inheritence">
  <extension type="Interception"/>
  <!-- register either type="IRepositoryClosed" or type="IRepository" -->
  <register type="IRepositoryClosed" mapTo="AcctPgmRepository">
    <interceptor type="InterfaceInterceptor"/>
    <policyInjection/>
  </register>
  <interception>
    <policy name="policy-create">
      <matchingRule name="create-rule2" type="MemberNameMatchingRule">
        <constructor>
          <param name="namesToMatch">
            <array type="string[]">
              <value value="Create"/>
            </array>
          </param>
        </constructor>
      </matchingRule>
      <callHandler name="create-handler1" type="UnityTest.LogHandler, UnityTest"></callHandler>
    </policy>
  </interception>
</container>

提供输出:

Begin
Creating...
End