我有一个 asp.net mvc 3 Web 应用程序,它具有实现系统相关 EFcontexts 和服务的插件程序集。
例如,我有一个通常如下所示的程序集:
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;
}
我想重构引导程序,以便我可以在编译时加载“未知”程序集,注册其中包含的类型,并允许 Web 应用程序根据需要引用控制器、服务、上下文。
我查看了Unity AutoRegistraion项目,它似乎让我接近了我想要的实现,但我不确定如何实现以下想法:
BootStrapper.BuildUnityConfiguration
Initialize Container
RegisterMyAssemblies()
我希望“RegisterMyAssemblies”进程注册程序集的通配符列表,然后继续在每个单独的程序集中注册类型。有什么建议么?