2012-07-19 38 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

相關問題