简体   繁体   English

使用structuremap解决对存储库接口MVC5的依赖性

[英]Resolve Dependency on a repository interface MVC5 using structuremap

I have a mapper that takes data from a repository project. 我有一个从存储库项目中获取数据的映射器。 I have a IMenueMapper interface that is passed into the homecontroller like this: 我有一个IMenueMapper接口传递到homecontroller,如下所示:

public HomeController(IMenueMapper menueMapper)
{
    _menueMapper = menueMapper;
}

but the menuemapper class itself use the IMenueMapperRepository , and this come from another project and is passed in via dll 但menuemapper类本身使用IMenueMapperRepository ,这来自另一个项目并通过dll传入

public MenueMapper(IMenueItemsRepository menueItems)
{
    _menueItems = menueItems;
}

While I can easily resolve the IMenuemapper in the MVC project, using structuremap.mvc5, I can't resolve the repository. 虽然我可以使用structuremap.mvc5轻松解决MVC项目中的IMenuemapper,但我无法解析存储库。 Is there a way of achieving the DI in this instance? 在这种情况下,有没有办法实现DI?

You need to register the abstraction (repository interface and implementation) in the composition root. 您需要在组合根中注册抽象(存储库接口和实现)。

You indicated that the IMenueMapper is registered via; 您表明IMenueMapper是通过注册的;

scan.AssemblyContainingType<MenueMapper>();

Since 以来

but the MenueMapper class itself use the IMenueMapperRepository , and this come from another project and is passed in via dll 但是MenueMapper类本身使用的是IMenueMapperRepository ,它来自另一个项目并通过dll传入

Then it should also be scanned as it belongs to another assembly 然后它也应该被扫描,因为它属于另一个组件

scan.AssemblyContainingType<MenueItemsRepository>();

Make sure that the project references the assembly in question 确保项目引用了相关的程序集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM