using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Coralcode.Framework.Extensions;
using Coralcode.Framework.Reflection;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
namespace Coralcode.Framework.Aspect.Unity
{
/// <summary>
/// IOC容器
/// </summary>
public class UnityService
{
static UnityService()
{
//注册标记
Current = new UnityContainer();
Current.AddNewExtension<Interception>();
var currentType = typeof (UnityService);
var containers = new List<dynamic>();
//模块初始化
containers.ForEach(item => item.Regist());
//模块启动
containers.ForEach(item => item.RegistComplete());
MetaDataManager.Type.GetAllTypes().ForEach(item =>
{
if (item == null )
return ;
var registers = item.GetCustomAttributes<InjectAttribute>().ToList();
if (registers.Count == 0)
return ;
registers.ForEach(register =>
{
if (register.RegisterType != null )
RegisterType(register.Name, register.RegisterType, item, GetLifetimeManager(register.LifetimeManagerType), GetInjectionMembers(register.AopType, item));
else
RegisterType(register.Name, item, GetLifetimeManager(register.LifetimeManagerType), GetInjectionMembers(register.AopType, item));
});
});
}
#region 属性
/// <summary>
/// Get the current configured container
/// </summary>
/// <returns>Configured container</returns>
private static IUnityContainer Current { get ; set ; }
#endregion
/// <summary>
/// 在当前模块中注册接口的实现
/// </summary>
protected virtual void Regist()
{
}
/// <summary>
/// 在当前模块中注册应用程序启动事件
/// </summary>
protected virtual void RegistComplete()
{
}
#region 注册相关的方法
/// <summary>
/// 获取生命周期
/// </summary>
/// <param name="lifetimeManagerType"></param>
/// <returns></returns>
public static LifetimeManager GetLifetimeManager(LifetimeManagerType lifetimeManagerType)
{
switch (lifetimeManagerType)
{
case LifetimeManagerType.Transient:
return new TransientLifetimeManager();
case LifetimeManagerType.ContainerControlled:
return new ContainerControlledLifetimeManager();
case LifetimeManagerType.Hierarchica:
return new HierarchicalLifetimeManager();
case LifetimeManagerType.Externally:
return new ExternallyControlledLifetimeManager();
case LifetimeManagerType.PerThread:
return new PerThreadLifetimeManager();
case LifetimeManagerType.PerResolve:
return new PerResolveLifetimeManager();
default :
return new TransientLifetimeManager();
}
}
/// <summary>
/// 注入aop方法
/// </summary>
/// <param name="aopType"></param>
/// <param name="type"></param>
/// <returns></returns>
public static InjectionMember[] GetInjectionMembers(AopType aopType, Type type)
{
var members = new List<InjectionMember>();
switch (aopType)
{
case AopType.VirtualMethodInterceptor:
members.Add( new Interceptor<VirtualMethodInterceptor>());
break ;
case AopType.InterfaceInterceptor:
members.Add( new Interceptor<InterfaceInterceptor>());
break ;
case AopType.TransparentProxyInterceptor:
members.Add( new Interceptor<TransparentProxyInterceptor>());
break ;
}
members.AddRange(type.GetCustomAttributes()
.Where(item => item.GetType().IsSubclassOf( typeof (UnityAopAttribute)))
.Cast<UnityAopAttribute>()
.Select(item => new InterceptionBehavior(item)));
return members.ToArray();
}
#endregion
/// <summary>
/// 注册泛型类型
/// </summary>
/// <param name="injectionMembers">构造函数参数</param>
public static void Register<TTarget, TSource>( params dynamic[] injectionMembers) where TSource : TTarget
{
RegisterType<TTarget, TSource>( "" ,injectionMembers);
}
/// <summary>
/// 注册泛型类型
/// </summary>
/// <param name="name"></param>
/// <param name="injectionMembers">构造函数参数</param>
public static void RegisterType<TTarget, TSource>( string name, params dynamic[] injectionMembers) where TSource : TTarget
{
var members = new List<InjectionMember>();
LinqExtensions.ForEach(injectionMembers, item =>
{
if (item is InjectionMember)
members.Add(item);
if (item is InjectionMember[])
members.AddRange(item);
else if (item is ConstructorParameter)
members.Add( new InjectionConstructor(item.Value));
else if (item is ConstructorParameter[])
members.AddRange((item as ConstructorParameter[]).Select(data => new InjectionConstructor(data.Value)));
});
var lifetimeManager = injectionMembers.OfType<LifetimeManager>().FirstOrDefault();
if ( string .IsNullOrEmpty(name))
{
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType<TTarget, TSource>();
else if (lifetimeManager == null )
Current.RegisterType<TTarget, TSource>(members.ToArray());
else if (injectionMembers == null )
Current.RegisterType<TTarget, TSource>(lifetimeManager);
else
Current.RegisterType<TTarget, TSource>(lifetimeManager, members.ToArray());
}
else
{
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType<TTarget, TSource>(name);
else if (lifetimeManager == null )
Current.RegisterType<TTarget, TSource>(name, members.ToArray());
else if (injectionMembers == null )
Current.RegisterType<TTarget, TSource>(name, lifetimeManager);
else
Current.RegisterType<TTarget, TSource>(name, lifetimeManager, members.ToArray());
}
}
/// <summary>
/// 注册类型
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
/// <param name="lifetimeManager"></param>
/// <param name="injectionMembers"></param>
public static void RegisterType( string name, Type target, Type source, params dynamic[] injectionMembers)
{
var members = new List<InjectionMember>();
LinqExtensions.ForEach(injectionMembers, item =>
{
if (item is InjectionMember)
members.Add(item);
if (item is InjectionMember[])
members.AddRange(item);
else if (item is ConstructorParameter)
members.Add( new InjectionConstructor(item.Value));
else if (item is ConstructorParameter[])
members.AddRange((item as ConstructorParameter[]).Select(data => new InjectionConstructor(data.Value)));
});
var lifetimeManager = injectionMembers.OfType<LifetimeManager>().FirstOrDefault();
if ( string .IsNullOrEmpty(name))
{
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType(target, source);
else if (lifetimeManager == null )
Current.RegisterType(target, source, members.ToArray());
else if (injectionMembers == null )
Current.RegisterType(target, source, lifetimeManager);
else
Current.RegisterType(target, source, lifetimeManager, members.ToArray());
}
else
{
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType(target, source, name);
else if (lifetimeManager == null )
Current.RegisterType(target, source, name, members.ToArray());
else if (injectionMembers == null )
Current.RegisterType(target, source, name, lifetimeManager);
else
Current.RegisterType(target, source, name, lifetimeManager, members.ToArray());
}
}
/// <summary>
/// 注册类型
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
/// <param name="lifetimeManager"></param>
/// <param name="injectionMembers"></param>
public static void RegisterType(Type target, Type source, params dynamic[] injectionMembers)
{
var members = new List<InjectionMember>();
LinqExtensions.ForEach(injectionMembers, item =>
{
if (item is InjectionMember)
members.Add(item);
if (item is InjectionMember[])
members.AddRange(item);
else if (item is ConstructorParameter)
members.Add( new InjectionConstructor(item.Value));
else if (item is ConstructorParameter[])
members.AddRange((item as ConstructorParameter[]).Select(data => new InjectionConstructor(data.Value)));
});
var lifetimeManager = injectionMembers.OfType<LifetimeManager>().FirstOrDefault();
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType(target, source);
else if (lifetimeManager == null )
Current.RegisterType(target, source, members.ToArray());
else if (injectionMembers == null )
Current.RegisterType(target, source, lifetimeManager);
else
Current.RegisterType(target, source, lifetimeManager, members.ToArray());
}
/// <summary>
/// 注册类型
/// </summary>
/// <param name="injectionMembers"></param>
public static void RegisterType(Type type, params dynamic[] injectionMembers)
{
var members = new List<InjectionMember>();
LinqExtensions.ForEach(injectionMembers, item =>
{
if (item is InjectionMember)
members.Add(item);
if (item is InjectionMember[])
members.AddRange(item);
else if (item is ConstructorParameter)
members.Add( new InjectionConstructor(item.Value));
else if (item is ConstructorParameter[])
members.AddRange((item as ConstructorParameter[]).Select(data => new InjectionConstructor(data.Value)));
});
var lifetimeManager = injectionMembers.OfType<LifetimeManager>().FirstOrDefault();
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType(type);
else if (lifetimeManager == null )
Current.RegisterType(type, members.ToArray());
else if (injectionMembers == null )
Current.RegisterType(type, lifetimeManager);
else
Current.RegisterType(type, lifetimeManager, members.ToArray());
}
/// <summary>
/// 注册类型
/// </summary>
/// <param name="type"></param>
/// <param name="injectionMembers"></param>
/// <param name="name"></param>
public static void RegisterType( string name, Type type, params dynamic[] injectionMembers)
{
var members = new List<InjectionMember>();
LinqExtensions.ForEach(injectionMembers, item =>
{
if (item is InjectionMember)
members.Add(item);
if (item is InjectionMember[])
members.AddRange(item);
else if (item is ConstructorParameter)
members.Add( new InjectionConstructor(item.Value));
else if (item is ConstructorParameter[])
members.AddRange((item as ConstructorParameter[]).Select(data => new InjectionConstructor(data.Value)));
});
var lifetimeManager = injectionMembers.OfType<LifetimeManager>().FirstOrDefault();
if ( string .IsNullOrEmpty(name))
{
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType(type);
else if (lifetimeManager == null )
Current.RegisterType(type, members.ToArray());
else if (injectionMembers == null )
Current.RegisterType(type, lifetimeManager);
else
Current.RegisterType(type, lifetimeManager, members.ToArray());
}
else
{
if (lifetimeManager == null && injectionMembers == null )
Current.RegisterType(type, name);
else if (lifetimeManager == null )
Current.RegisterType(type, name, members.ToArray());
else if (injectionMembers == null )
Current.RegisterType(type, name, lifetimeManager);
else
Current.RegisterType(type, name, lifetimeManager, members.ToArray());
}
}
/// <summary>
/// 创建实例
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static object Resolve(Type source)
{
return Current.Resolve(source);
}
private static ConcurrentDictionary< string , Action<dynamic>> _handles = new ConcurrentDictionary< string , Action<dynamic>>();
/// <summary>
/// 当创建新实例时触发
/// </summary>
/// <param name="handler"></param>
/// <returns></returns>
public void OnCreation<T>(Action<T> handler)
{
_handles.TryAdd( typeof (T).FullName, item => handler(item.Data));
}
public static bool HasRegistered(Type type)
{
return Current.IsRegistered(type);
}
/// <summary>
/// 创建泛型实例
/// </summary>
/// <returns></returns>
public static T Resolve<T>()
{
var result = Current.Resolve<T>();
Action<dynamic> handler;
if (_handles.TryGetValue( typeof (T).FullName, out handler))
{
handler( new EventArgs<T>(result));
}
return result;
}
/// <summary>
/// 创建泛型实例
/// </summary>
/// <returns></returns>
public static T Resolve<T>( string name)
{
return Current.Resolve<T>(name);
}
/// <summary>
/// 创建泛型实例集合
/// </summary>
/// <returns></returns>
public static T[] ResolveAll<T>()
{
var serviceImpls = Current.ResolveAll<T>();
List<T> proxiedServiceImpls = new List<T>();
foreach ( var serviceImpl in serviceImpls)
{
Action<dynamic> handler;
if (_handles.TryGetValue( typeof (T).FullName, out handler))
{
handler( new EventArgs<T>(serviceImpl));
}
proxiedServiceImpls.Add(serviceImpl);
}
return proxiedServiceImpls.ToArray();
}
public static void Release( object obj)
{
Current.Teardown(obj);
}
}
}
|