使用 MvcSiteMapProvider + DI +存储库模式=存储库对象为空

发布于 2024-12-22 21:23:50 字数 2947 浏览 2 评论 0原文

在我的应用程序中,我有一个项目登记册。 要访问这些项目,网址为: /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 技术交流群。

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

发布评论

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

评论(1

落墨 2024-12-29 21:23:50

我相信这个问题和你的问题很相似:
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.

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