Lind.DDD.IoC依赖注入与面向方面的实现

时间:2022-02-16 17:00:23

回到目录

IoC是解耦的灵魂,很难想像一个框架中没有IoC会变成什么样子,Lind.DDD里的IoC是通过Unity实现的,由依赖注入(unity)和方法拦截组成(Interception),依赖注入可以通过事前定义好的实现方式去动态建立某个接口的实例,例如,在仓储接口IRepository里,你可以在配置文件中定义它由EF实现,也可以让它由Mongodb实现,而表现出来的结果就是数据的持久化方式的不同。

模块的结构

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANcAAACQCAIAAAAUQmk2AAAIX0lEQVR4nO2cu4sbVxSH9+9wsYXBBq9KFdsapQwirV0bgzt3ro0JAq/BIYVxFgRxWC+qgnDnByzGgoAguMk2SaqsK2+z9sY+4JCgFJOMtfdx5t4zjzN35vdxMKO79yXNx52R5vhuEADabNTR6WQyGZ2ljlFAZ6jewslkcufO1+9O3ucBCwFPBRYay16uYPYSFoJCTAsPDw9ju8hUY8Jp4eZZfJ2H1AGpc8bCxWKxtbUV24XMwgzerfW/wsIO89nCxWJx4cIFwcluxkLQYf6zMFcw6sSPihBYuD4HWNgTNuisglEn/ubNm7yFz589j7LQ8A8W9oQNQ8GoEz+bzXgL79//BhaCQja2trYCv67aHB4e8hZeuXKlKgthZIcp+3vh1atXeRF/+/V3p4U+9Z0voWC3KWuh/bDOYG9vj1kLAaDyFh4cHPAWZlQyV9BVaslmACAKWAj02RjurhAI3YCFCP1oi4W+tVp9YogGohUWZsI5M7TV54ZoIIQWXnsiHM+35vkytNU/IEQDIbTw419CEcmTiePL0Fb/gBANhNDC1Wp1+kkiIgXkIzJroYH6x8d8PupzSCjkFt56IRGxpIX56CEzbOxDhHMlQ27hcFciou+K7Hvux4weMsNmAhaWjFIWDndXuz+vTj+tLn8f2rByC50XaOcle70kPy5saLeKahLeT5+jlIXXnqxOP61uvYhoWMdauH52w+s4NRUc2GMFju5r28OQWyhQcKhqobH8FKrDt4KFFUap78ixCg5bsBZWVQILKwyhhX//I1FwWNpCg/WS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xUtp959rZnMrlrXwvVT6zuw28rqOA8KTeIn1jfq3dfal+VKRDf2r6/fF+799EPU6M5juzDKQgpITPQ5Z6xqUW/BGL2wbfeod19rxsLjk+NMxCwqsTBwoXK2Fddxqhz1FgTNO0a9+1ozV2QZm2cxCu2adgV7qbNfGpUL+2Ga5BWY9+Iculco7GsNgAGeIwN9YCHQBxYCfWAh0AcWAn1gIdAHFgJ9kGsN9EGuNdCnC7nWfLXwVkCL5HOt+b9uBiQf8MDaBkg+1zrwr/miyPccOxaohORzrfMSfrXzpbo4S5zJMr6xjBQb+06AeRfIqclIPtearKtt4FrIXKyd1ZjKhXWYtyC+VegSyedaU4yFdmHIIsqM5WwVcvVn5sO06irJ51oTa2GhK3ydwrGYVgILC//UVbqQa03WWffdcjG9MTWZsZiemZf8JGFhKMi15umhSWXAc+SK6ediVhJYCPSBhUAfWAj0gYVAH1gI9IGFQB/kWgN9kGsN9Eks15oqfdIV20Nvn7DVTWK51s5MhSapcFDYnJNYrrX6mYOFdZBerjWTHWMcGwfOQmc/vgwaCs7jCunH18r3LvgPJOncnCRzrclzabZPIVOHLKWc/TDNBa1iRy80KWSG7SfJXOt8DvmBvR44K/tOPNMqsGfngbOrwtGjfApcm1tOYrnW/BLorLZeKLCwcAKy9ZIZvbyF4c1bQsK51s5ycq06eR1fE6bQ2bO9QIb3Yxc6X/qmx3wm/EfUZpBrDfTBc2SgDywE+sBCoA8sBPrAQqAPLAT6wEKgD3KtgT7ItQb6JJZrzT83M2oarRJ6otU3Esu1prDMAL4haBuJ5VoTLOwiSeZa2wd8ropR32jF29mBjJX2k16udeBaGGIhM4pvLMhXB+nlWldlIYX9lw5nIVyslsRyralSC/ly8Z9ALInlWmf4bvt8d4qbZ7EL8xLfWLgvrBXkWgN98BwZ6AMLgT6wEOgDC4E+sBDo8y+7W00XXF/m4wAAAABJRU5ErkJggg==" alt="" />

服务定位器ServiceLocator

服务定位器可以帮助我们在不引用程序集的情况下,自动将它进行反射,这对于某些扩展注入的场合,非常有用,也是通过单例模式实现的。

    /// <summary>
/// Represents the Service Locator.
/// </summary>
public sealed class ServiceLocator : IServiceProvider
{
#region Private Fields
private readonly IUnityContainer _container;
#endregion #region Private Static Fields
private static readonly ServiceLocator instance = new ServiceLocator();
#endregion #region Ctor
/// <summary>
/// Initializes a new instance of ServiceLocator class.
/// </summary>
private ServiceLocator()
{
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); if (section == null)
{
var unityConfig = System.AppDomain.CurrentDomain.BaseDirectory + @"\IoC.config";
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = unityConfig };
var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
section = (UnityConfigurationSection)configuration.GetSection("unity");
} if (section == null)
throw new ArgumentException("请配置unity节点..."); _container = new UnityContainer(); #region 装载config中的类型
section.Configure(_container);
#endregion #region 注册动态类型
LoadDynamicType(_container);
#endregion }
#endregion #region Public Static Properties
/// <summary>
/// Gets the singleton instance of the ServiceLocator class.
/// </summary>
public static ServiceLocator Instance
{
get { return instance; }
} #endregion #region Private Methods
/// <summary>
/// 装载一批动态的类型
/// Author:zhangzhanling
/// Date:2015-04-03
/// </summary>
private void LoadDynamicType(IUnityContainer _container)
{
//unity动态类型注入,各个程序集用,分开,支持*通配符号
string unityDynamicAssembly = System.Configuration.ConfigurationManager.AppSettings["unityDynamicAssembly"];
//是否同时启动数据集缓存策略
string unityCachingDoing = System.Configuration.ConfigurationManager.AppSettings["unityCachingDoing"] ;
InjectionMember[] injectionMembers = new InjectionMember[] { };
if (unityCachingDoing == "")
{
injectionMembers = new InjectionMember[] { new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<CachingBehavior>() };
}
if (!string.IsNullOrWhiteSpace(unityDynamicAssembly))
{
Array.ForEach(unityDynamicAssembly.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), dllName =>
{
var baseDir = AppDomain.CurrentDomain.BaseDirectory;
if (System.Web.HttpContext.Current != null)
{
baseDir += "bin";
}
var files = Directory.GetFiles(baseDir, dllName);
var iTypes = new List<Type>();
foreach (var file in files)
{
var interfaceASM = Assembly.LoadFrom(Path.Combine(baseDir, file));
var types = from t in interfaceASM.GetTypes()
where !string.IsNullOrWhiteSpace(t.Namespace)
select t; foreach (var type in types)
{
if (type.GetInterfaces() != null && type.GetInterfaces().Any())
foreach (var father in type.GetInterfaces())
{
_container.RegisterType(father
, type
, injectionMembers);
}
}
}
}); } } private 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
}

Interception方法拦截

这是AOP面向方面编程里的一个概念,它在方法进行前或者后,对它进行拦截,并注入新的业务逻辑,这种方法一般是接口方法和虚方法,为了方便大家使用,大家抽象了一个基类

    /// <summary>
/// 拦截器抽象基类
/// 实现拦截器的项目需要继承此类,只引用Microsoft.Practices.Unity.Interception.dll程序集
/// </summary>
public abstract class InterceptionBase : IInterceptionBehavior
{
/// <summary>
/// 获取当前行为需要拦截的对象类型接口。
/// </summary>
/// <returns></returns>
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
} /// <summary>
///通过实现此方法来拦截调用并执行所需的拦截行为。
/// </summary>
/// <param name="input">调用拦截目标时的输入信息</param>
/// <param name="getNext">通过行为链来获取下一个拦截行为的委托</param>
/// <returns>从拦截目标获得的返回信息</returns>
public abstract IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext); /// <summary>
/// 获取一个<see cref="Boolean"/>值,该值表示当前拦截行为被调用时,是否真的需要执行拦截动作
/// </summary>
public bool WillExecute
{
get { return true; }
}
}

下面是一个异常拦截器的实现

 /// <summary>
/// 拦截器实例,具体拦截器可以自己去建立项目来实现,需要实现IInterceptionBehavior接口
/// 表示用于异常日志记录的拦截行为。
/// </summary>
public class ExceptionLoggingBehavior : InterceptionBase
{ /// <summary>
/// 通过实现此方法来拦截调用并执行所需的拦截行为。
/// </summary>
/// <param name="input">调用拦截目标时的输入信息。</param>
/// <param name="getNext">通过行为链来获取下一个拦截行为的委托。</param>
/// <returns>从拦截目标获得的返回信息。</returns>
public override IMethodReturn Invoke(
IMethodInvocation input,
GetNextInterceptionBehaviorDelegate getNext)
{
//方法执行前
var methodReturn = getNext().Invoke(input, getNext);//原方法被执行
//方法执行后
if (methodReturn.Exception != null)
{
Console.WriteLine(methodReturn.Exception.Message);
Logger.LoggerFactory.Instance.Logger_Error(methodReturn.Exception);
}
return methodReturn;
} }

下面是IOC所需要的配置

<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<!--BEGIN: Unity-->
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
<container>
<extension type="Interception" />
<!--被拦截的类型-->
<register type="Lind.DDD.Test.AOP,Lind.DDD.Test" mapTo="Lind.DDD.Test.AOP,Lind.DDD.Test">
<interceptor type="VirtualMethodInterceptor"/>
<!--InterfaceInterceptor,VirtualMethodInterceptor,TransparentProxyInterceptor,这种方法要求被拦截的类继承MarshalByRefObject-->
<interceptionBehavior type="Lind.DDD.IoC.Interception.ExceptionLoggingBehavior,Lind.DDD" />
<!--拦截行为-->
</register>
</container>
</unity> </configuration>

非常感谢各位的阅读!

回到目录