0

基本クラスとそこから派生したクラスを持つアプリケーションがあり、各実装クラスには独自のインターフェイスがあります。基本クラスの派生型の例外処理に Unity のインターセプトを使用したいと考えています。

私はインターセプトが初めてなので、すべての癖を知りません。私の知る限り、各実装解決でインターセプトを登録する必要があります。ポイントは、すべての実装に基本クラスがあることです。そのため、冗長性をスキップして、各実装クラスで起動する基本クラスのみにインターセプトを設定できると考えました。

これは私の設定です:

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

基本サービスから継承するさらに多くのサービス クラスを用意しますが、これらはすべて同じインターセプト動作を行うため、時間を大幅に節約できると考えました。

これは Unity で可能で、どのように可能ですか? モデルに多少の修正が必要な場合は、軽微なものである限り、喜んで対応します。

4

1 に答える 1

0

タイプに手動で動作を適用するのではなく、Unity でのポリシー インジェクションを検討することをお勧めします。ポリシーでは、次のことを行う必要があります。

  1. ICallHandler を実装するクラスを作成します (基本的には IInterceptionBehavior を削減します) - これが例外ハンドラーの動作になります。
  2. 「一致するルール」を持つポリシーを作成します。この場合、BaseService などを実装する登録済みの型に対して CallHandler を使用するポリシーです。
  3. すべてのサービスを Unity に登録する必要がありますが、今度は Interceptor と InterceptionBehavior を渡します。多くのサービスがある場合は、Unity Automapperのようなものを検討することをお勧めします。これにより、登録とインターセプト動作をいじる必要の両方が簡素化されます。
于 2013-04-30T21:25:13.900 に答える