我知道此站点上有几个类似的主题,但是找不到有效的解决方案。我有一个名为“ SportsStore”的解决方案,它包含3个项目,全部使用完整的.NET 4框架。这些项目名为“ SportsStore.Domain”,“ SportsStore.UnitTests”和“ SportsStore.WebUI”。
在“ SportsStore.WebUI”项目中,我创建了一个名为“ Infrastructure”的文件夹,其中有一个名为“ NinjectControllerFactory.cs”的类,其完整代码如下。注意顶部的最后一个“ using”语句:“ using 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);
}
}
}
最佳答案
为了使类访问在另一个程序集中声明的类型,必须引用它们。如果您正在使用Ninject(我想),那么您已经对Ninject程序集进行了此操作。
即使Domain
是您的程序集,在相同的解决方案上,您也必须在WebUI
上引用它,否则他们将彼此不认识。
因此,请确保:
右键单击您的WebUI
项目
选择添加参考
转到项目选项卡
选择Domain
项目
做完了!
重建,您一切顺利!
问候
关于c# - 类型或 namespace 名称不存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11569220/