title: Dependency injection (DI) in ASP.Net MVC 6
body: <p>i was reading a write up on easily dependency injection in ASP.Net MVC 6 from this url <strong><a href="http://weblogs.asp.net/scottgu/introducing-asp-net-5">http://weblogs.asp.net/scottgu/introducing-asp-net-5</a></strong></p>
  
  <p>they show how very easily we can inject dependency into project</p>
  
  <h2>1st one</h2>
  
  <pre><code>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&lt;TimeService&gt;();
      }
  
  
  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();
      }
  }
  </code></pre>
  
  <h2>2nd one</h2>
  
  <pre><code>public class HomeController : Controller
  {
      [Activate]
      public TimeService TimeService { get; set; }
  }
  </code></pre>
  
  <p>now see the second code. are they trying to say that if we use <code>[Activate]</code> attribute then we do not have to instantiate <code>TimeService</code> by controller constructor injection?</p>
  
  <p>just tell me if we use <code>[Activate]</code> attribute then what will be the advantage ?</p>
  
  <p>if we use <code>[Activate]</code> attribute then what line of code we can eliminate from 1st same code. thanks</p>