5

I have the code,

public class VendorManagementController : Controller
{
    private readonly IVendorRespository _vendorRespository;

    public VendorManagementController()
    {
        _vendorRespository = new VendorRespository();
    }

Now I want to use dependency injection. So the code will be

public class VendorManagementController : Controller
{
    private readonly IVendorRespository _vendorRespository;

    public VendorManagementController(IVendorRespository vendorRespositor)
    {
        _vendorRespository = vendorRespositor;
    }

My question is that I do not find the place where I can create VendorRespository object and how I can pass it to the VendorManagementController with defined parameterized VendorManagementController(IVendorRespository vendorRespositor) constructor?

5
  • Which DI container are you using ? Commented Jan 26, 2016 at 15:32
  • @IMU, I have not use any DI container. Should I? Commented Jan 26, 2016 at 15:34
  • Yes, for example you can use StructureMap, here is the doc and example of how to use, structuremap.github.io, ardalis.com/… Commented Jan 26, 2016 at 15:39
  • @IMU DI is built in with MVC6, you don't need a separate DI package Commented Jan 26, 2016 at 15:44
  • Cool, I didn't try MVC6. Thanks Commented Jan 26, 2016 at 15:48

2 Answers 2

3

In MVC6 Dependency Injection is part of the framework - so you don't need Unit, Ninject, etc.

Here's a tutorial: http://dotnetliberty.com/index.php/2015/10/15/asp-net-5-mvc6-dependency-injection-in-6-steps/

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I just find Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc(); }, but I don't know how to put there.
Have you followed the demo? It goes through all of the steps.
2

Dependency injection is baked into ASP.NET MVC 6. To use it you just need set up your dependencies in the ConfigureServices method of Startup.cs.

The code looks like this:

   public void ConfigureServices(IServiceCollection services)
   {

      // Other code here

      // Single instance in the current scope.  Create a copy of CoordService for this 
      // scope and then always return that instance
      services.AddScoped<CoordService>();

      // Create a new instance created every time the SingleUseClass is requested
      services.AddTransient<SingleUseClass>();

#if DEBUG
      // In debug mode resolve a call to IMailService to return DebugMailService
      services.AddScoped<IMailService, DebugMailService>();
#else
      // When not debugging resolve a call to IMailService to return the 
      // actual MailService rather than the debug version
      services.AddScoped<IMailService, MailService>();
#endif
    }

That example code shows a few things:

  • The lifecycle of the item you inject can be controlled with AddInstance, AddTransient, AddSingleton and AddScoped
  • A compile time #if can be used to inject a different object at debug time and runtime.

Lots more info in the official documentation for ASP.NET MVC 6 Dependency Injection, and also a good basic walk-through here.

2 Comments

My code is services.AddTransient<IVendorRespository>();. Is it right?
I think you would need to do services.AddTransient<IVendorRespository, VendorRespository>(). That would create a new instance everytime though, you might be better with AddScoped to create a single instance of the repo for the current scope, or even AddSingleton to create a single instance across the whole app. See more here: davidhayden.me/blog/asp-net-mvc-6-dependency-injection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.