使用配置文件进行 Unity 依赖注入 - 无法解析依赖关系

发布于 2024-09-30 03:04:30 字数 1767 浏览 1 评论 0原文

我正在使用我的 app.config 告诉 Unity 我的接口类型映射...

<unity>
<containers>
  <container>
    <types>
      <type type="UnityDAL.Interfaces.IDataContextFactory, UnityDAL"
         mapTo="UnityDAL.UnityDemoDataContextFactory, UnityDAL" />
      <type type="UnityDAL.Interfaces.IProductRepository, UnityDAL"
         mapTo="UnityDAL.ProductRepository, UnityDAL" />
      <type name="productRepo" 
         type="UnityDAL.Interfaces.IProductRepository, UnityDAL"
         mapTo="UnityDAL.ProductRepository, UnityDAL" />

   and so on...

使用此代码

var wrapper = UnityWrapper.Create();
var productRepository = 
    wrapper.Container.Resolve<IProductRepository>("productRepo");
var productsBO = new ProductBO(productRepository);
var products = productsBO.GetAllProducts();

这是包装器对象的构造函数...

public UnityWrapper()
{
    _container = new UnityContainer();
    var section = 
        (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
    section.Containers.Default.Configure(_container);
}

但我收到一个异常,上面写着...

{"Resolution of the dependency failed ,类型=“IProductRepository”, 名称=“productRepo”。异常消息是:当前构建操作 (构建密钥构建密钥[UnityDAL.ProductRepository,productRepo])失败: 尝试时无法解析参数 dataContextFactory 调用构造函数 UnityDAL.ProductRepository(UnityDAL.Interfaces. IDataContextFactory(数据上下文工厂)。 (策略类型 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy,索引 2)"}

我认为这个节点正在连接它

<type type="UnityDAL.Interfaces.IDataContextFactory, UnityDAL"
   mapTo="UnityDAL.UnityDemoDataContextFactory, UnityDAL" />

。这里的想法最初是创建一个很好的依赖链。知道我做错了什么吗?如果您有任何建议或提示我如何解决这个问题,我想听听他们的帮助,

干杯,

圣地亚哥。

I'm using my app.config to tell Unity my interface to type mappings...

<unity>
<containers>
  <container>
    <types>
      <type type="UnityDAL.Interfaces.IDataContextFactory, UnityDAL"
         mapTo="UnityDAL.UnityDemoDataContextFactory, UnityDAL" />
      <type type="UnityDAL.Interfaces.IProductRepository, UnityDAL"
         mapTo="UnityDAL.ProductRepository, UnityDAL" />
      <type name="productRepo" 
         type="UnityDAL.Interfaces.IProductRepository, UnityDAL"
         mapTo="UnityDAL.ProductRepository, UnityDAL" />

   and so on...

using this code

var wrapper = UnityWrapper.Create();
var productRepository = 
    wrapper.Container.Resolve<IProductRepository>("productRepo");
var productsBO = new ProductBO(productRepository);
var products = productsBO.GetAllProducts();

Here is the constructor for the wrapper object...

public UnityWrapper()
{
    _container = new UnityContainer();
    var section = 
        (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
    section.Containers.Default.Configure(_container);
}

but I get an exception that says...

{"Resolution of the dependency failed, type = \"IProductRepository\",
name = \"productRepo\". Exception message is: The current build operation
(build key Build Key[UnityDAL.ProductRepository, productRepo]) failed:
The parameter dataContextFactory could not be resolved when attempting
to call constructor UnityDAL.ProductRepository(UnityDAL.Interfaces.
IDataContextFactory dataContextFactory). (Strategy type
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)"}

I thought this node was wiring that up

<type type="UnityDAL.Interfaces.IDataContextFactory, UnityDAL"
   mapTo="UnityDAL.UnityDemoDataContextFactory, UnityDAL" />

The idea here was originally to create a nice dependency chain. Any idea what I'm doing wrong? If you have any advice or tips on how I can correct the problem, I would like to hear them. Thanks for any help.

Cheers,

~ck in San Diego

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-10-07 03:04:30

这是一个映射问题。

尝试解析UnityDAL.Interfaces.IProductRepository,您需要首先解析UnityDAL.Interfaces.IDataContextFactory。接下来尝试解析 UnityDAL.UnityDemoDataContextFactory 你会错过一些映射。可能 UnityDAL.UnityDemoDataContextFactory 的 ctor 需要尚未注册的东西。

顺便说一句:你在这里所做的是使用
一个服务地点。我避免这个
如果可能的话进行练习,但是如果你
绝对需要这个然后尝试
公开公共服务
定位器
。该 dll 随 Unity 一起提供
并提供简单的服务定位器
只有接口。

This is a mapping problem.

Trying to resolve the UnityDAL.Interfaces.IProductRepository you need to first resolve UnityDAL.Interfaces.IDataContextFactory. Next trying to resolve UnityDAL.UnityDemoDataContextFactory you miss some mapping. Probably the ctor of the UnityDAL.UnityDemoDataContextFactory requires something that has being not registered.

By the way: what you do here is using
a service location. I avoid this
practice if possible but if you
absolutely need this then try to
expose the common service
locator
. This dll ships with Unity
and provides a simple service locator
ONLY interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文