0

I foolishly decided to try something new on a Friday job!

So I have used NuGet to add Ninject.Web.Mvc 2.2.x.x to my .Net MVC2 project.

I've altered my Global.asax.cs

using System.Web.Mvc;
using System.Web.Routing;
using IntegraRecipients;
using Mailer;
using Ninject;
using Ninject.Web.Mvc;
using Ninject.Modules;

namespace WebMailer
{

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("favicon.ico");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Mail", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new INinjectModule[] { new MailModule()});
    }

    internal class MailModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IMailing>().To<Mailing>();
            Bind<IMailingContext>().To<MailingContext>();
            Bind<IRecipientContext>().To<RecipientContext>();
        }
    }
}

}

and I've created a controller like so...

using System.Linq;
using System.Web.Mvc;
using WebMailer.Models;

namespace WebMailer.Controllers
{
    [ValidateInput(false)]
    public class MailController : Controller
    {
        private readonly IMailingContext _mailContext;
        private readonly IRecipientContext _integraContext;

        public MailController(IMailingContext mail,IRecipientContext integra)
        {
            _mailContext = mail;
            _integraContext = integra;
        }

        public ActionResult Index()
        {
            return View(_mailContext.GetAllMailings().Select(mailing => new MailingViewModel(mailing)).ToList());
        }
    }
}

But the controller is still insisting that

The type or namespace name 'IRecipientContext' could not be found (are you missing a using directive or an assembly reference?)

and

The type or namespace name 'IMailingContext' could not be found (are you missing a using directive or an assembly reference?)

My google-fu has failed me and I really hope this is just a silly typo/missing line thing

Thanks in advance

P

Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96

2 Answers2

1

Ninject does not change the way assemblies are compiled! It deos not magically add references to other assemblies or add using directives. If you are using interfaces from other assemblies you have to add a using directive and a reference to this assembly.

All Ninject is about is to wire up your application at runtime.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • The two contexts are each in their own class libraries. I've got unit tests covering them so they work in and of themselves... And if I just drop the using references into the controller it all works. – Paul D'Ambra Mar 25 '11 at 20:58
  • Just created a new MVC2 application in a fresh solution, added a class library with a IService/Service that just return a string. Added Ninject the same as above and the controller is still demanding I add the using blah; – Paul D'Ambra Mar 25 '11 at 21:49
  • Seems I missunderstood your question. You should mention that it is a compiler error. See updated answer – Remo Gloor Mar 26 '11 at 23:43
  • That was my original guess but then I see questions like this one... http://stackoverflow.com/questions/4019585/how-to-use-ninject-conventions-extension-without-referencing-assembly-or-types-w – Paul D'Ambra Mar 27 '11 at 08:44
  • You need a reference to your dependencies (interface) so that they are known by the compiler. But you don't necessarily need a reference to the implementation of the dependencies. – Remo Gloor Mar 28 '11 at 06:43
  • So is it better practice to have the interface in the class library but the implementation of that in the... wait this feels like a new question – Paul D'Ambra Mar 28 '11 at 10:18
  • @Paul D'Ambra: Go search for Onion Architecture or ASP.NET MVC2 in action for a good description. Also the ninject wiki covers most of the ground in this – Ruben Bartelink Mar 28 '11 at 23:07
0

I am have what appears to be a similar problem.

I have a simple WPF Window project with the compiled Ninject.dll linked in. However, the following is giving me errors...

using Ninject;
namespace CatalogueManager
{
  public class ServiceLocator
  {
    public IMainWindowViewModel GetMainWindowViewModel()
    {
      return Kernel.Get<IMainWindowViewModel>();
    }

    static IKernel Kernel;
    static ServiceLocator()
    {
      Kernel = new StandardKernel(new NinjectConfiguration());
    }
  }
}

In particular, "Ninject" namespace and IKernel are prompting the compile time message "type or name space 'X' not found..."

George
  • 1
  • Hi George, this may well be a different question. They key difference is that my solution wasn't resolving *my* class libraries whereas yours isn't resolving ninject class libraries. I'd make sure they are referenced and available and check if you need a WPF specific ninject reference but I don't write WPF so am probably not the best person to answer... you're better off asking a new question if you still have the problem. – Paul D'Ambra Apr 11 '11 at 08:42