gpt4 book ai didi

c# - Autofac 模块有自己的依赖项

转载 作者:行者123 更新时间:2023-12-01 16:19:49 26 4
gpt4 key购买 nike

鉴于某些模块本身具有依赖性,我正在努力解决如何在模块中组织我的 Autofac 组件注册。

我在接口(interface)中实现了配置数据的抽象( web.config):

interface IConfigurationProvider
{
T GetSection<T>(string sectionName)
where T : System.Configuration.ConfigurationSection;
}

以及 ASP.NET ( WebConfigurationProvider ) 和“桌面”应用程序 ( ExeConfigurationProvider ) 的实现。

我的一些 autofac 模块需要 IConfigurationProvider作为构造函数参数,但有些则不然:

class DependentModule : Module
{
public DependentModule(IConfigurationProvider config)
{
_config = config;
}

protected override void Load(ContainerBuilder builder)
{
var configSection = _config.GetSection<CustomConfigSection>("customSection");
builder.RegisterType(configSection.TypeFromConfig);
}

private readonly IConfigurationProvider _config;
}

class IndependentModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(/* other stuff not based on configuration */);
}
}

RegisterType()扩展方法不接受注册委托(delegate) ( Func<IComponentContext, T> ),例如 Register()是的,我无法注册 IConfigurationProvider预先处理,然后当我去注册配置中指定的类型时解决它,例如:

// this would be nice...
builder.RegisterType(c => c.Resolve<IConfigurationProvider>().GetSection<CustomConfigSection>("sectionName").TypeFromConfig);

这意味着我需要能够注册有或没有依赖 IConfigurationProvider 的模块.

很明显如何手动实例化每个模块并注册它:

IConfigurationProvider configProvider = ...;
var builder = new ContainerBuilder();
builder.RegisterModule(new DependentModule(configProvider));
builder.RegisterModule(new IndependentModule());
using (var container = builder.Build())
{
...
}

但我不想手动实例化我的模块 - 我想扫描模块的程序集并自动注册它们(如讨论的 in this question )。所以我必须使用反射来扫描程序集 IModule类型,并使用 Activator.CreateInstance制作可注册实例。但我怎么知道是否通过IConfigurationProvider作为构造函数参数。当其他模块具有额外或不同的依赖项时会发生什么?

必须有一种更直接的方法来完成基本任务:注册通过接口(interface)提供的某些配置中指定的类型,对吧?那么我该怎么做呢?

最佳答案

你可以这样做:

using System.Collections.Generic;
using System.Linq;
using Autofac;
using Autofac.Core;
using NUnit.Framework;

namespace Yo_dawg
{
[TestFixture]
public class I_heard_you_like_containers
{
[Test]
public void So_we_built_a_container_to_build_your_container()
{
var modules = GetModules();
Assert.That(modules.Length, Is.EqualTo(4));

var builder = new ContainerBuilder();

foreach (var module in modules)
builder.RegisterModule(module);

var container = builder.Build();
}

private IModule[] GetModules()
{
var builder = new ContainerBuilder();

var configurationProvider = new ConfigurationProvider();
builder.RegisterInstance(configurationProvider).AsImplementedInterfaces().ExternallyOwned();

builder.RegisterAssemblyTypes(GetType().Assembly)
.Where(t => t.IsAssignableTo<IModule>())
.AsImplementedInterfaces();

using (var container = builder.Build())
return container.Resolve<IEnumerable<IModule>>().ToArray();
}
}

public class ModuleA : Module
{
public ModuleA(IConfigurationProvider config)
{
}
}

public class ModuleB : Module
{
public ModuleB(IConfigurationProvider config)
{
}
}

public class ModuleC : Module
{
}

public class ModuleD : Module
{
}


public interface IConfigurationProvider
{
}

public class ConfigurationProvider : IConfigurationProvider
{
}
}

关于c# - Autofac 模块有自己的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11089487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com