Is it safe to say that a Container is the "program" or mechanism that manages IoC via DI, but it doesn't really change your initial design of DI to accomplish loose coupling?
In other words, if I want to achieve loose coupling, say for a Logger, using a Container doesn't change that. It just manages it in case I had a lot of dependencies to construct a class (less typing, more manageable, better Unit testing, etc.)
It won't change the fact that I have interfaces and contracts with ILogger or IDatabase, for example, correct?
//Assume I have to interfaces and two classes that implement them
Class DemoDI
{
ILogger myLogger;
IDatabase myDatabase;
DemoDI(ILogger myLogger, IDatabase myDatabase)
{
this.myLogger = myLogger;
this.myDatabase = myDatabase;
}
//Methods that do stuff with the logger and database
}
I'm asking if that wouldn't change, that design pattern. It's just that somewhere, (main?), I would (say with Unity)
ioc.RegisterType<ILogger MyLoggingClass>(new ContainerControlledLifetimeManager());
ioc.RegisterType<IDatabase MySQLDatabaseClass>; //transient by default
And, if MySQLDatabaseClass had,
interface IDatabaseConnection
{
void GetConnection();
}
Class DataConnection : IDatabaseConnection
{
public void GetConnection()
{
//Get a connection
}
}
Class MySQLDatabaseClass
{
DataConnection myConn;
MySQLDatabaseClass(DataConnection myConn)
{
this.myConn = myConn;
this.myConn.GetConnection();
}
//Do other things;
}
Then the Container would wire that up too, assuming I had a:
ioc.RegisterType<IDatabaseConnection, DataConnection>();
If not I'd have to do it all by hand. The container is the helper that makes it nice and neat and assembles it all.
Now, if I want to Unit test the database class I would just have to adjust my container:
ioc.RegisterType<IDatabase MyMockedUpMySQLDatabaseClass>; //transient by default
Do I understand the purpose of an IoC Container correctly? Hopefully, I didn't get too lost in my dependencies.