解决 Open Generic 依赖关系时 StructureMap 错误

发布于 2024-10-19 07:11:32 字数 450 浏览 6 评论 0原文

所以我已经像这样在 StructureMap 中连接了我的开放通用插件

scan.ConnectImplementationsToTypesClosing(typeof(IRepository<>));

但仍然会遇到可怕的情况

没有为 PluginFamily KharaSoft.Utils.IRepository`1[[KharaSoft.App.Core.DomainObject, KharaSoft.App.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] 定义默认实例

我在初始化容器后进行调试并看到它确实有一个 RepositoryBase<> 实例注册所以它知道我想要做什么,但它不会为我关闭它。我在这里缺少什么吗?

So I've wired up my open generic plugin in StructureMap like so

scan.ConnectImplementationsToTypesClosing(typeof(IRepository<>));

But still get the dreaded

No Default Instance defined for PluginFamily KharaSoft.Utils.IRepository`1[[KharaSoft.App.Core.DomainObject, KharaSoft.App.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]

I debug after the Container is initialized and see that it does indeed have an instance of RepositoryBase<> registered so it knows what I want done, but it won't close it for me. Is there something I'm missing here?

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

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

发布评论

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

评论(2

浅忆 2024-10-26 07:11:32

如果没有看到完整的扫描代码或项目布局,就很难进行锻炼。遇到此问题时,我通常会执行一些默认步骤。

确保您已

scan.WithDefaultConventions()

确保包含存储库类的程序集包含在扫描中:

x.AssemblyContainingType(typeof(UserRepository)); 

确保您有正确的实现:

IRepository<User>

具有匹配

Repository<User>

希望此建议中的某些内容可以帮助您找到问题。

It's hard to workout without seeing the full Scan code or your project layout. There are a few default steps I normally go through when I have this issue.

Ensure you have

scan.WithDefaultConventions()

Ensure that the assembly containing the Repository classes is included in the scan:

x.AssemblyContainingType(typeof(UserRepository)); 

Ensure that you have the correct implementations in place:

IRepository<User>

has matching

Repository<User>

Hopefully something amongst this advice might help you find the issue.

慢慢从新开始 2024-10-26 07:11:32

所以我不确定这是否是“最好”的方法,但这是我发现有效的方法。我必须像这样显式注册插件的开放实现:

ObjectFactory.Initialize(
  x =>
    {
      x.Scan(scan =>
        {
          scan.Assembly(typeof (IRepository<>).Assembly);
          scan.WithDefaultConventions();
        });
      x.For(typeof (IRepository<>)).Use(typeof (RepositoryBase<>));
      x.For<IUnitOfWork>().Use<MyDataContext>();
    });
return ObjectFactory.Container;

请参阅我不想在所有情况下直接关闭通用。所以现在我的 MVC 控制器可以采用这样的依赖关系

public PlayerController(IRepository<Player> players)
{
  Players = players;
}

并且 StructureMap 将使用 RepositoryBase 的实例关闭依赖关系

So I'm not sure if this is the "best" way but this is what I found that works. I had to explicitly register the open implementation of the plugin like this:

ObjectFactory.Initialize(
  x =>
    {
      x.Scan(scan =>
        {
          scan.Assembly(typeof (IRepository<>).Assembly);
          scan.WithDefaultConventions();
        });
      x.For(typeof (IRepository<>)).Use(typeof (RepositoryBase<>));
      x.For<IUnitOfWork>().Use<MyDataContext>();
    });
return ObjectFactory.Container;

See I didn't want to close the generic directly in all cases. So now my MVC controller can take a dependency like so

public PlayerController(IRepository<Player> players)
{
  Players = players;
}

And StructureMap will close the dependency with an instance of RepositoryBase

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