I'm having trouble designing classes to best make use of DI / IoC principles, particularly when a class shares a dependency with one of its dependencies (ie. X has dependencies Y and Z, and Y has dependency Z).
An example will probably be useful:
Lets say I have a class that will encapsulate some connection information for my app. Call it 'ConnectionInfo'
class ConnectionInfo
{
// ...
}
I've designed a worker class that uses the connection info, so I'll be injecting my dependencies through the constructor.
class Worker
{
public Worker(ConnectionInfo c)
{
// ...
}
}
Now, lets say I want to build another class. It needs access to the connection info, but I also want to have access to the functionality provided by the Worker class.
class AnotherClass
{
public AnotherClass(ConnectionInfo c, Worker w)
{
// ...
}
}
This is all well and good. However, there is an implied logical relationship between the ConnectionInfo object injected into the Worker, and the ConnectionInfo object injected into the AnotherClass. The design only makes sense when they are one and the same. This isn't enforced, though. There is nothing stopping one ConnectionInfo being injected into AnotherClass, and a completely unrelated ConnectionInfo being injected into Worker.
Am I dealing with IoC / DI in the wrong way?
Should the classes be designed differently, or should the issues be handled in another way (such as enforcing via parameter validation, or maybe just leaving it up to the IoC container)?