AOP--面向切面程式設計

你好呀嗯嗯發表於2024-09-03

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);
        }

相關文章