LCLFramework框架之IOC
我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑。
借助于“第三方”实现具有依赖关系的对象之间的解耦,如下图:
LCLFramework框架之依赖注入(IOC)职责
- 依赖注入(Dependency Injection,简称DI)
- 依赖查找(Dependency Lookup)
- 控制反转
LCLFramework框架之依赖注入(IOC)设计
网上有很多的IOC框架如何让用户选择自己熟悉的IOC框架了,那LCL就需要提供可扩展的IOC应该如何让框架注入自己熟悉的IOC框架呢?
下图是基本的IOC扩展契约,所有的第三方都要实现IObjectContainer契约,下图中实现了,Unity,TinyIoc两个第三方IOC框架。
抛弃了ObjectContainer类,直接使用接口进行扩展
LCLFramework框架之依赖注入(IOC)设计代码
/// <summary> /// 表示持久化事件时出现的并发异常 /// </summary> public interface IObjectContainer { /// <summary> /// 注册一个给定的类型及其所有实现的接口 /// </summary> /// <param name="type"></param> void RegisterType(Type type); /// <summary> /// 注册一个给定的类型及其所有实现的接口 /// </summary> /// <param name="type"></param> /// <param name="key"></param> void RegisterType(Type type, string key); /// <summary> /// 注册给定程序集中符合条件的所有类型 /// </summary> /// <param name="typePredicate"></param> /// <param name="assemblies"></param> void RegisterTypes(Func<Type, bool> typePredicate, params Assembly[] assemblies); /// <summary> /// 注册给定接口的实现 /// </summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <param name="life"></param> void Register<TService, TImpl>(LifeStyle life) where TService : class where TImpl : class, TService; /// <summary> /// 注册给定接口的实现 /// </summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <param name="key"></param> /// <param name="life"></param> void Register<TService, TImpl>(string key, LifeStyle life = LifeStyle.Singleton) where TService : class where TImpl : class, TService; /// <summary> /// 注册给定接口的默认实现 /// </summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <param name="life"></param> void RegisterDefault<TService, TImpl>(LifeStyle life) where TService : class where TImpl : class, TService; /// <summary> /// 注册给定类型的实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance"></param> /// <param name="life"></param> void Register<T>(T instance, LifeStyle life) where T : class; /// <summary> /// 注册给定类型的实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance"></param> /// <param name="key"></param> /// <param name="life"></param> void Register<T>(T instance, string key, LifeStyle life) where T : class; /// <summary> /// 判断给定的类型是否已经注册 /// </summary> /// <param name="type"></param> /// <returns></returns> bool IsRegistered(Type type); /// <summary> /// 判断给定的类型是否已经注册 /// </summary> /// <param name="type"></param> /// <param name="key"></param> /// <returns></returns> bool IsRegistered(Type type, string key); /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> T Resolve<T>() where T : class; /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> T Resolve<T>(string key) where T : class; /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <param name="type"></param> /// <returns></returns> object Resolve(Type type); /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <param name="key"></param> /// <param name="type"></param> /// <returns></returns> object Resolve(string key, Type type); } [DebuggerDisplay("ObjectContainer = {_container}")] public class ServiceLocator { private readonly IObjectContainer _container = LEnvironment.AppObjectContainer; public static readonly ServiceLocator Instance = new ServiceLocator(); private ServiceLocator() { } /// <summary> /// 注册一个给定的类型及其所有实现的接口 /// </summary> /// <param name="type"></param> public void RegisterType(Type type) { _container.RegisterType(type); } /// <summary> /// 注册一个给定的类型及其所有实现的接口 /// </summary> /// <param name="type"></param> /// <param name="key"></param> public void RegisterType(Type type, string key) { _container.RegisterType(type, key); } public void RegisterType(Type type, Type Impl) { //_container.RegisterType() } /// <summary> /// 注册给定程序集中符合条件的所有类型 /// </summary> /// <param name="typePredicate"></param> /// <param name="assemblies"></param> public void RegisterTypes(Func<Type, bool> typePredicate, params Assembly[] assemblies) { _container.RegisterTypes(typePredicate, assemblies); } /// <summary> /// 注册给定接口的实现 /// </summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <param name="life"></param> public void Register<TService, TImpl>(LifeStyle life = LifeStyle.Singleton) where TService : class where TImpl : class, TService { _container.Register<TService, TImpl>(life); } /// <summary> /// 注册给定接口的实现 /// </summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <param name="key"></param> /// <param name="life"></param> public void Register<TService, TImpl>(string key, LifeStyle life = LifeStyle.Singleton) where TService : class where TImpl : class, TService { _container.Register<TService, TImpl>(key, life); } /// <summary> /// 注册给定接口的默认实现 /// </summary> /// <typeparam name="TService"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <param name="life"></param> public void RegisterDefault<TService, TImpl>(LifeStyle life = LifeStyle.Singleton) where TService : class where TImpl : class, TService { _container.RegisterDefault<TService, TImpl>(life); } /// <summary> /// 注册给定类型的实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance"></param> /// <param name="life"></param> public void Register<T>(T instance, LifeStyle life = LifeStyle.Singleton) where T : class { _container.Register<T>(instance, life); } /// <summary> /// 注册给定类型的实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance"></param> /// <param name="key"></param> /// <param name="life"></param> public void Register<T>(T instance, string key, LifeStyle life = LifeStyle.Singleton) where T : class { _container.Register<T>(instance, key, life); } /// <summary> /// 判断给定的类型是否已经注册 /// </summary> /// <param name="type"></param> /// <returns></returns> public bool IsRegistered(Type type) { return _container.IsRegistered(type); } /// <summary> /// 判断给定的类型是否已经注册 /// </summary> /// <param name="type"></param> /// <param name="key"></param> /// <returns></returns> public bool IsRegistered(Type type, string key) { return _container.IsRegistered(type, key); } /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T Resolve<T>() where T : class { return _container.Resolve<T>(); } /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T Resolve<T>(string key) where T : class { return _container.Resolve<T>(key); } /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <param name="type"></param> /// <returns></returns> public object Resolve(Type type) { return _container.Resolve(type); } /// <summary> /// 获取给定类型的一个实例 /// </summary> /// <param name="key"></param> /// <param name="type"></param> /// <returns></returns> public object Resolve(string key, Type type) { return _container.Resolve(key, type); } }
LCLFramework框架之IOC扩展
public class UnityObjectContainer : IObjectContainer { private IUnityContainer _unityContainer; public UnityObjectContainer() { _unityContainer = new UnityContainer(); } public UnityObjectContainer(IUnityContainer unityContainer) { _unityContainer = unityContainer; } public IUnityContainer UnityContainer { get { return _unityContainer; } } public void RegisterType(Type type) { var life = ParseLife(type); if (!_unityContainer.IsRegistered(type)) { if (life == LifeStyle.Singleton) { _unityContainer.RegisterType(type, new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterType(type); } } foreach (var interfaceType in type.GetInterfaces()) { if (!_unityContainer.IsRegistered(interfaceType)) { if (life == LifeStyle.Singleton) { _unityContainer.RegisterType(interfaceType, type, new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterType(interfaceType, type); } } } } public void RegisterType(Type type, string key) { var life = ParseLife(type); if (!IsRegistered(type, key)) { if (life == LifeStyle.Singleton) { _unityContainer.RegisterType(type, new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterType(type); } } foreach (var interfaceType in type.GetInterfaces()) { if (!IsRegistered(interfaceType, key)) { if (life == LifeStyle.Singleton) { _unityContainer.RegisterType(interfaceType, type, new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterType(interfaceType, type); } } } } public void RegisterTypes(Func<Type, bool> typePredicate, params Assembly[] assemblies) { foreach (var assembly in assemblies) { foreach (var type in assembly.GetExportedTypes().Where(x => typePredicate(x))) { RegisterType(type); } } } public void Register<TService, TImpl>(LifeStyle life = LifeStyle.Singleton) where TService : class where TImpl : class, TService { if (life == LifeStyle.Singleton) { _unityContainer.RegisterType<TService, TImpl>(new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterType<TService, TImpl>(); } } public void Register<TService, TImpl>(string key, LifeStyle life = LifeStyle.Singleton) where TService : class where TImpl : class, TService { if (life == LifeStyle.Singleton) { _unityContainer.RegisterType<TService, TImpl>(key, new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterType<TService, TImpl>(key); } } public void RegisterDefault<TService, TImpl>(LifeStyle life = LifeStyle.Singleton) where TService : class where TImpl : class, TService { if (life == LifeStyle.Singleton) { _unityContainer.RegisterType<TService, TImpl>(new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterType<TService, TImpl>(); } } public void Register<T>(T instance, LifeStyle life = LifeStyle.Singleton) where T : class { if (life == LifeStyle.Singleton) { _unityContainer.RegisterInstance<T>(instance, new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterInstance<T>(instance); } } public void Register<T>(T instance, string key, LifeStyle life = LifeStyle.Singleton) where T : class { if (life == LifeStyle.Singleton) { _unityContainer.RegisterInstance<T>(key, instance, new ContainerControlledLifetimeManager()); } else { _unityContainer.RegisterInstance<T>(key, instance); } } public bool IsRegistered(Type type) { return _unityContainer.IsRegistered(type); } public bool IsRegistered(Type type, string key) { return _unityContainer.IsRegistered(type, key); } public T Resolve<T>() where T : class { return _unityContainer.Resolve<T>(); } public T Resolve<T>(string key) where T : class { return _unityContainer.Resolve<T>(key); } public object Resolve(Type type) { return _unityContainer.Resolve(type); } public object Resolve(string key, Type type) { return _unityContainer.Resolve(type, key); } private static LifeStyle ParseLife(Type type) { var componentAttributes = type.GetCustomAttributes(typeof(ComponentAttribute), false); return componentAttributes.Count() <= 0 ? LifeStyle.Transient : (componentAttributes[0] as ComponentAttribute).LifeStyle; } }