AOP

using System;

namespace MyAop
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //vs调试时的监视窗口/即时窗口/输出窗口

            //什么是AOP?
            //把公共逻辑抽出来,通过配置实现公共功能的动态添加
            //举个例子,之前公司每个项目组都有美工测试,现在我把所有美工测试都抽出来成立设计部门和测试部门,哪个项目组需要啥,我再给协调人  这种模式就是AOP
            //POP   OOP   AOP(允许开发者动态的改变OO模型)(Aspect Oriented Programming)

            //AOP 面向切面 有以下好处:
            //  1.关注核心业务逻辑,权限/异常/日志/事务/缓存写好后 等可以交给AOP配置,其他人不用关心实现细节
            //  2.功能动态扩展,公共功能集中管理,规范化,实现代码复用
            //其实代理模式和装饰器模式也算实现AOP,但是不能动态配置,所以是静态实现

            //动态AOP:
            //  1.Remoting 
            //  2.Castle(Emit)
            //  3.静态织入 ---PostSharp(收费)
            //  4.unity

            CastleProxyAop.Show();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace MyAop
{
    public class RealProxyAop
    {
        //using System.Runtime.Remoting.Messaging;
        //using System.Runtime.Remoting.Proxies;
        //引用上面两个,而且.NetCore是没有的 实现不了
        //真实代理和透明代理的方法都是固定写法

        //public static void Show()
        //{
        //    UserProcessor processor = new MyAop.RealProxyAop.UserProcessor();
        //    User user = new User() { Name = "xiaogao", Password = "12345" };
        //    processor.RegUser(user);

        //    UserProcessor userProcessor = TransparentProxy.Create<UserProcessor>();
        //    userProcessor.RegUser(user);

        //}

        //public class MyRealProxy<T>:RealProxy
        //{
        //    private T tTarget;

        //    public MyRealProxy(T target) : base(typeof(T))
        //    {
        //        this.tTarget = target;
        //    }

        //    public override IMessage Invoke(IMessage msg)
        //    {
        //        BeforeProceede(msg);

        //        IMethodCallMessage callMessage = (IMethodCallMessage)msg;
        //        object returnValue = callMessage.MethodBase.Invoke(this.tTarget, callMessage.Args);

        //        AfterProceede(msg);

        //        return new ReturnMessage(returnValue, new object[0], 0, null, callMessage);
        //    }

        //    public void BeforeProceede(IMessage msg)
        //    {
        //        Console.WriteLine("方法执行前");
        //    }

        //    public void AfterProceede(IMessage msg)
        //    {
        //        Console.WriteLine("方法执行后");
        //    }
        //}

        //public static class TransparentProxy
        //{
        //    public static T Create<T>()
        //    {
        //        T instance = Activator.CreateInstance<T>();
        //        MyRealProxy<T> realProxy = new MyRealProxy<T>(instance);
        //        T transparentProxy = (T)realProxy.GetTransparentProxy();
        //        return transparentProxy;
        //    }
        //}


        public interface IUserProcessor
        {
            void RegUser(User user);
        }

        //必须继承MarshalByRefObject,否则无法生成,实现不了Aop
        public class UserProcessor : MarshalByRefObject, IUserProcessor
        {
            public void RegUser(User user)
            {
                Console.WriteLine($"用户已注册。Name:{user.Name},Password:{user.Password}");
            }
        }

        public class User
        {
            public string Name { set; get; }
            public string Password { set; get; }
        }
    }


}
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Text;

namespace MyAop
{
    public class CastleProxyAop
    {
        public static void Show()
        {
            User user = new User() { Name = "xiaogao", Password = "12345" };
            ProxyGenerator generator = new ProxyGenerator();
            MyInterceptor interceptor = new MyInterceptor();

            UserProcessor userProcessor = generator.CreateClassProxy<UserProcessor>(interceptor);
            userProcessor.RegUser(user);
        }

        public class MyInterceptor : IInterceptor
        {
            public void Intercept(IInvocation invocation)
            {
                PreProceed(invocation);
                invocation.Proceed();
                PreProceed(invocation);
            }

            public void PreProceed(IInvocation invocation)
            {
                Console.WriteLine("方法执行前");
            }
            public void PostProceed(IInvocation invocation)
            {
                Console.WriteLine("方法执行后");
            }
        }


        public interface IUserProcessor
        {
            void RegUser(User user);
        }

        public class UserProcessor : IUserProcessor
        {
            //必须是虚方法,否则无法生成,实现不了Aop
            public virtual void RegUser(User user)
            {
                Console.WriteLine($"用户已注册。Name:{user.Name},Password:{user.Password}");
            }
        }

        public class User
        {
            public string Name { set; get; }
            public string Password { set; get; }
        }
    }
}

下面是.framework的,没找到.netcore的unity资料

using MyAop.UnityWay;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAop
{
    class Program
    {
        static void Main(string[] args)
        {
            //RealProxyAop.Show();
            UnityConfigAop.Show();
            Console.ReadKey();
        }
    }


}
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

//需要添加这几个包
//添加Unity
//添加Unity.Configuration
//添加Unity.Interception
//添加Unity.Interception.Configuration    //这个包不添加虽然编译能过,运行时会报错configuration会为null

namespace MyAop.UnityWay
{
    public class UnityConfigAop
    {
        public static void Show()
        {
            //AppDomain.CurrentDomain.BaseDirectory 的有关问题:
            //  控制台的路径在debug
            //  但是Asp.net项目地址是bin的父目录

            //AOP就是在配置设置用哪个类实现接口

            //执行顺序的问题:
            //  配置的循序就是调用的顺序,但是方法中可以中断执行后面的模块,也可以等待后面的模块完毕再执行自己的动作
            
            //方法的选择性进行AOP?
            //  用特性标记接口的方法,自己写特性,自己在Behavior中判断特性,如果有这个特性,跳过(当然可以执行一些自己的动作)


            User user = new User() { Name = "xiaogao", Password = "1234512345" };
            #region 固定写法,不用关心
            //声明容器
            IUnityContainer container = new UnityContainer();//

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();//
            fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"CfgFiles\Unity.config");
            Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
            //这个是配置中容器的名字<container>标签
            var ss = configSection.Configure(container, "aopContainer");
            #endregion
            //配置中已经写好了用哪个类来实现IUserProcessor接口
            IUserProcessor processor = container.Resolve<IUserProcessor>();
            try { processor.RegUser(user); }
            catch(Exception e) { Console.WriteLine(e.ToString()); };
           
            processor.GetUser(user);
        }
    }

    public interface IUserProcessor
    {
        [Skip("LogAfterBehavior")]
        void RegUser(User user);

        User GetUser(User user);
    }

    public class UserProcessor : IUserProcessor
    {
        public void RegUser(User user)
        {
            Console.WriteLine($"用户已注册。Name:{user.Name},Password:{user.Password}");
        }

        public User GetUser(User user)
        {
            return user;
        }
    }

    public class User
    {
        public string Name { set; get; }
        public string Password { set; get; }
    }
}

 这个是新建的配置Unity.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity"
             type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
                               Unity.Configuration" />
  </configSections>
  <unity>
    <!--type前面是完整类型名称,后面是Dll名称-->
    <sectionExtension
      type ="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
      Unity.Interception.Configuration"/>
    <containers>
      <!--可以添加多个container来实现多个接口的AOP-->
      <!--容器名称,代码中是要用这个名称的-->
      <container name ="aopContainer">
        <extension type ="Interception"/> <!--前面的写法都是固定的-->
        <!--type前面是接口名称,后面是dll名称;mapTo是要映射的类的名称,后面是dll名称-->
        <register type ="MyAop.UnityWay.IUserProcessor,MyAop" mapTo="MyAop.UnityWay.UserProcessor,MyAop">
          <!--指定扩展类型是基于接口的扩展(还有基于方法的、基于类的)-->
          <interceptor type ="InterfaceInterceptor"/>
          <interceptionBehavior type="MyAop.UnityWay.LogBeforeBehavior,MyAop"/>
          <interceptionBehavior type="MyAop.UnityWay.ParameterCheckBehavior,MyAop"/>
          <interceptionBehavior type="MyAop.UnityWay.CachingBehavior,MyAop"/>
          <interceptionBehavior type="MyAop.UnityWay.ExceptionLoggingBehavior,MyAop"/>
          <interceptionBehavior type="MyAop.UnityWay.LogAfterBehavior,MyAop"/>
          <interceptionBehavior type="MyAop.UnityWay.MonitorBehavior,MyAop"/>
        </register>
      </container>
    </containers>
  </unity>


</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace MyAop.UnityWay
{
    public class LogBeforeBehavior : IInterceptionBehavior
    {
        public bool WillExecute
        {
            get
            {
                return true;
            }
        }

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

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            //扩展的逻辑全都写在这
            //input参数包含映射方法的所有信息,包括参数,返回值,接口,接口要映射的类
            //那么可以用反射获取更多信息

            Console.WriteLine("LogBeforeBehavior");
            foreach(var item in input.Inputs)
            {
                Console.WriteLine(item.ToString());
            }
            return getNext().Invoke(input, getNext);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace MyAop.UnityWay
{
    public class ParameterCheckBehavior : IInterceptionBehavior
    {
        public bool WillExecute
        {
            get
            {
                return true;
            }
        }

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

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("ParameterCheckBehavior");
            User user = input.Inputs[0] as User;
            if (user.Password.Length < 10)
            {
                //也可以直接抛出异常
                return input.CreateExceptionMethodReturn(new Exception("密码长度不能小于10位"));
            }
            else
            {
                Console.WriteLine("参数正确");
                return getNext().Invoke(input, getNext);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace MyAop.UnityWay
{
    public class CachingBehavior : IInterceptionBehavior
    {
        public bool WillExecute
        {
            get
            {
                return true;
            }
        }

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

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("CachingBehavior");
            if (input.MethodBase.Name.Equals("GetUser"))
                return input.CreateMethodReturn(new User() { Name = "小高", Password = "Eleven" });
            return getNext().Invoke(input, getNext);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace MyAop.UnityWay
{
    public class ExceptionLoggingBehavior : IInterceptionBehavior
    {
        public bool WillExecute
        {
            get
            {
                return true;
            }
        }

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

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("ExceptionLoggingBehavior");
            //先后面其他的方法,执行完了看返回是否有异常,正常异常处理应该放在第一步
            IMethodReturn methodReturn = getNext()(input, getNext);
            if (methodReturn.Exception == null)
            {
                Console.WriteLine("无异常");
            }
            else
            {
                Console.WriteLine($"异常:{methodReturn.Exception.Message}");
            }
            return methodReturn;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace MyAop.UnityWay
{
    public class LogAfterBehavior : IInterceptionBehavior
    {
        public bool WillExecute
        {
            get
            {
                return true;
            }
        }

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

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("LogAfterBehavior");

            if (input.MethodBase.IsDefined(typeof(SkipAttribute), true))
            {
                SkipAttribute attribute = (SkipAttribute)input.MethodBase.GetCustomAttributes(typeof(SkipAttribute), true)
                    .Where(t => t.GetType().Name.Equals("SkipAttribute")).First();
                Console.WriteLine(attribute.bahaviorName);
                return getNext()(input, getNext);
            }

            foreach (var item in input.Inputs)
            {
                //反射获取更多信息
                Console.WriteLine(item.ToString());
            }
            IMethodReturn methodReturn = getNext()(input, getNext);
            Console.WriteLine("LogAfterBehavior" + methodReturn.ReturnValue);
            return methodReturn;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;

namespace MyAop.UnityWay
{
    public class MonitorBehavior : IInterceptionBehavior
    {
        public bool WillExecute
        {
            get
            {
                return true;
            }
        }

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

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine(this.GetType().Name);
            string methodName = input.MethodBase.Name;
            Stopwatch watch = new Stopwatch();
            watch.Start();
            IMethodReturn methodReturn = getNext().Invoke(input, getNext);
            watch.Stop();
            Console.WriteLine($"{this.GetType().Name}类的{methodName}方法用时{watch.ElapsedMilliseconds}ms");
            return methodReturn;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAop.UnityWay
{
    [AttributeUsage(AttributeTargets.Method)]
    public class SkipAttribute : Attribute
    {
        public string bahaviorName { set; get; }
        public SkipAttribute(string bahaviorName)
        {
            this.bahaviorName = bahaviorName;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值