1

我知道这个网站上有几个类似的主题,但我找不到有效的解决方案。我有一个名为“SportsStore”的解决方案,它包含 3 个项目,所有项目都使用完整的 .NET 4 框架。这些项目被命名为“SportsStore.Domain”、“SportsStore.UnitTests”和“SportsStore.WebUI”。

在“SportsStore.WebUI”项目中,我创建了一个名为“Infrastructure”的文件夹,其中有一个名为“NinjectControllerFactory.cs”的类。它的完整代码如下。请注意顶部的最后一个“使用”语句:“使用 SportsStore.Domain.Abstract”。我的程序不会编译,它告诉我命名空间不存在。Intellisense 识别“SportsStore”,但只说“WebUI”是我的下一个选择。它根本无法识别“SportStore.Domain”。我已经尝试清理、重建、关闭、打开、重新启动、将所有项目的框架更改回客户端,然后返回完整,但似乎没有任何效果。

底线是我正在尝试访问我的 IProductRepository.cs 存储库文件,该文件是“SportsStore.Domain”项目中 SportsStore.Domain.Abstract 命名空间的一部分。

我希望这很容易纠正?提前致谢!

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using System.Linq;
using Ninject;
using Moq;
using SportsStore.Domain.Abstract;

//To begin a project that uses Ninject and/or Moq (mocking data) you
//need to add reference to them.  Easiest way is to select 'View', then
//'Other Windows', and 'Package Manager Console'.  Enter the commands:
//Install-Package Ninject -Project SportsStore.WebUI
//Install-Package Ninject -Project SportsStore.UnitTests
//Install-Package Moq -Project SportsStore.UnitTests

//We placed this file in a new folder called 'Infrastructure' within the 
//'SportsStore.WebUI' project.  This is a standard way to define what we
//need to do with Ninject since we are going to use Ninject to create our
//MVC application controllers and handle the dependency injection (DI).
//To do this, we need to create a new class and make a configuration
//change.

//Finally we need to tell MVC that we want to use this class to create
//controller objects, which we do by adding a statement to the 
//'Global.asax.cs' file in this 'SportsStore.WebUI' project.

namespace SportsStore.WebUI.Infrastructure
{
public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return controllerType == null
            ? null
            : (IController)ninjectKernel.Get(controllerType);
    }

    private void AddBindings()
    {
        //During development, you may not want to hook your IProductRepository to
        //live data yet, so here we can create a mock implementation of the data
        //and bind it to the repository.  This is MOQ in work - great tool to allow
        //you to develop real code and make it think it's using the live data.  This
        //uses the 'System.Linq' namespace
        Mock<IProductRepository> mock = new Mock<IProductRepository>();
        mock.Setup(m => m.Products).Returns(new List<Product>
        {
            new Product { Name = "Football", Price = 25 },
            new Product { Name = "Surf board", Price = 179 },
            new Product { Name = "Running shoes", Price = 95 }
        }.AsQueryable());
        ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
    }
 }
}
4

1 回答 1

5

为了让一个类访问在另一个程序集中声明的类型,您必须引用它们。如果您使用的是 Ninject(我想),那么您已经对 Ninject 程序集执行了此操作。

即使Domain是您的程序集,在同一个解决方案上,您也必须在 处引用它WebUI,否则它们将不会互相认识。

所以,让我们确保:

  1. 右键单击您的WebUI项目
  2. 选择添加参考
  3. 转到项目选项卡
  4. 选择Domain项目
  5. 完毕!

重建,你很高兴!

问候

于 2012-07-19T21:13:56.860 回答