Problem
I have some problems to understand when and where exactly in my code I should use dependency injectors like Ninject.
Code
Let's say for example we have the following code:
//WITHOUT NINJECT:
IMailSender mailSender = new MockMailSender();
//WITH NINJECT:
IMailSender mailSender = kernel.Get<IMailSender>();
This one is not a dependency injection so does it make sense to use Ninject in this scenario?
Another example shows how my code gets really messed up by using a dependency injector:
public void CalculateRevenueRecognitions(IContract contract)
{
//WITH NINJECT
var kernel = new StandardKernel(new DefaultModule());
var arguments = new List<IParameter>
{
new ConstructorArgument("amount",contract.Revenue),
new ConstructorArgument("date", contract.WhenSigned)
};
contract.AddRevenueRecognition(kernel.Get<IRevenueRecognition>(arguments.ToArray()));
//WITHOUT NINJECT:
contract.AddRevenueRecognition(new RevenueRecognition(contract.Revenue, contract.WhenSigned))));
}
Question
When should I use a dependency injector?
- on constructor injections, parameter injection, etc.
- on object creation (do dependency injectors replace classical object creation with new?)
- are the others?
When shouldn't I use a dependency injectors?