3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Prism 6.3でAOPを使う方法

Posted at

問題

Prism 6.3 + Unityを使用していて、AOPをやろうと思い、最新のUnity.Interceptionパッケージ 5.5.3をNugetから取得。しかし、AOPのクラスが参照できない。

解決方法

Prism6.3で参照しているUnityが古いことが判明。Interceptionパッケージ 4.0.1をNugetから取得することで解決。
どうやらPrism6.3は4系のUnityと依存関係がある様子。
さらには4系と5系で破壊的変更が起きているようで名前空間が違う。

4系

Microsoft.Practices.Unity.InterceptionExtension

5系

Unity.Interception

サンプルコード。PrismのBootStrapper。jobsRepositoryのメソッド呼び出し時にLogBehaviorを呼ぶように設定。サンプルコードなので、CreateShellでjobsRepositoryを呼び出していますが、実際にはこんなところで呼び出しません。

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;

    class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {

            Container.AddNewExtension<Interception>();
            Container.RegisterType<jobsRepository>(new Interceptor<VirtualMethodInterceptor>()
                                         , new InterceptionBehavior<LogBehavior>());

            var jobsRepo = Container.Resolve<jobsRepository>();
            jobsRepo.GetJobs();
            return Container.Resolve<MainWindow>();
        }
     }
using Microsoft.Practices.Unity.InterceptionExtension;

    public class LogBehavior : IInterceptionBehavior
    {
        public bool WillExecute => true;

        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Enumerable.Empty<Type>();
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Debug.WriteLine("開始: " + input.MethodBase.Name);

            var result = getNext()(input, getNext);

            Console.WriteLine($"終了:{input.MethodBase.Name}");

            return result;
        }
    }
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?