【发布时间】:2016-06-09 07:45:38
【问题描述】:
我正在使用 StructureMap.MVC5 并且有以下项目和类:
Web
HomeController(PageService pageService)
Services
PageService(IPageRepository pageRepository)
Repositories
PageRepository : IPageRepository
IRepositories
IPageRepository
使用 StructureMap.MVC5 的默认实现,它会自动将 PageService 解析为我的 HomeController,但不会将 PageRepository 解析为我的 PageService。这给了我一个例外:No parameterless constructor defined for this object.
这可以通过在 DefaultRegistry 中添加一行来解决:
For<IPageRepository>().Use<PageRepository>();
但显然我宁愿让 StructureMap 自动解决这个问题。有没有办法做到这一点?
这是 DefaultRegistry 的样子:
public DefaultRegistry()
{
Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
}
【问题讨论】:
标签: asp.net .net asp.net-mvc dependency-injection structuremap