使用 DryIoc 替换 Abp 的 DI 框架 (6)

而 AbpBootstrapper 类型还有一处问题一样是使用了 IWindsorContainer 提供的方法,这里改为 DryIoc 提供的方法即可。

private void RegisterBootstrapper() { if (!IocManager.IsRegistered<AbpBootstrapper>()) { // IocManager.IocContainer.Register( // Component.For<AbpBootstrapper>().Instance(this) // ); IocManager.IocContainer.UseInstance(this); } }

第二个问题则是 AbpKernelModule 当中的报错,其实与上一个类型的错误一样,第一个是调用了之前的 Install 的方法,并且 Intsaller 也不是继承自 IDryIocInstaller,另一个问题则是使用了 IWindsorContainer 里面的注册方法。

public override void Initialize() { foreach (var replaceAction in ((AbpStartupConfiguration)Configuration).ServiceReplaceActions.Values) { replaceAction(); } // IocManager.IocContainer.Install(new EventBusInstaller(IocManager)); IocManager.Install(new EventBusInstaller(IocManager)); IocManager.Register(typeof(IOnlineClientManager<>), typeof(OnlineClientManager<>), DependencyLifeStyle.Singleton); IocManager.RegisterAssemblyByConvention(typeof(AbpKernelModule).GetAssembly(), new ConventionalRegistrationConfig { InstallInstallers = false }); }

EventBusInstaller 的变更:

using System.Reflection; using Abp.Configuration.Startup; using Abp.Dependency; using Abp.Events.Bus.Factories; using Abp.Events.Bus.Handlers; using Castle.MicroKernel; using Castle.MicroKernel.Registration; using Castle.MicroKernel.SubSystems.Configuration; using Castle.Windsor; using DryIoc; namespace Abp.Events.Bus { /// <summary> /// Installs event bus system and registers all handlers automatically. /// </summary> internal class EventBusInstaller : IDryIocInstaller { private readonly IIocResolver _iocResolver; private readonly IEventBusConfiguration _eventBusConfiguration; private IEventBus _eventBus; public EventBusInstaller(IIocResolver iocResolver) { _iocResolver = iocResolver; _eventBusConfiguration = iocResolver.Resolve<IEventBusConfiguration>(); } public void Install(IIocManager iocManager) { if (_eventBusConfiguration.UseDefaultEventBus) { iocManager.IocContainer.UseInstance<IEventBus>(EventBus.Default); } else { iocManager.IocContainer.Register<IEventBus,EventBus>(Reuse.Singleton); } _eventBus = iocManager.Resolve<IEventBus>(); iocManager.RegisterTypeEventHandler += (manager, type, implementationType) => { if (!typeof(IEventHandler).GetTypeInfo().IsAssignableFrom(implementationType)) { return; } var interfaces = implementationType.GetTypeInfo().GetInterfaces(); foreach (var @interface in interfaces) { if (!typeof(IEventHandler).GetTypeInfo().IsAssignableFrom(@interface)) { continue; } var genericArgs = @interface.GetGenericArguments(); if (genericArgs.Length == 1) { _eventBus.Register(genericArgs[0], new IocHandlerFactory(_iocResolver, implementationType)); } } }; } } }

另外一处的变更如下:

private void RegisterMissingComponents() { if (!IocManager.IsRegistered<IGuidGenerator>()) { // IocManager.IocContainer.Register( // Component // .For<IGuidGenerator, SequentialGuidGenerator>() // .Instance(SequentialGuidGenerator.Instance) // ); IocManager.IocContainer.UseInstance<IGuidGenerator>(SequentialGuidGenerator.Instance); IocManager.IocContainer.UseInstance<SequentialGuidGenerator>(SequentialGuidGenerator.Instance); } IocManager.RegisterIfNot<IUnitOfWork, NullUnitOfWork>(DependencyLifeStyle.Transient); IocManager.RegisterIfNot<IAuditingStore, SimpleLogAuditingStore>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<IPermissionChecker, NullPermissionChecker>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<IRealTimeNotifier, NullRealTimeNotifier>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<INotificationStore, NullNotificationStore>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<IUnitOfWorkFilterExecuter, NullUnitOfWorkFilterExecuter>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<IClientInfoProvider, NullClientInfoProvider>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<ITenantStore, NullTenantStore>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<ITenantResolverCache, NullTenantResolverCache>(DependencyLifeStyle.Singleton); IocManager.RegisterIfNot<IEntityHistoryStore, NullEntityHistoryStore>(DependencyLifeStyle.Singleton); if (Configuration.BackgroundJobs.IsJobExecutionEnabled) { IocManager.RegisterIfNot<IBackgroundJobStore, InMemoryBackgroundJobStore>(DependencyLifeStyle.Singleton); } else { IocManager.RegisterIfNot<IBackgroundJobStore, NullBackgroundJobStore>(DependencyLifeStyle.Singleton); } } 4.1.6 测试

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyypz.html