I think in some cases you do not want to inject an instance into the constructor. I often add lazy objects or factory delegates. I will try to explain why you want to do this.
In some real-world scenarios, you can embed multiple instances into the constructor, and sometimes, depending on the application logic, only some of them are actually used during the life cycle of the object. This means that sometimes you initialize the dependency, even if it is not used.
In .NET 4, you can use the Lazy class, which allows you to instantiate the dependency class only when it is first used. This is great when dependency instances take a long time or an instance requires a lot of memory.
public class ClassA { private readonly Lazy<IClassB> _lazyClassBObj; public ClassA(Lazy<IClassB> lazyClassBObj) { _lazyClassBObj = lazyClassBObj; } public void UseClassBMethod() { _lazyClassBObj.Value.classBMethod(); } } class Program { static void Main(string[] args) { ClassA classA = new ClassA (new Lazy<IClassB>(() => new ClassB)); ... } }
Another trick is to insert a factory delegate into the constructor instead of the instance. It is very similar to Lazy's solution, but has several advantages. Implementing a factory delegate is cool if your class should be able to create any number of new instances of the dependency class (want to instantiate in a loop or something like that). In this example, ClassA will reference a method that can create an object that implements IClassB. To make things more interesting, ClassB, which implements IClassB, also has an IClassC dependency.
public class ClassA { private readonly Lazy<IClassC> _lazyClassBObj; private readonly Func<IClassC, IClassB> _classBObjFactory; public ClassA(Func<IClassC, IClassB> classBObjFactory, Lazy<IClassC> lazyClassBObj) { _classBObjFactory = classBObjFactory; _lazyClassBObj = lazyClassBObj; } public void UseClassBMethod() { var classC = _lazyClassBObj.Value; var classB1 = _classBObjFactory(classC);
The main advantage of this approach is to reduce the need for memory without initializing objects that you cannot use. The advantage of introducing a factory delegate is that you can hide the dependency initialization logic, and you can control which factory parameters are exposed to the caller. ClassA does not need to know how to collect all its dependencies, it only needs to know the parameters that it knows about, and it can control. This makes it easier to replace dependencies.
I hope this makes sense :) I just wanted to demonstrate how you can get more from the template using the C # functional style.