AOP--面向切面编程

1.使用代理模式实现AOP

复制代码
      public class UserProcessor : IUserProcessor
        {
            public void RegUser(User user)
            {
                Console.WriteLine("用户已注册。Name:{0},Password :{1}", user.Name, user.Password);
            }
        }
        /// <summary>
        /// 代理模式实现AOP
        /// </summary>
        public class ProxyUserProcessor : IUserProcessor
        {
            private IUserProcessor _UserProcessor = new UserProcessor();

            public void RegUser(User user)
            {
                BeforeProceed(user);
                this._UserProcessor.RegUser(user);
                AfterProceed(user);
            }

            private void AfterProceed(User user)
            {
                Console.WriteLine("方法执行后");
            }

            private void BeforeProceed(User user)
            {
                Console.WriteLine("方法执行前");
            }
        }
复制代码

2.装饰器模式实现AOP

复制代码
        public interface IUserProcessor
        {
            void RegUser(User user);
        }

        public class UserProcessor : IUserProcessor
        {
            public void RegUser(User user)
            {
                Console.WriteLine("用户已注册。Name:{0},Password :{1}", user.Name, user.Password);
            }
        }
        /// <summary>
        /// 装饰器模式实现AOP
        /// </summary>
        public class UserProcessorDecorator : IUserProcessor
        {
            private IUserProcessor _UserProcessor { get; set; }
            public UserProcessorDecorator(IUserProcessor userProcessor)
            {
                _UserProcessor = userProcessor;
            }

            public void RegUser(User user)
            {
                BeforeProceed(user);
                this._UserProcessor.RegUser(user);
                AfterProceed(user);
            }

            private void AfterProceed(User user)
            {
                Console.WriteLine("方法执行后");
            }

            private void BeforeProceed(User user)
            {
                Console.WriteLine("方法执行前");
            }
        }
复制代码

3.Unity实现AOP

调用如下:需读配置文件

复制代码
                IUnityContainer container = new UnityContainer();
                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = Path.Combine(@"D:\C#高级学习\AOP\MyAOP\MyAOP\CfgFiles\Unity.Config");
                Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection configSelction = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                configSelction.Configure(container, "aopContainer");

                IUserProcessor processor = container.Resolve<IUserProcessor>();
                processor.RegUser(user);
                processor.GetUser(user);
                processor.GetUser(user);
复制代码

配置文件如下:

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Unity.Configuration"/>
  </configSections>
  <unity>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Unity.Interception.Configuration"/>
    <containers>
      <container name="aopContainer">
        <extension type="Interception"/>
        <register type="MyAOP.UnityWay.IUserProcessor, MyAOP" mapTo="MyAOP.UnityWay.UserProcessor, MyAOP">
          <interceptor type="InterfaceInterceptor"/>
          <interceptionBehavior type="MyAOP.UnityWay.ExceptionLoggingBehavior, MyAOP"/>
          <interceptionBehavior type="MyAOP.UnityWay.CachingBehavior, MyAOP"/>
          <interceptionBehavior type="MyAOP.UnityWay.LogBeforeBehavior, MyAOP"/>
          <interceptionBehavior type="MyAOP.UnityWay.ParameterCheckBehavior, MyAOP"/>
          <interceptionBehavior type="MyAOP.UnityWay.LogAfterBehavior, MyAOP"/>
        </register>
      </container>
    </containers>
  </unity>
</configuration>
复制代码

具体的类如下:

如遇到getNext().Invoke,就会执行后续的步骤,后续的步骤是指配置文件中下一个类。

复制代码
    public class LogBeforeBehavior:IInterceptionBehavior
    {

        public bool WillExecute
        {
            get { return true; }
        }
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }
        public IMethodReturn Invoke(IMethodInvocation input,
            GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("logbeforbehavior");
            foreach (var item in input.Inputs)//input.MethodBase
            {
                Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
            }
            return getNext().Invoke(input, getNext);
        }
复制代码

 

posted @   你好呀嗯嗯  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
点击右上角即可分享
微信分享提示