派生类中的Unity拦截 [英] Unity Interception in Derived Class

查看:60
本文介绍了派生类中的Unity拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用派生类时,策略注入不再起作用.

I've got a situation where policy injection no longer works when I'm using a derived class.

涉及的类如下(基本上是一个接口,一个抽象基类和一个实现类):

The classes involved look like this (basically an interface, an abstract base class, and an implementation class):

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()方法,也会发生这种情况.

If I remove the ElmtRepository base class, it works as expected. With the base class, the injection doesn't happen. No error messages, but no policies either. This happens even if I implement the Create() method in the derived class.

是否有一种方法可以使此类类层次结构与Unity策略注入配合使用?

Is there a way to make this sort of class hierarchy work with Unity policy injection?

谢谢, 吉姆

推荐答案

Unity通常不会出现这种类继承问题.但是,配置通用名称会带来无穷无尽的错误消息.我敢打赌,这是您的真正问题.但是,由于您没有发布错误消息(或AcctPgm类或AcctPgmAuthorizationHandler),所以我不确定.

This kind of class inheritance Unity usually does without a problem. However configuring generics is endless headaches with poor error messages. I bet that’s your real problem. However since you didn’t post your error message (or the AcctPgm class, or the AcctPgmAuthorizationHandler) I can’t be sure.

我将包含的类型更改为int,并使此版本的代码正常工作:

I changed the contained type to int and got this version of your code working:

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);
        }
    }
}

使用配置:

<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

这篇关于派生类中的Unity拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆