Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection (DI) in ASP.Net MVC 6

i was reading a write up on easily dependency injection in ASP.Net MVC 6 from this url http://weblogs.asp.net/scottgu/introducing-asp-net-5

they show how very easily we can inject dependency into project

1st one

namespace WebApplication1
{
    public class TimeService
    {
        public TimeService()
        {
            Ticks = DateTime.Now.Ticks.ToString();
        }
        public String Ticks { get; set; }
    }
}


register the time service as a transient service in the ConfigureServices method of the Startup class:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddTransient<TimeService>();
    }


public class HomeController : Controller
{

    public TimeService TimeService { get; set; }

    public HomeController(TimeService timeService)
    {
        TimeService = timeService;
    }


    public IActionResult About()
    {
        ViewBag.Message = TimeService.Ticks + " From Controller";
        System.Threading.Thread.Sleep(1);
        return View();
    }
}

2nd one

public class HomeController : Controller
{
    [Activate]
    public TimeService TimeService { get; set; }
}

now see the second code. are they trying to say that if we use [Activate] attribute then we do not have to instantiate TimeService by controller constructor injection?

just tell me if we use [Activate] attribute then what will be the advantage ?

if we use [Activate] attribute then what line of code we can eliminate from 1st same code. thanks

like image 887
Mou Avatar asked Feb 24 '15 13:02

Mou


People also ask

What is dependency injection in ASP.NET MVC?

The Dependency Injection pattern is a particular implementation of Inversion of Control. Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source (for example, an xml configuration file).

What is difference between Addtransient and Addscoped and Addsingleton?

Singleton is a single instance for the lifetime of the application domain. Scoped is a single instance for the duration of the scoped request, which means per HTTP request in ASP.NET. Transient is a single instance per code request.

What is dependency injection DI )? What are the types of DI?

A class is no longer responsible for creating the objects it requires, and it does not have to delegate instantiation to a factory object as in the Abstract Factory design pattern. There are three types of dependency injection — constructor injection, method injection, and property injection.

Why DI is required?

The dependency injection technique enables you to improve this even further. It provides a way to separate the creation of an object from its usage. By doing that, you can replace a dependency without changing any code and it also reduces the boilerplate code in your business logic.


1 Answers

The differences between the two code blocks are indeed that the first one leverages Constructor Injection to resolve the dependency on TimeService, while the second example marks a property as one that needs resolving using Property Injection.

What this means is simply that the following constructor becomes redundant:

public HomeController(TimeService timeService)
{
    TimeService = timeService;
}

As to why one would opt for Constructor versus Property Injection, I find that trying to have a list of your dependencies clearly listed out in your constructor highlights when a class becomes too dependent, which raises concerns as to what a class is trying to accomplish and, subsequently, makes it a candidate for refactoring.

Property Injection via [Activate] will not be supported from beta5 onwards.

like image 132
Yannick Meeus Avatar answered Oct 19 '22 03:10

Yannick Meeus