I'm fairly new to all this, so this is probably OOP 101 but I can't get my head around it, assume the following C# code lives in an assembly:
internal interface IDataStore
{
void Store(string name, object data);
object Retrieve(string name);
}
internal class DBStore : IDataStore
{
public DBStore(string connection) { }
public void Store(string name, object data) { }
public object Retrieve(string name) { }
}
public class GizmoManager
{
public GizmoManager(IDataStore dataStore) { }
// Other stuff
}
public class WidgetManager
{
public WidgetManager(IDataStore dataStore) { }
// Other stuff
}
If a second assembly tries to create a GizmoManager and a WidgetManager, it can't because it has no way to get hold of a DBStore (since it is internal not public).
The following don't work AFAICS:
- Make DBStore and IDataStore public. Bad because the client assembly can then bypass GizmoManager/WidgetManager and access the DB however it likes.
- Don't pass in an IDataStore to the GizmoManager and WidgetManager constructors. Bad because it reduces testability (you can't easily pass in a MockDataStore).
- Do something magic with factories. Doesn't seem to solve anything because you need to pass the same IDataStore to both GizmoManager and WidgetManager and thus the client assembly still needs to assign an IDataStore to a variable (which it can't because IDataStore is internal).
This is probably stunningly obvious but I just can't see it. How would one overcome this contradiction?
TIA.