Stuart Hemming
Stuart Hemming

Reputation: 1663

Why are my Dependencies Not Injected?

I have a simple MVC5 App. I want to inject a repository in to one of my Controllers.

The Controller looks like this ...

public class HomeController : Controller
{

    IToDoRepository repo;

    public HomeController(IToDoRepository repo)
    {
        this.repo = repo;
    }

    // Methods ...
}

My Repository looks like this...

public class ToDoRepository : IToDoRepository
{
    ToDoListData data;

    public ToDoRepository(ToDoListData dataSource)
    {
        this.data = dataSource;
    }

    // Blah...
}

And ToDoListData is just a dummy data source defined like this ...

public class ToDoListData : List<ToDoItem>
{
    public ToDoListData()
    {
        Add(new ToDoItem { Id = 1, Title = "Do Shopping" });
        Add(new ToDoItem { Id = 2, Title = "Feed Dogs", Complete = true });
        Add(new ToDoItem { Id = 3, Title = "Pay Gas Bill" });
    }
}

Using nuget, I've added the Unity.Mvc5 package. The total list of packages installed in the project is...

<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net45" />
  <package id="bootstrap" version="3.0.0" targetFramework="net45" />
  <package id="jQuery" version="1.10.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Modernizr" version="2.6.2" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="5.0.6" targetFramework="net45" />
  <package id="Respond" version="1.2.0" targetFramework="net45" />
  <package id="Unity" version="3.0.1304.1" targetFramework="net45" />
  <package id="Unity.Mvc5" version="1.1" targetFramework="net45" />
  <package id="Unity.WebAPI" version="5.1" targetFramework="net45" />
  <package id="WebGrease" version="1.5.2" targetFramework="net45" />
</packages>

I updated Global.asax to call UnityConfig.RegisterComponents() and modified that method to look like this ...

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        container.RegisterType<IToDoRepository, ToDoRepository>(
            new ContainerControlledLifetimeManager());

        container.RegisterType<ToDoListData, ToDoListData>(
            new ContainerControlledLifetimeManager());

        GlobalConfiguration.Configuration.DependencyResolver = 
            new UnityDependencyResolver(container);

    }
}

Now, I'm relatively new to this, but what I've read suggests (or at least my addled brain seems to think it suggests) that this should run.

However.

Firing up the site returns..

System.MissingMethodException: No parameterless constructor defined for this object.

Can anyone suggest what I might have missed?

Upvotes: 0

Views: 180

Answers (2)

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

Read the error: No parameterless constructor defined for this object. I don't see ToDoList, but assume it is parameterless, so that would not be the issue. The issue then is the ToDoRepository has a parameter, but is not supplied. And Unity is not fixing the issue by supplying it.

@Qamar appears to have a fix. You can also make sure the ToDoList dependency is resolved and pass to the resolved ToDoRepository in your code. It depends on how much you want to wire the plumbing and how much magic.

I like @Qamar's tip about Bootstrapper. Need to look into it.

Upvotes: 0

qamar
qamar

Reputation: 1447

I think below is the right way to overwrite a dependency resolver.

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

I suggest you use mvc unity bootstrapper instead of unity package from nuget. Bootsrapper comes with basic set up.

Upvotes: 2

Related Questions