基本クラスとそこから派生したクラスを持つアプリケーションがあり、各実装クラスには独自のインターフェイスがあります。基本クラスの派生型の例外処理に 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 で可能で、どのように可能ですか? モデルに多少の修正が必要な場合は、軽微なものである限り、喜んで対応します。