使用城堡创建带有第三方 IoC 的 AOP



我正在尝试使用面向方面的编程。 问题是内部内置的自定义 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");
            }
        }
    }
}

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

  1. 使用接受运行时类型的种姓代理生成器的重载。
  2. 为了执行拦截器,您需要拦截类实现中的虚拟方法。

最新更新