【问题标题】:Generic Repository Structuremap IOC question通用存储库结构图 IOC 问题
【发布时间】:2009-12-22 18:35:54
【问题描述】:

我是 IOC 和 strcturemap 的新手,但我想知道如何注册从同一接口实现的不同类,例如实现 CustomerRepository 和 CategoryRepository 的 IRepository。 而且,如您所见,如果我可以将它与泛型类型一起使用。 Like Repository 或 Repository

x.ForRequestedType<IRepository<Category>>().TheDefault.Is.OfConcreteType<IRepository<Category>>();

试过这个没用..任何帮助都值得赞赏!

【问题讨论】:

  • 我不知道 StructureMap 究竟是如何工作的,但我会写Is.OfConcreteType&lt;Repository&lt;Category&gt;&gt;'. Repository, Repository, Repository, not IRepository`。

标签: structuremap ioc-container


【解决方案1】:

在配置 StructureMap 时,您可以使用 Scan API 自动注册封闭类型。

Scan(x =>
{
  x.TheCallingAssembly();
  x.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
});

这个功能在最新的trunk中,但我不确定它是否是任何发布版本,并且可能还没有在官方文档中。不过,有一个很好的blog post that explains it

【讨论】:

    【解决方案2】:

    没有为 PluginFamily MVCPoco.Data.IRepository 定义默认实例`1[[MVCPoco.Core.Category, MVCPoco.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], MVCPoco.Data, Version= 1.0.0.0,文化=中性,PublicKeyToken=null

    得到了最新版本...项目运行但是当我在构造函数中注入一个 IRepository 它给了我这个错误。

    【讨论】:

    • 我需要注册ir吗?存储库的名称是 Repository.cs...我不知道我做错了什么!
    • 我遇到了同样的问题,确保它找不到的类是公开的。
    猜你喜欢
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多