If your base class is independent of ILogger , you must remove the property from the base class. This way you keep the base class constructor clean.
Otherwise, you can create a factory that can create descendants of MyBaseClass . It might look like this:
public interface IMyBaseClassFactory { MyBaseClass CreateNew<TMyBaseClass>() where TMyBaseClass : MyBaseClass; }
Now you can create a register that implements IMyBaseClassFactory , which can create new instances and register optional dependencies:
public MyBaseClassFactoryImpl : IMyBaseClassFactory { public MyBaseClass CreateNew<TMyBaseClass>() {
Most IoC containers allow you to decorate injection properties with attributes. The advantage of using factory is that your application will remain completely unfamiliar with the IoC map used. As a disadvantage, work remains to be done (creating a factory by entering factory instead of the instance itself and calling factory to create a new instance).
When defining an injectable property, you need to make it read / write. Of course, you do not want anyone to accidentally reset this property afterwards. Without a constructor installation, this is difficult to do with compile-time support. However, checking the runtime is easy:
private ILogger logger; public ILogger Logger { get { return this.logger; } set { if (this.logger != null) { throw new InvalidOperationException("Logger has already been set."); } this.logger = value; } }
Hope this helps.
Steven
source share