I have an asp.net-mvc site and I am using LinFu to do IOC. I ran into an issue where a number of actions have a dependency that I want to inject into the controller but i only want to initialize the dependency if i call the action that depends on it.
so in my controller I have this code in my controller:
public PersonController
{
private IPeopleImporter _peopleImporter;
public override void Initialize(LinFu.IoC.Interfaces.IServiceContainer source)
{
_peopleImporter= source.GetService<IPeopleImporter>();
base.Initialize(source);
}
public JsonResult GetDetails(int id)
{
var p = _peopleImporter.Get(id);
var personDetails = new {p.Id, p.FirstName, p.LastName, StandardId = p.StandardIdLogin, p.PersonNumber};
return Json(personDetails);
}
}
to initiatize PeopleImporter is pretty expensive so my issue is that I want to solve two things:
I want to make the implementatioo of IPeopleImporter "pluggable" so I can IOC in the interface into the controller
I have a lot of actions so I don't want the cost of initiatizing the IPeopleImporter if the user never calls the specific action that needs it. It seems like in the code above I do that initiatization on every call of PersonController
My initiatization code is like this:
this.AddService(typeof(IPeopleImporter), typeof(DatabaseImporter), LifecycleType.Singleton);
This seems like common pattern / issue. Is there a recommended solution. in the meantime, the alternative (to avoid the performance hit is to simple "new" up the concete implmentation inside the controller (and avoid IOC) ?