C
C#16mo ago
br4kejet

❔ 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 {}
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?
4 Replies
Omnissiah
Omnissiah16mo ago
but what is it that would be reapeated? also should it be inherited (or can it be done by composition)?
Angius
Angius16mo ago
A separate type for an archive file I get, because it's a file but behaves like a directory But I don't think you need separate classes for zips, tars, tar.gzs, 7zs, rars, and everything else
br4kejet
br4kejetOP16mo ago
As in there doesn't need to be a custom "Zip file" and "Zip folder" class? The only thing with that is having to create a general file path, which could either be the physical file path or the file path within the archive (zip) file In the case of the text document file, I would need to implement the same functionality twice for the PhysicalTextDocumentTreeFile and ZippedTextDocumentTreeFile, such as a ReadText function or GetCharCount or something like that I thought maybe using composition, where i'd have the physical and archived text file objects, and then a TextDocumentFile property for both of them. But then the other problem is if I need the same setup for another type of file, like some sort of custom compression archive... I'd have the class CustomArchiveFile as a property for both PhysicalCustomArchiveFile and ZippedCustomArchiveFile but then i'd somehow have to manage their child items, because CustomArchiveFile would contain its own hierarchy of items
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server