/// <summary> /// Initializes a new instance of the <see cref="DependencyLifetimeScopeEndingEventArgs"/> class. /// </summary> /// <param name="lifetimeScope">The lifetime scope that is ending.</param> public DependencyLifetimeScopeEndingEventArgs(IDependencyLifetimeScope lifetimeScope) { if (lifetimeScope == null) { throw new ArgumentNullException(nameof(lifetimeScope)); } LifetimeScope = lifetimeScope; }
private TNextSequenceNumberRepository ResolveNextSequenceNumberRepository(IDependencyLifetimeScope scope) { var nextSequenceNumberRepository = (TNextSequenceNumberRepository)scope.Resolve(typeof(TNextSequenceNumberRepository)); if (nextSequenceNumberRepository == null) { throw new InvalidOperationException($"Can't resolve {typeof(TNextSequenceNumberRepository).Name} from the dependency lifetime scope"); } return(nextSequenceNumberRepository); }
/// <summary> /// Initializes a new instance of the <see cref="DependencyResolvedEventArgs"/> class. /// </summary> /// <param name="lifetimeScope">The lifetime scope where the dependency was resolved.</param> /// <param name="dependencyType">The dependency type that was resolved.</param> /// <param name="dependency">The dependency instance that was resolved.</param> public DependencyResolvedEventArgs(IDependencyLifetimeScope lifetimeScope, Type dependencyType, object dependency) { if (lifetimeScope == null) { throw new ArgumentNullException(nameof(lifetimeScope)); } if (dependencyType == null) { throw new ArgumentNullException(nameof(dependencyType)); } if (dependency == null) { throw new ArgumentNullException(nameof(dependency)); } LifetimeScope = lifetimeScope; DependencyType = dependencyType; Dependency = dependency; }
/// <summary> /// 依赖引擎初始化(再次初始化等于重新构造) /// </summary> public void Initialize() { #region 1.初始化固定系统级注册 ContainerBuilder builder = new ContainerBuilder(); builder.Register <ITypeFinder>(c => new TypeDefaultFinder()).SingleInstance(); builder.Register <IDbMappingHandler>(c => new DbMappingHandler()).SingleInstance(); //加密&解密 this.CryptographyRegistration(builder); this._container = builder.Build(); ITypeFinder typeFinder = this._container.Resolve <ITypeFinder>(); if (null == typeFinder) { throw new Exception("typeFinder is not register!"); } #endregion #region 2.IDependencyLifetimeScope引擎接口注册 IEnumerable <Type> lifetimeScopeTypes = typeFinder.FindClassesOfType <IDependencyLifetimeScope>(); int lifetimeScopeClassTotal = lifetimeScopeTypes.Count(d => d.FullName != this.GetType().FullName); if (lifetimeScopeClassTotal <= 0) { this._lifetimeScop = this; } else { if (lifetimeScopeClassTotal > 1) { throw new Exception("IDependencyLifetimeScope接口的实现只用在全局实现一次即可,切勿多次实现!"); } this._lifetimeScop = Activator.CreateInstance(lifetimeScopeTypes.First()) as IDependencyLifetimeScope; } #endregion #region 3.IDependencyRegisterHook 外部挂钩注册 IEnumerable <Type> hookTypes = typeFinder.FindClassesOfType <IDependencyRegisterHook>(); if (hookTypes.Any()) { builder = new ContainerBuilder(); List <IDependencyRegisterHook> hookList = new List <IDependencyRegisterHook>(); foreach (var hookT in hookTypes) { hookList.Add(Activator.CreateInstance(hookT) as IDependencyRegisterHook); } hookList = hookList.AsQueryable().OrderBy(d => d.Priority).ToList(); hookList.ForEach(o => o.Register(builder, typeFinder)); builder.Update(this._container); } #endregion #region 99.挂载自身接口与实现的注册(单例) builder = new ContainerBuilder(); builder.RegisterInstance(this).As(typeof(IDependencyManager), typeof(IDependencyResolver)).SingleInstance(); builder.Update(this._container); #endregion #region 100.将注册的IDependencyRebuildDelegate接口实现进行纳入执行 IEnumerable <Type> rebuildDelegateTypes = typeFinder.FindClassesOfType <IDependencyInitComplete>(); List <IDependencyInitComplete> taskList = new List <IDependencyInitComplete>(); foreach (var taskT in rebuildDelegateTypes) { taskList.Add(Activator.CreateInstance(taskT) as IDependencyInitComplete); } foreach (var taskOpt in taskList.AsQueryable().OrderBy(d => d.Priority)) { taskOpt.OnCompleted(this._container); } #endregion }