gpt4 book ai didi

.net - 如何使用 Unity DI 加载具有依赖项的程序集,然后注册内部组件

转载 作者:行者123 更新时间:2023-12-02 04:16:30 26 4
gpt4 key购买 nike

我有一个 asp.net mvc 3 Web 应用程序,它具有实现系统相关 EFcontext 和服务的插件程序集。

例如,我有一个通常如下所示的程序集:

System1Controller : IController // from System.Web.Mvc

ISystem1Service {
IList<System1Type> GetOperation(string something);
}

System1Service : ISystem1Service
{
private ISystem1Entities context;
public System1Service(ISystem1Entities context)
{
this.context = context;
}
}

ISystem1Entities
{
IDbSet<System1Type> Operations { get; set; }
}

System1Entities : DbContext, ISystem1Entities

使用 Unity Bootstrapper 并调用 Bootstrapper.Initialize() 可与以下 BuildUnityContainer() 实现配合使用

private static IUnityContainer BuildUnityContainer()
{
var theContainer = new UnityContainer();
var connectionString = ConfigurationManager.ConnectionStrings["WebAppConnectionString"];
if (connectionString == null)
{
throw new ApplicationException("The ConnectionString is not defined.");
}

// register all your components with the container here
// it is NOT necessary to register your controllers
theContainer.RegisterType<ISystem1Entities, System1Entities>(
new HierarchicalLifetimeManager(),
new InjectionConstructor(connectionString.ConnectionString));

theContainer.RegisterType<ISystem1Service, System1Service>();

var factory = new UnityControllerFactory(theContainer);
ControllerBuilder.Current.SetControllerFactory(factory);

return theContainer;
}

我想重构 Bootstrap ,以便可以在编译时加载“未知”程序集,注册其中包含的类型,并允许 Web 应用程序根据需要引用 Controller 、服务和上下文。

我查看了 Unity AutoRegistraion项目,它似乎让我接近了我想要的实现,但我不确定如何实现以下想法:

BootStrapper.BuildUnityConfiguration
Initialize Container
RegisterMyAssemblies()

我希望“RegisterMyAssemblies”进程注册程序集的通配符列表,然后继续注册每个单独程序集中的类型。有什么建议吗?

最佳答案

您不应该尝试使用应用程序的主 Bootstrap (组合根)来配置这些插件程序集,因为这很难做到正确。这很难做到正确,因为主程序不知道要注册哪些类型,以及这些类型应该在什么生命周期内注册。

相反,让每个插件程序集都有自己的 Bootstrap 方法,并将应用程序的容器实例传递到此插件 Bootstrap 上。

例如,您可以在核心程序集中定义一个 IBootstrapper 接口(interface),并让每个插件程序集实现该接口(interface)。例如:

public class System1Bootstrapper : IBootstrapper
{
void IBootstrapper.Bootstrap(IUnityContainer container)
{
var conString = ConfigurationManager
.ConnectionStrings["WebAppConnectionString"];

if (conString == null)
{
throw new ApplicationException(
"The ConnectionString is not defined.");
}

// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterType<ISystem1Entities, System1Entities>(
new HierarchicalLifetimeManager(),
new InjectionConstructor(conString.ConnectionString));

container.RegisterType<ISystem1Service, System1Service>();
}
}

在应用程序的组合根目录中,您现在只需添加以下代码即可注册所有插件类型:

var pluginBootstrappers =
from Assembly assembly in BuildManager.GetReferencedAssemblies()
from type in assembly.GetExportedTypes()
where typeof(IBootstrapper).IsAssignableFrom(type)
select (IBootstrapper)Activator.CreateInstance(type);

pluginBootstrappers.ToList().ForEach(b => b.Bootstrap(container));

关于.net - 如何使用 Unity DI 加载具有依赖项的程序集,然后注册内部组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12783768/

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