當前位置:
首頁 >
利用Unity实现AOP
發布時間:2025/3/15
21
豆豆
生活随笔
收集整理的這篇文章主要介紹了
利用Unity实现AOP
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
.NET程序中,可以利用Unity來實現AOP,用來進行日志、緩存或權限的處理。這里我們來寫一個簡單的程序,讓其實現簡單的AOP功能。
1.使用NuGet,在項目中獲取Microsoft.Practices.Unity。
2.新建一個ITalk類及其實現
public interface ITalk{string Speak(string msg);}public class Talk : ITalk{public string Speak(string msg){Console.WriteLine(msg);return msg;}}3.再進一個ServiceLocator類,用來實現接口的依賴反轉
using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Reflection;namespace AopDemo {/// <summary>/// Represents the Service Locator./// </summary>public sealed class ServiceLocator : IServiceProvider{#region Private Fieldsprivate readonly IUnityContainer container;#endregion#region Private Static Fieldsprivate static readonly ServiceLocator instance = new ServiceLocator();#endregion#region Ctor/// <summary>/// Initializes a new instance of <c>ServiceLocator</c> class./// </summary>private ServiceLocator(){UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");container = new UnityContainer();section.Configure(container);}#endregion#region Public Static Properties/// <summary>/// Gets the singleton instance of the <c>ServiceLocator</c> class./// </summary>public static ServiceLocator Instance{get { return instance; }}#endregion#region Private Methodsprivate IEnumerable<ParameterOverride> GetParameterOverrides(object overridedArguments){List<ParameterOverride> overrides = new List<ParameterOverride>();Type argumentsType = overridedArguments.GetType();argumentsType.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList().ForEach(property =>{var propertyValue = property.GetValue(overridedArguments, null);var propertyName = property.Name;overrides.Add(new ParameterOverride(propertyName, propertyValue));});return overrides;}#endregion#region Public Methods/// <summary>/// Gets the service instance with the given type./// </summary>/// <typeparam name="T">The type of the service.</typeparam>/// <returns>The service instance.</returns>public T GetService<T>(){return container.Resolve<T>();}/// <summary>/// Gets the service instance with the given type by using the overrided arguments./// </summary>/// <typeparam name="T">The type of the service.</typeparam>/// <param name="overridedArguments">The overrided arguments.</param>/// <returns>The service instance.</returns>public T GetService<T>(object overridedArguments){var overrides = GetParameterOverrides(overridedArguments);return container.Resolve<T>(overrides.ToArray());}/// <summary>/// Gets the service instance with the given type by using the overrided arguments./// </summary>/// <param name="serviceType">The type of the service.</param>/// <param name="overridedArguments">The overrided arguments.</param>/// <returns>The service instance.</returns>public object GetService(Type serviceType, object overridedArguments){var overrides = GetParameterOverrides(overridedArguments);return container.Resolve(serviceType, overrides.ToArray());}#endregion#region IServiceProvider Members/// <summary>/// Gets the service instance with the given type./// </summary>/// <param name="serviceType">The type of the service.</param>/// <returns>The service instance.</returns>public object GetService(Type serviceType){return container.Resolve(serviceType);}#endregion} } View Code4.接下來是錯誤和緩存處理的類。我們這邊只是簡單的在控制臺輸出一句話,證明代碼有執行。
ExceptionLoggingBehavior.cs
public class ExceptionLoggingBehavior : IInterceptionBehavior{public IEnumerable<Type> GetRequiredInterfaces(){return Type.EmptyTypes;}public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext){Console.WriteLine("ExceptionLoggingBehavior");return getNext().Invoke(input, getNext);}public bool WillExecute{get { return true; }}} View CodeCachingBehavior.cs
public class CachingBehavior : IInterceptionBehavior{public IEnumerable<Type> GetRequiredInterfaces(){return Type.EmptyTypes;}public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext){Console.WriteLine("CachingBehavior");return getNext().Invoke(input, getNext);}public bool WillExecute{get { return true; }}} View Code5.配置App.Config文件
<configuration><configSections><section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/></configSections><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup><!--BEGIN: Unity--><unity xmlns="http://schemas.microsoft.com/practices/2010/unity"><sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/><container><extension type="Interception"/><!--Cache Provider--><register type="AopDemo.ITalk, AopDemo" mapTo="AopDemo.Talk, AopDemo"><interceptor type="InterfaceInterceptor"/><interceptionBehavior type="AopDemo.InterceptionBehaviors.CachingBehavior, AopDemo"/><interceptionBehavior type="AopDemo.InterceptionBehaviors.ExceptionLoggingBehavior, AopDemo"/></register></container></unity><!--END: Unity--> </configuration>6.調用
static void Main(string[] args){ITalk talk = ServiceLocator.Instance.GetService<ITalk>();talk.Speak("Hello");}7.結果
可以看到在打印出Hello前,代碼先執行到了緩存與錯誤處理的方法。
PS:如果IOC的時候報類似錯誤
Exception is: InvalidOperationException - The type OAManageClient has multiple constructors of length 2. Unable to disambiguate.則配置文件應該增加<constructor></constructor>
<register type="AopDemo.ITalk, AopDemo" mapTo="AopDemo.Talk, AopDemo"><interceptor type="InterfaceInterceptor"/><interceptionBehavior type="AopDemo.InterceptionBehaviors.CachingBehavior, AopDemo"/><interceptionBehavior type="AopDemo.InterceptionBehaviors.ExceptionLoggingBehavior, AopDemo"/> <constructor></constructor></register>?
轉載于:https://www.cnblogs.com/Gyoung/archive/2013/06/06/3123022.html
總結
以上是生活随笔為你收集整理的利用Unity实现AOP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [041] 微信公众帐号开发教程第17篇
- 下一篇: Delphi中的基础数据类型