使用Castle通过第三方IoC创建AOP

问题描述 投票:0回答:1

我正在尝试使用面向方面的编程。问题是内置的自定义IoC不支持这种类型的编程。我把问题分解成最重要的部分。我正在使用城堡来实现AOP。问题在代码注释中描述。

我不认为我能做到这一点,因为泛型是打算在编译时知道的。但是,我希望社区能够超越我。

更新2 - 更新到工作代码(这要归功于@vasiloreshenski)

using Castle.DynamicProxy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace Tests.Component.CastleTests
{
    [TestClass]
    public class GenericTest
    {
        [TestMethod]
        public void TestGeneric()
        {
            IMyPoco mypoco = ContrivedCustomIoc.Create<IMyPoco>();
            mypoco.DoWorkNoLogging();
            mypoco.DoWork();
            //Assert.IsTrue(typeof(MyPoco) == mypoco.GetType());
        }
    }

    public class ContrivedCustomIoc
    {
        private static Dictionary<string, string> mappings = new Dictionary<string, string>();

        static ContrivedCustomIoc()
        {
            //This comes from XML file
            mappings.Add("Tests.Component.CastleTests.IMyPoco", "Tests.Component.CastleTests.MyPoco");
        }

        public static T Create<T>() where T : class
        {
            string contractType = typeof(T).FullName;
            Type thisTypeInterface = Type.GetType(contractType);
            Type thisTypeConcrete = Type.GetType(mappings[contractType]);

            //Things work up until this point just fine
            //return (T)Activator.CreateInstance(thisTypeConcrete);


            var generator = new Castle.DynamicProxy.ProxyGenerator();
            //ERROR.  Class to proxy must be a class.
            //This is because T is an interface
            //Is it possible to use Castle with this custom IoC?  I want to avoid replacing the entire IoC 
            //I'd like to simply get an aspect oriented programming pattern in place
            //return generator.CreateClassProxy<T>(new MyLoggingInterceptor());

            object result = generator.CreateClassProxy(thisTypeConcrete, ProxyGenerationOptions.Default,
                new IInterceptor[1] { new MyLoggingInterceptor() });

            return (T) result;
        }
    }

    public interface IMyPoco
    {
        void DoWorkNoLogging();
        void DoWork();
    }

    public class MyPoco : IMyPoco
    {
        public void DoWorkNoLogging()
        {
            Console.Write(("Work bein done without logging"));
        }

        public virtual void DoWork()
        {
            Console.WriteLine("Work bein done!");
        }
    }

    public class MyLoggingInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            try
            {
                Console.WriteLine("Interceptor starting");
                invocation.Proceed();
                Console.WriteLine("Interceptor ending");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                Console.WriteLine("Exiting from interceptor");
            }
        }
    }
}
c# castle-dynamicproxy
1个回答
2
投票

注意:这是导致成功拦截器调用的注释的摘要。

  1. 使用接受运行时类型的caste proxy generator的重载。
  2. 为了执行拦截器,您需要在类实现中拦截一个虚方法。
© www.soinside.com 2019 - 2024. All rights reserved.