I have a class that serves as a data model. I’ll simplify it like this:
public class DataModel
{
public bool IsDataModelActive {get; internal set;}
}
So the programmers who will use my DLL will only see what they need without risking to break anything.
On the flipside, I want to use DI in my classes that use DataModel. So I have to create an interface:
public interface IDataModel
{
bool IsDataModelActive {get;}
}
And so I inject this in my class:
public class Class1
{
IDataModel dataModel;
public Class1(IDataModel dataModel)
{
this.dataModel = dataModel
}
}
In this case, it is impossible for me to access the setter of dataModel.IsDataModelActive.
The best patch I’ve found is to cast after receiving the injection:
public class Class1
{
DataModel dataModel;
public Class1(IDataModel dataModel)
{
this.dataModel = (DataModel)dataModel
}
}
This helps for the unit tests, but it kind of break the whole concept of DI. What if I have another class that implements IDataModel?
What strategy do you/would you use?
If the interface IDataModel doesn’t allow you to access the setter from outside of the DataModel, then (hopefully not surprisingly) you shouldn’t need to access the setter from outside of the DataModel. Either the DataModel itself or something that can see the setter should be responsible for setting that value.