使用 MvcSiteMapProvider + DI +存储库模式=存储库对象为空
在我的应用程序中,我有一个项目登记册。 要访问这些项目,网址为: /project/{url-project}
the value of `{url-project}` is defined in the registration of the project!
我使用 这个项目 生成路径,但由于“注册项目”是动态的,必须手动实现它,如下所示 这篇文章。
所以我这样做了:
public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
[Inject]
public IProjectRepository ProjectRepository { get; set; }
#region Overrides of DynamicNodeProviderBase
public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
// Build value
var returnValue = new List<DynamicNode>();
var listDB = (from p in ProjectRepository.Query()
select new { p.Name, p.ID });
// Create a node for each album
foreach (var project in listDB.ToList())
{
var node = new DynamicNode { Title = project.Name };
node.RouteValues.Add("id", project.ID);
returnValue.Add(node);
}
// Return
return returnValue;
}
#endregion
}
在我的 Mvc.sitemap
<mvcSiteMapNode title="Projetos" controller="Project" action="Index">
<mvcSiteMapNode title="Novo Projeto" controller="Project" action="Create" />
<mvcSiteMapNode title="Projeto" action="About" dynamicNodeProvider="BindSolution.Infra.ProjectDetailsDynamicNodeProvider, BindSolution" />
</mvcSiteMapNode>
问题
我的问题是 ProjectRepository
它始终为空!
kernel.Bind<IProjectRepository>().To<ProjectRepository>().InRequestScope();
但在我的设置中 Ninject 是正确的! 该存储库在控制器中使用并且运行完美!
我尝试通过构造函数
注入此依赖项,但结果是相同的!
public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
public IProjectRepository ProjectRepository { get; set; }
public ProjectDetailsDynamicNodeProvider(IProjectRepository projectRepository)
{
ProjectRepository = projectRepository
}
#region Overrides of DynamicNodeProviderBase
public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
// Build value
var returnValue = new List<DynamicNode>();
var listDB = (from p in ProjectRepository.Query()
select new { p.Name, p.ID });
// Create a node for each album
foreach (var project in listDB.ToList())
{
var node = new DynamicNode { Title = project.Name };
node.RouteValues.Add("id", project.ID);
returnValue.Add(node);
}
// Return
return returnValue;
}
#endregion
}
In my application I have a register of projects.
To access these projects is the url: /project/{url-project}
the value of `{url-project}` is defined in the registration of the project!
I use this project to generate the path, but as the "register project" is dynamic had to implement it manually as shows this article.
So I did:
public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
[Inject]
public IProjectRepository ProjectRepository { get; set; }
#region Overrides of DynamicNodeProviderBase
public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
// Build value
var returnValue = new List<DynamicNode>();
var listDB = (from p in ProjectRepository.Query()
select new { p.Name, p.ID });
// Create a node for each album
foreach (var project in listDB.ToList())
{
var node = new DynamicNode { Title = project.Name };
node.RouteValues.Add("id", project.ID);
returnValue.Add(node);
}
// Return
return returnValue;
}
#endregion
}
In my Mvc.sitemap
<mvcSiteMapNode title="Projetos" controller="Project" action="Index">
<mvcSiteMapNode title="Novo Projeto" controller="Project" action="Create" />
<mvcSiteMapNode title="Projeto" action="About" dynamicNodeProvider="BindSolution.Infra.ProjectDetailsDynamicNodeProvider, BindSolution" />
</mvcSiteMapNode>
Problem
My problem is ProjectRepository
which is always null!
kernel.Bind<IProjectRepository>().To<ProjectRepository>().InRequestScope();
But in my setup Ninject is correct!
This repository is used in the controller and it works perfectly!
Attemps
I tried to inject this dependency via the constructor but the result was the same!
public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
public IProjectRepository ProjectRepository { get; set; }
public ProjectDetailsDynamicNodeProvider(IProjectRepository projectRepository)
{
ProjectRepository = projectRepository
}
#region Overrides of DynamicNodeProviderBase
public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
// Build value
var returnValue = new List<DynamicNode>();
var listDB = (from p in ProjectRepository.Query()
select new { p.Name, p.ID });
// Create a node for each album
foreach (var project in listDB.ToList())
{
var node = new DynamicNode { Title = project.Name };
node.RouteValues.Add("id", project.ID);
returnValue.Add(node);
}
// Return
return returnValue;
}
#endregion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这个问题和你的问题很相似:
MVC3、Ninject、MvcSiteMapProvider - 如何注入依赖项以覆盖method
这意味着基本上你没有一个好的解决方案。
I believe that this question is similar to yours:
MVC3, Ninject, MvcSiteMapProvider - How to inject dependency to overridden method
Which means that basically you there isn't a good solution.