I’ve been relying on IoC pattern for 1 year now using structuremap & asp.net mvc. IoC is really neat especially if you have a lot of dependencies in your class, just inject dependency in your constructor and your done, the IoC will auto-inject it for you.
My question is that im using asp.net mvc with a controller class which have IServices dependencies.
I need the dependency services only for a certain controller action let say “/customer/add”
but i don’t need those dependency on other action let say “/Customer/Index”. However, since i’m using a DI (Dependency injection) in constructor, the dependency are always instantiated even if i don’t need them. Is that any good? object creation is expensive and consume memory footprint.
Of course, i can do container.GetInstance inside an action, but it is not a good practice since you will heavily be dependent on IoC in your code and unit testing.
Any suggestion? correct me if im wrong with something.
Updated in response to jgauffins comment
I see two straightforward ways to solve your issue:
Inject a service factory to your constructor and create the service on demand.
Inject a service proxy to your constructor and let the proxy create the real service on demand.