Microsoft Unity基类拦截 [英] Microsoft Unity base class interception

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

问题描述

我有一个应用程序,它具有基类和从基类派生的类,每个实现类都有其自己的接口.我想将Unity的侦听功能用于对基类的派生类型进行异常处理.

I have an application that has a base class and derived classes from it with each implementation class having it's own interface. I would like to use Unity's interception for exception handling on derived types of a base class.

我是拦截的新手,所以我不知道所有的怪癖.据我所知,我必须在每个实现解析中注册拦截.关键是我所有的实现都有一个基类,因此我认为我可以跳过冗余并仅在基类上设置拦截,这将在每个实现类上触发.

I am new to interception so I don't know all the quirks. As far as I know, I have to register interception with each implementation resolve. The point is that all of my implementations have a base class, so I thought that I could skip the redundancy and set the interception on base class only, which would fire on each implementation class.

这是我的设置:

public class NotificationViewModel
{
   // some properties
}

public class CompanyViewModel : NotificationViewmodel
{
   // some properties
}

public class BaseService
{
}

public interface ICompanyService
{
   public NotificationViewModel Test();
}

public class CompanyService : BaseService, ICompanyService
{
   public CompanyViewModel Test()
   {
      // call exception
   }
}

public class TestUnityContainer : UnityContainer
{
   public IUnityContainer RegisterComponents()
   {
      this
         .AddNewExtension<Interception>()
         .RegisterType<ICompanyService, CompanyService>(
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<TestInterceptionBehavior>());

      return this;
    }
}

public class TestInterceptionBehavior : IInterceptionBehavior
{
   public IEnumerable<Type> GetRequiredInterfaces()
   {
      return new[] { typeof( INotifyPropertyChanged ) };
   }

   public IMethodReturn Invoke( IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext )
   {
      IMethodReturn result = getNext()( input, getNext );

      if( result.Exception != null && result.Exception is TestException )
      {             
         object obj = Activator.CreateInstance( ( ( System.Reflection.MethodInfo )input.MethodBase ).ReturnType );
         NotificationViewModel not = ( NotificationViewModel )obj;
         // do something with view model
         result.ReturnValue = obj;
         result.Exception = null;
      }

      return result;
   }

   public bool WillExecute
   {
      get { return true; }
   }
}

这很好,但是我想在TestUnityContainer

public class TestUnityContainer : UnityContainer
{
   public IUnityContainer RegisterComponents()
   {
      this
         .AddNewExtension<Interception>()
         .RegisterType<BaseService>(
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<TestInterceptionBehavior>() );
         .RegisterType<ICompanyService, CompanyService>();

      return this;
    }
}

我将有更多的服务类从基本服务继承,我认为这将为我节省很多时间,因为它们都具有相同的拦截行为.

I will have many more service classes inheriting from base service and I thought this would save me a lot of time because they all have the same interception behavior.

Unity有可能吗?如何?如果需要对模型进行一些较小的校正,只要它们较小,我就会向他们敞开大门.

Is this possible with Unity and how? If some minor corrections to the model are necessary, I am open for them, as long as they are minor.

推荐答案

我建议您查看Unity中的策略注入,而不是手动对类型应用行为.使用政策,您必须:-

I would suggest you look at Policy Injection in Unity rather than manually applying behaviours on types. With policies you must:-

  1. 创建一个实现ICallHandler的类(基本上是减少的IInterceptionBehavior)-这将是您的异常处理程序行为.
  2. 创建具有匹配规则"的策略-在您的情况下,该策略将CallHandler用于实现BaseService或类似功能的所有已注册类型.
  3. 您仍然必须将所有服务注册到Unity,但现在需要传递Interceptor和InterceptionBehavior.如果您有很多服务,建议您查看类似我的 Unity Automapper 之类的东西既要注册又要弄乱拦截行为.
  1. Create an class that implements ICallHandler (basically a cut down IInterceptionBehavior) - this would be your exception handler behavior.
  2. Create a policy that has "matching rules" - in your case, a policy that uses your CallHandler for any registered types that implement BaseService or similar.
  3. You will still have to register all your services into Unity, but now pass in the Interceptor and InterceptionBehavior. If you have many services, I would suggest looking at something like my Unity Automapper which will simplify both registration and having to mess around with interception behaviors.

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

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