Reputation: 127
I know there are several similar topics like this on this site, but I can't find a solution that works. I have a solution called 'SportsStore' and it contains 3 projects, ALL that use the full .NET 4 framework. The projects are named 'SportsStore.Domain', 'SportsStore.UnitTests' and 'SportsStore.WebUI'.
Within the 'SportsStore.WebUI' project, I created a folder called 'Infrastructure' and in it I have a class called 'NinjectControllerFactory.cs' The complete code for it is below. Note the last 'using' statement at the top: 'using SportsStore.Domain.Abstract'. My program will NOT compile and it tells me the namespace does not exist. Intellisense recognizes 'SportsStore' but only says 'WebUI' is my next option. It will not recognize 'SportStore.Domain' at all. I have tried cleaning, rebuilding, closing, opening, rebooting, changing frameworks back to Client for all projects and then back to Full, and nothing seems to work.
Bottom line is I'm trying to get access to my IProductRepository.cs repository file which is part of the SportsStore.Domain.Abstract namespace in the 'SportsStore.Domain' project.
I'm hoping this is something easy to correct? Thanks in advance!
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);
}
}
}
Upvotes: 0
Views: 9632
Reputation: 7692
In order for a class to access a type declared in another assembly, you must reference them. If you are using Ninject (I suppose), you have did this to the Ninject assembly.
Even though Domain
is an assembly of yours, on the same solution, you must reference it at the WebUI
, otherwise they won't know each other.
So, let's make sure:
WebUI
projectDomain
projectRebuild and you're good to go!
Regards
Upvotes: 4