Consider a situation where you have some controller that uses some service which is injected by some IOC container like autofac or ninject. Service is initialized each time a request hits that controller, even if it just wants to trigger simple action that only calls a view. The question here is how to defer initialization for a service for when it's really needed so I can save some resources. Should I use here Lazy? Or perhaps should I inject this services into method instead of controller?
Example:
public class SomeController : Controller
{
private readonly ISomeService _service;
public SomeController(ISomeService service)
{
this._service = service;
}
// action that completely doesn't need to initialize service
public ActionResult Index()
{
return View();
}
// this action needs to initialize
public ActionResult Save()
{
this._service.DoSomething();
return View();
}
}