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