I have a base Class Base having dependecy Dep and default and Injection Constructor-
Class Base : IBase
{
public IDep Dep { get; set; }
public Base()
{
Console.WriteLine("Default Constructor Base ");
}
[InjectionConstructor]
public Base(IDep dep)
{
Console.WriteLine("Injection Constructor Base ");
Dep = dep;
}
}
I thought that Dependency dep should get injected automatically(through Constructor Injection) when derived class is resolved.
But this doesnt seem to work when I derive a class from it and Resolve that class, Instead a default Constructor of Base being called.
I can only get this to work when I explicitly call the constructor from the Derived Class.
class Derived : Base
{
public Derived ()
{
Console.WriteLine("Default Constructor Derived ");
}
public Derived (IDep dep) : base(dep1)
{
Console.WriteLine("Injection Constructor Derived ");
}
}
Does unity provide any direct way to implicitly call the injection Constructor of base class (not by explicit Construtor call)? If not, Is there any reason that why unity container is not doing by itself??
Base
andDerived
contain each just one constructor that takes in the required dependencies, the problem wont exist at all. I do agree however with @BatteryBackupUnit that having a base class might not be the best design. – Suppositive