我正在尝试编写类似于“统一模块化”项目的代码。我构建了 2 个模块:模块 A(通过代码加载)和模块 D(从目录加载)。两者都成功加载。
但是当我尝试设置依赖时它不起作用。我真的无法弄清楚这个项目中提到的依赖关系。(我在 ModuleA 类中将依赖项设置为属性,在构建后复制 moduleD)这是我的实现:
引导程序.cs
protected override IModuleCatalog CreateModuleCatalog()
{
return new AggregateModuleCatalog();
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
Type moduleAType = typeof(ModuleAModule);
ModuleCatalog.AddModule(new ModuleInfo()
{
ModuleName = ModuleNames.ModuleA,
ModuleType = moduleAType.AssemblyQualifiedName
});
DirectoryModuleCatalog directoryCatalog = new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
((AggregateModuleCatalog)ModuleCatalog).AddCatalog(directoryCatalog);
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
this.RegisterTypeIfMissing(typeof(IModuleTracker), typeof(ModuleTracker), true);
}
模块A.cs
[Module(ModuleName = ModuleNames.ModuleA)]
[ModuleDependency(ModuleNames.ModuleD)]
public class ModuleAModule : IModule
{
private ILoggerFacade _logger;
private IModuleTracker _moduleTracker;
public ModuleAModule(ILoggerFacade logger, IModuleTracker moduleTracker)
{
_logger = logger;
_moduleTracker = moduleTracker;
_moduleTracker.ModuleConstructed("ModuleA");
}
public void Initialize()
{
_logger.Log("ModuleA demonstrates logging during Initialize().", Category.Info, Priority.Medium);
_moduleTracker.ModuleInitialized("ModuleA");
}
}
模块D.cs
[Module(ModuleName = ModuleNames.ModuleD)]
public class ModuleDModule : IModule
{
private ILoggerFacade _logger;
private IModuleTracker _moduleTracker;
public ModuleDModule(ILoggerFacade logger, IModuleTracker moduleTracker)
{
_logger = logger;
_moduleTracker = moduleTracker;
_moduleTracker.ModuleConstructed("ModuleD");
}
public void Initialize()
{
_moduleTracker.ModuleInitialized("ModuleD");
}
}