如何将Unity的流畅配置移植到XML配置?
我在控制器中使用构造函数注入,通过在 DI 容器中注册的 contextFactory 来处理多个上下文(首先是 EF 代码)。我有 3 个从 CoreContext 继承的 ProductXContext。我将每个上下文都放在一个单独的项目中。目前我的MCV项目中都引用了它们。 我想保留引用的基本上下文 (CoreContext),但删除 ProductXContext 引用并使用 Unity 在运行时解析和注入依赖项。
目前,我正在使用流畅的配置来设置我的容器:
var container = new UnityContainer();
container.RegisterType<CoreContext>(new InjectionFactory(c => ContextFactory.CreateContext()));
上下文工厂如下所示:
public static CoreContext CreateContext()
{
var userProduct = HttpContext.Current.Session["Product"] as string;
CoreContext context;
switch (userProduct)
{
case null:
throw new Exception("No product is selected. Can't define an appropriate context.");
case "Product 1":
context = new Product1Context();
break;
case "Product 2":
context = new Product2Context();
break;
case "Product 3":
context = new Product3Context();
break;
default:
throw new Exception("Current product is not supported. Can't define an appropriate context.");
}
return context;
}
控制器如下所示:
public class MyController : Controller
{
private CoreContext _context;
public MyController(CoreContext context)
{
_context = context;
}
...
}
因此,如果我要从 MVC 项目中删除 ProductXContext 引用,似乎我必须切换到 XML 配置因为为了编译流畅的代码,您必须引用所有代码。但我现在有了想法:
- 如何在 XML 配置中声明 InjectionFactory。
- 如果我设法声明它,如何在我的 ContextFactory 中实例化 ProductXContext,因为我不再引用它们。
PS:有没有一种方法可以使用 XML 配置注册外部类型,然后声明jectionFactory 使用流畅的配置?
提前致谢!
I'm using constructor injection in my controllers to work with multiple contexts (EF code first) via a contextFactory registered in my DI container. I have 3 ProductXContexts that inherit from my CoreContext. I have each context in a separate project. Currently they are all referenced in my MCV project.
I want to keep the base context (CoreContext) referenced but remove the ProductXContext references and use Unity to resolve and inject the dependences at runtime.
Currently I'm using fluent configuration to set up my container:
var container = new UnityContainer();
container.RegisterType<CoreContext>(new InjectionFactory(c => ContextFactory.CreateContext()));
The context factory looks like this:
public static CoreContext CreateContext()
{
var userProduct = HttpContext.Current.Session["Product"] as string;
CoreContext context;
switch (userProduct)
{
case null:
throw new Exception("No product is selected. Can't define an appropriate context.");
case "Product 1":
context = new Product1Context();
break;
case "Product 2":
context = new Product2Context();
break;
case "Product 3":
context = new Product3Context();
break;
default:
throw new Exception("Current product is not supported. Can't define an appropriate context.");
}
return context;
}
And controllers are like this:
public class MyController : Controller
{
private CoreContext _context;
public MyController(CoreContext context)
{
_context = context;
}
...
}
So it seems I have to switch over to XML configuration if I'm going to remove ProductXContext references from my MVC project since in order to compile fluent code you must have references to all the code. But I have now idea of:
- how to declare the InjectionFactory in XML configuration.
- if I manage to declare it, how to instantiate ProductXContext inside my ContextFactory since I don't have reference to them anymore.
PS: Is there a way to register the external types using XML config and then declare the injectionFactory using fluent config?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要后期绑定支持而不直接引用您的 ProductXContext 项目,那么还有替代解决方案。 TecX 项目 包含一个增强的 Unity 配置引擎,允许您扫描程序集以查找某些类型的实现并自动注册它们。它还具有来自温莎城堡的称为类型化工厂的功能,可以自动生成像您的 ContextFactory 这样的工厂。
If you need support for late-binding without direct references to your ProductXContext projects there are alternative solutions. The TecX project contains an enhanced configuration engine for Unity that allows you to scan assemblies for implementations of certain types and registers them automatically. It also has a feature from Castle Windsor called Typed Factories that auto-generates factories like your ContextFactory.