我知道在这个网站上有几个类似的主题,但我找不到一个有效的解决方案.我有一个名为'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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
要使类访问在另一个程序集中声明的类型,必须引用它们.如果您正在使用Ninject(我想),那么您已经对Ninject程序集执行了此操作.
即使Domain
是你的集合,在同一个解决方案中,你必须在它上面引用它WebUI
,否则它们就不会相互了解.
所以,让我们确保:
WebUI
项目Domain
项目重建,你很高兴去!
问候