Autofac — automatic injection into the controller?

Dig examples, articles — can not understand.


Here we have the Autofac registration (functional primitive, purely to learn).

public interface ISiteInfo
{
 string GetReffer(Controller controller);
}

 public class SiteInfo : ISiteInfo
{
 public string GetReffer(Controller controller)
{
 if (controller.Request.UrlReferrer != null)
{
 return controller.Request.UrlReferrer.ToString();
}
 return "";
}
}

 public class MvcApplication : System.Web.HttpApplication
{
 protected void Application_Start()
{
 var builder = new ContainerBuilder();
 builder.RegisterControllers(typeof (MvcApplication).Assembly);
 builder.RegisterType<SiteInfo>(); 
 builder.Register<ISiteInfo>(x => x.Resolve<SiteInfo>());
 var container = builder.Build();
 DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
[...]
}
 }



Is. Great. But how to use the auto-create instance of a class?


In ninject it would look like this:

public class QuestsListController : Controller
{
[Inject]
 public ISiteInfo InfoObject { get; set; }
[...]
 }



But such a scenario is as beautiful to implement autofac I do not understand — you have to use this.

public class QuestsListController : Controller
{
 public ISiteInfo InfoObject { get; set; }

 public QuestsListController(SiteInfo _InfoObject)
{
 InfoObject = _InfoObject;
}
[...]
 }



That is beautiful

How to deal with such things? Or I just don't understand something?


Still catches my tests Ninject speed much inferior to the rest.
October 3rd 19 at 02:56
1 answer
October 3rd 19 at 02:58
I can only advise to look towards Unity (Microsoft Enterprise Library) [InjectionConstructor]. When You get to test your classes, You'll understand why dependency injection in the constructor is the best solution.
I tried and got me there, indeed, passing through the constructor — more convenient :) - corbin_Barrows17 commented on October 3rd 19 at 03:01

Find more questions by tags .NETASP.NETC#Patterns of designing