IOC容器特性注入第四篇:容器初始化
IOC容器,就是把各种服务都注入到容器里,想要什么就去拿什么,不仅解决服务类和接口类的耦合度还可以提高性能方便管理。
这里封装一个接口类和一个实现类
1.IContainerManager接口
public interface IContainerManger : IDisposable { //#region AddResolvingObserver //void AddResolvingObserver(IResolvingObserver observer); //#endregion #region AddComponent void AddComponent<TService>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton); void AddComponent(Type type, string key = "", ComponentLifeStyle liftStyle = ComponentLifeStyle.Singleton); void AddComponent<TService, TImplementation>(string key = "", ComponentLifeStyle liftStyle = ComponentLifeStyle.Singleton); void AddComponent(Type service, Type implementation, string key = "", ComponentLifeStyle liftStyle = ComponentLifeStyle.Singleton, params Parameter[] parameters); void AddComponentInstance<TService>(object instance, string key = ""); void AddComponentInstance(object instance, string key = ""); void AddComponentInstance(Type service, object instance, string key = ""); #endregion #region Resolve T Resolve<T>(string key = "", params Parameter[] parameters) where T : class; object Resolve(Type type, string key = "", params Parameter[] parameters); #endregion #region ResolveAll T[] ResolveAll<T>(string key = ""); object[] ResolveAll(Type type, string key = ""); #endregion #region TryResolve T TryResolve<T>(string key = "", params Parameter[] parameters); object TryResolve(Type type, string key = "", params Parameter[] parameters); #endregion #region ResolveUnRgeistered(未注册) T ResolveUnregistered<T>() where T : class; object ResolveUnregistered(Type type); #endregion }
也是一样,实例版本和泛型版本都写了,方便使用
2. ContainerManager实现类
public class ContainerManager : StandardKernel, IContainerManger { #region Fields private IKernel _container; #endregion #region .ctor public ContainerManager() { _container = new StandardKernel(); _container.Settings.Set("InjectAttribute", typeof(DaHua.Common.Runtime.Dependency.InjectAttribute)); } #endregion #region Properties public IKernel Container { get { return _container; } } #endregion #region AddComponent public void AddComponent<TService>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { AddComponent(typeof(TService), typeof(TService), key, lifeStyle); } public void AddComponent(Type type, string key = "", ComponentLifeStyle liftStyle = ComponentLifeStyle.Singleton) { AddComponent(type, type, key, liftStyle); } public void AddComponent<TService, TImplementation>(string key = "", ComponentLifeStyle liftStyle = ComponentLifeStyle.Singleton) { AddComponent(typeof(TService), typeof(TImplementation), key, liftStyle); } public void AddComponent(Type service, Type implementation, string key = "", ComponentLifeStyle liftStyle = ComponentLifeStyle.Singleton, params Parameter[] parameters) { var binding = _container.Bind(service).To(implementation); if (parameters != null) { var ninjectParameter = ConvertParameters(parameters); foreach (var parameter in ninjectParameter) { binding.WithParameter(parameter); } } binding.PerLifeStyle(liftStyle).MapKey(key).ReplaceExsting(service); } #endregion #region AddComponentInstance public void AddComponentInstance<TService>(object instance, string key = "") { AddComponentInstance(typeof(TService), instance, key); } public void AddComponentInstance(object instance, string key = "") { AddComponentInstance(instance.GetType(), instance, key); } public void AddComponentInstance(Type service, object instance, string key = "") { _container.Bind(service).ToConstant(instance).MapKey(key).ReplaceExsting(service); } #endregion #region ConvertParameters private static NinjectParameters.IParameter[] ConvertParameters(Parameter[] parameters) { if (parameters == null) { return null; } return parameters.Select(it => new NinjectParameters.ConstructorArgument(it.Name, (context) => it.valueCallback())) .ToArray(); } #endregion #region Resolve public virtual T Resolve<T>(string key = "", params Parameter[] parameters) where T : class { if (string.IsNullOrWhiteSpace(key)) { return _container.Get<T>(ConvertParameters(parameters)); } return _container.Get<T>(key, ConvertParameters(parameters)); } public virtual object Resolve(Type type, string key = "", params Common.Runtime.Parameter[] parameters) { if (string.IsNullOrWhiteSpace(key)) { return _container.Get(type, ConvertParameters(parameters)); } return _container.Get(type, key, ConvertParameters(parameters)); } #endregion #region ResolveAll public virtual T[] ResolveAll<T>(string key = "") { if (string.IsNullOrWhiteSpace(key)) { return _container.GetAll<T>().ToArray(); } return _container.GetAll<T>(key).ToArray(); } public virtual object[] ResolveAll(Type type, string key = "") { if (string.IsNullOrWhiteSpace(key)) { return _container.GetAll(type).ToArray(); } return _container.GetAll(type, key).ToArray(); } #endregion #region public virtual T TryResolve<T>(string key = "", params Common.Runtime.Parameter[] parameters) { if (string.IsNullOrWhiteSpace(key)) { return _container.TryGet<T>(ConvertParameters(parameters)); } return _container.TryGet<T>(key, ConvertParameters(parameters)); } public virtual object TryResolve(Type type, string key = "", params Common.Runtime.Parameter[] parameters) { if (string.IsNullOrWhiteSpace(key)) { return _container.TryGet(type, ConvertParameters(parameters)); } return _container.TryGet(type, key, ConvertParameters(parameters)); } #endregion #region ResolveUnRegistered public T ResolveUnregistered<T>() where T : class { return ResolveUnregistered(typeof (T)) as T; } public object ResolveUnregistered(Type type) { var constructors = type.GetConstructors(); foreach (var constructor in constructors) { var parameters = constructor.GetParameters(); var parameterInstances = new List<object>(); foreach (var parameter in parameters) { var service = Resolve(parameter.ParameterType); if(service==null) parameterInstances.Add(service); } return Activator.CreateInstance(type, parameterInstances.ToArray()); } throw new Exception("构造方法没有发现需要依赖的字段"); } #endregion public void Dispose() { if (this._container != null && !this._container.IsDisposed) { this._container.Dispose(); } this._container = null; } }
这个类不仅实现了接口还继承Ninject官方类,是为了减少其它程序在做注入的时候减少对Ninject的依赖,有了这个类以后,未来的扩展程序集在注册组件时就不用引用Ninject,对它形成依赖。
下一篇: