The fifth concept in the SOLID principle is the dependency inversion principle. It is heavily related to dependency injection, and Inversion of control, and when any of these concepts are mentioned, they are often used in a way that makes them synonymous with each other. Example:
Autofac is an addictive Inversion of Control container for .NET Core, ASP.NET Core, .NET 4.5.1+, Universal Windows apps, and more.
and
the Spring Framework implementation of the Inversion of Control (IoC) principle. IoC is also known as dependency injection (DI).
I am defining automatic factories here as factories that construct objects based on configuration. Autofac and Spring are two examples. Technically the are designed as IoC Containers, but broadly speaking they are used as automatic factories.
I often hear people calling Autofac, or Spring IoC "Dependency Injection" frameworks. Example:
We can call this as our IoC (Inversion of Control) container or a DI (Dependency Injection) framework.
So much of the online documentation seems to bring along with it the baggage of automatic factories. On the face of it, the SOLID principle does not seem to formally require automatic factories for constructing objects. But, is it a consequence of dependency inversion that automatic factories are required to correctly implement the SOLID principle? If the highest level of the app constructs the dependencies and then passes them in to the constructor of the highest level class, is this following the SOLID principle? Or, are IoC Containers required for this?
Take this C# Example
public interface IMessageSender
{
void SendMessage(string message);
}
public class ConsoleMessageSender : IMessageSender
{
public void SendMessage(string message)
{
Console.WriteLine(message);
}
}
public interface IApp
{
void SendHelloMessage();
}
public class App : IApp
{
private IMessageSender _messageSender;
public App(IMessageSender messageSender)
{
_messageSender = messageSender;
}
public void SendHelloMessage()
{
_messageSender.SendMessage("Hello World!");
}
}
Here is Version A) without IoC / Automatic Factories:
class Program
{
static void Main(string[] args)
{
var app = new App(new ConsoleMessageSender());
app.SendHelloMessage();
}
}
Here is Version B) with Autofac for the IoC container
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<ConsoleMessageSender>().As<IMessageSender>();
builder.RegisterType<App>().As<IApp>();
var container = builder.Build(ContainerBuildOptions.None);
var app = container.Resolve<IApp>();
app.SendHelloMessage();
}
}
Is Version B) more correct from a SOLID point of view? Would Version A) fail to conform to the SOLID principle because of the lack of an IoC container?
Note: I'm looking for a clear definition here. IoC containers are often bundled up with the concept of DI and SOLID, but the question is about whether or not they are strictly required. There is a question of whether or not they are accoutrement that have been added to SOLID over time