2012-07-19 158 views
1

我知道在这个网站上有几个类似的主题,但我找不到可行的解决方案。我有一个名为'SportsStore'的解决方案,它包含3个项目,全部使用完整的.NET 4框架。这些项目被命名为“SportsStore.Domain”,“SportsStore.UnitTests”和“SportsStore.WebUI”。类型或命名空间名称不存在

在'SportsStore.WebUI'项目中,我创建了一个名为'Infrastructure'的文件夹,其中我有一个名为'NinjectControllerFactory.cs'的类。下面是完整的代码。请注意最上面的'using'声明:'使用SportsStore.Domain.Abstract'。我的程序不会编译,它告诉我名称空间不存在。智能感知识别'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); 
    } 
} 
} 
+0

您是否在WebUI项目中添加了对域程序集的引用? – 2012-07-19 21:03:14

+0

安装resharper :) – Simon 2012-07-20 07:06:00

回答

5

为了一类以访问另一个组件声明的类型,你必须参考他们。如果你使用Ninject(我想),你已经对Ninject组件做了这个。

尽管Domain是您的组装件,但在相同的解决方案中,您必须在WebUI处引用它,否则它们将不会彼此了解。

所以,让我们确保:

  1. 右键单击您WebUI项目
  2. 选择添加引用
  3. 转到项目标签
  4. 选择Domain项目
  5. 完成!

重建和你很好去!

Regards

+0

优秀!!我不知道这个,并假设VS只是为你做了这个。是的,我必须先与Ninject和Moq做到这一点。非常感谢!我每天都会学到新的东西:) – 2012-07-19 21:16:33

+0

非常欢迎=) – 2012-07-19 21:17:14

相关问题