我知道在這個網站上有幾個類似的主題,但我找不到可行的解決方案。我有一個名爲'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);
}
}
}
您是否在WebUI項目中添加了對域程序集的引用? – 2012-07-19 21:03:14
安裝resharper :) – Simon 2012-07-20 07:06:00