I've used both of the below patterns for testability and I'm wondering which is better OOP/SOLID.
Pattern 1: Provide two constructors, one of which creates the concrete dependency
public MyClass
{
private IDependency dependency;
public MyClass() : this(new ConcreteDependency()) { }
public MyClass(IDependency dependency)
{
this.dependency = dependency;
}
}
Pattern 2: Base class has dependency intializeer and derived class calls it with concrete dependency
public abstract MyClassBase
{
private IDependency dependency;
protected void Initialize(IDependency dependency)
{
this.dependency = dependency;
}
}
public MyClass : MyClassBase
{
public MyClass()
{
Initialize(new ConcreteDependency());
}
}
Aucun commentaire:
Enregistrer un commentaire