Using C#, I got a class called BaseConfigurations which handles CRUD operations. This class also contains a protected method which is used inside the CRUD handling methods:
public class BaseConfigurations : ICRUDHandler
{
public virtual bool CreateSetting(string someData)
{
//some logic
ProtectedMethod();
//more logic
}
protected virtual void ProtectedMethod() { ... }
}
Now there's a different version which can handle the CRUD operation a bit different:
public class BaseConfigurationsV2 : BaseConfigurations { ... }
There are 2 instances of BaseConfigurations
and BaseConfigurationsV2
living at the same time and should be invoked based on some logic I have.
Now there's another class which handles specifically secret data:
public class SecretConfigurations : BaseConfigurations
{
public ICRUDHandler _crudHandler;
public SecretConfigurations(ICRUDHandler handler)
{
_crudHandler = handler; //this handler gets either the instance of `BaseConfigurations` or `BaseConfigurationsV2`
}
public override bool CreateSetting(string data)
{
_crudHandler.CreateSetting(data);
//adds additional logic
}
public override void ProtectedMethod() { ... }
}
My problem here is that because the ICRUDHandler which SecretConfigurations receives is either BaseConfigurations or BaseConfigurationsV2, it doesn't know the protected method which was overriden in SecretConfigurations and doesn't invoke it.
The easy way to fix it is to probably just make another class of SecretConfigurationsV2
which will derive from BaseConfigurationsV2 and then have the handler be initialized to either SecretConfigurations
or SecretConfigurationsV2
, but that involves code duplication.
How would you design a fix for this issue?