I have several objects that inherit from a TreeTagModel. The inheriting objects are meant to be placed in a tree structure and retrieved with limited need to understand what "it" is until the object is consumed. By this I mean, it can get passed through a few functions as TreeTagModel before it is ever understood what sub class type it is.
However, the code becomes significantly more convoluted, once I include a predicate to ensure all inheriting classes implement a ResetIfSameAsRestoreModel(T restoreModel). There is a need for a large number of changes through the code to indicate what kind of TreeTagModel is being used.
For example: I can no longer pass the TreeTagModel parameter. Instead a TreeTagModel, TreeTagModel, etc is required which seems to be going away from the initial (albeit maybe ill-advised) intention of the classification as a TreeTagModel object instead of the subclass. Additionally, moving from a TreeTagModel to the original subclass requires additional classification and I am not longer able to just rely on a call to if(typeof(FordModel).IsInstanceOf(curTreeModel)). Instead I have to know that it is a FordModel (or maybe just use object but I have not tried that) so I can set the object via: TreeTagModel<FordModel> curTreeTag = GetSomeTreeTagModel();
My question is:
- Is there a better way this should be implemented to ensure that any new TreeTagModel inheriting objects are forced to implement a ResetIfSameAsRestoreModel(subClassObjectType resetModel ) function. As my current approach that moved from
abstract TreeTagModel
toabstract TreeTagModel<T>
made the code significantly more cumbersome and less readable.
//Legacy approach
public abstract TreeTagModel : AnotherBaseModel
{
//Previously used just as a container to categorize specific
//inherited object that are handled in a slightly different way.
//i.e. the object that inherit from treeTagModel can be placed in
//a tree structure
}
public class FordModel : TreeTagModel
{ ... }
public class ChevyModel : TreeTagModel
{ ... }
public class VolvoModel : TreeTagModel
{ ... }
In an attempt to force the inclusion of ResetIfSameAsRestoreModel(...) I have made the following changes
//Current attempt to ensure all subclasses have there version of ResetIfSameAsRestoreModel(...)
public abstract TreeTagModel<T> : AnotherBaseModel
{
protected abstract bool ResetIfSameAsRestoreModel(T restoreModel);
}
public class FordModel : TreeTagModel<FordModel>
{
...
protected override bool ResetIfSameAsRestoreModel(FordModel restoreModel)
{
throw new NotImplementedException();
}
}
public class ChevyModel : TreeTagModel<ChevyModel>
{
...
protected override bool ResetIfSameAsRestoreModel(ChevyModel restoreModel)
{
throw new NotImplementedException();
}
}
public class VolvoModel : TreeTagModel<VolvoModel>
{
...
protected override bool ResetIfSameAsRestoreModel(VolvoModel restoreModel)
{
throw new NotImplementedException();
}
}