Good evening, I'm trying to implement SOLID principle with DI for the first time in a middle-size project. Most of the time I understand but now I have a trouble. Let me take a poor exemple but it shows the structure of the app. I have inherited classes with different constructors (code provided below). When I need an instance, I know the class I need. So, 2 questions about this code : 1/Do I have to use the interface in this case to achieve SOLID principe (or simply declare an instance of ClassA like ClassA exemple=new ClassA("text") in Program.cs) 2/Is it well done? (what is good/what is bad, what to do/what to avoid ?)
class Program
{
static void Main(string[] args)
{
IFactory exemple = new Factory();
//With Unity--> exemple=Resolve<IFactory>();
exemple.GetTextClassA("test");
exemple.GetTextClassB(1);
Console.ReadLine();
}
}
public interface IFactory
{
ClassA GetTextClassA(string text);
ClassB GetTextClassB(int text);
}
public class Factory : IFactory
{
public ClassA GetTextClassA(string text)
{
return new ClassA(text);
}
public ClassB GetTextClassB(int text)
{
return new ClassB(text);
}
}
public abstract class MainClass
{
private string _text;
public MainClass(){}
protected abstract string GetText();
protected virtual void Initialize()
{
_text = GetText();
Console.WriteLine(_text);
}
public string TextToDisplay
{
get { return _text; }
}
}
public class ClassA : MainClass
{
string _textA;
public ClassA(string textParam)
{
_textA = textParam;
base.Initialize();
}
protected override string GetText()
{
return "Text is :"+_textA;
}
}
public class ClassB : MainClass
{
int _numParam;
public ClassB(int numParam)
{
_numParam = numParam;
base.Initialize();
}
protected override string GetText()
{
return "Text is :" + _numParam.ToString();
}
}
Many thanks for your comments.