C#C
C#3y ago
7 replies
hugeman

❔ Hierarchical data types and inheritance

I have a small file browser app, and I use view models to store the files (lazily loading child files when my view models' IsExpanded property is modified by the view).

But I'm struggling to find a way to avoid repeating the same code for the same type of files (as in, file extension like .zip) when those files are placed in special folder view models.

I have BaseTreeItem which is the base view model for all tree items. Then, BasePhysicalTreeItem (contains a File Path) which PhsyicalTreeFile and PhsyicalTreeFolder (which stores child BaseTreeItems) both extend.

But then I could have special files, like ZipTreeFile or MyCustomCompressionTreeFile or TextDocumentTreeFile. ZipTreeFile would contain a collection of children (similar to PhysicalTreeFolder), but they would be of type BaseZipEntryViewModel (which extends BaseTreeItem) so that it can store a reference to the owning Zip file and other stuff to.

And now the problem is that, I have to have 2 different types of TextDocumentTreeFile, one for a physical file, and one for a zipped file:
class PhysicalTextDocumentTreeFile : PhsyicalTreeFile {}
class ZippedTextDocumentTreeFile : BaseZipEntryViewModel {}

and there's no way to have some common base class for text documents, meaning I have to repeat the same code for both of those types. How can I get around this?
Was this page helpful?