Stoopkid
Stoopkid

Reputation: 1965

How to best juggle between child and parent objects

I am going to have a very large database of objects loaded into memory that contain a string (path) and a UInt (index number). For a smaller selection of these at any given time I need to also have many more members for things like extension, filesize, date modified, created and taken, resolution, length and possibly even a bitmap thumbnail. Lots of stuff that I don't want space reserved for in memory because most of it won't be used at any given time.

So with my code, I would input a tag string into a function and it would return a List<> of these file objects. But I then need them to have all of these additional members to load data into.

The only way I know how to handle this is to return that list of the parent objects, create a new list of child objects and copy the data over. Is there a cleaner way of handling this situation?

Upvotes: 0

Views: 74

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502816

Perhaps use composition instead of inheritance - have an extra property of what's currently your base class for "extended properties"... then just populate that property lazily:

public class FileInfo // Or whatever
{
    public string Path { get { ... } };
    public uint Index { get { ... } };
    public ExtendedFileInfo ExtendedFileInfo { get { ... } }
}

public class ExtendedFileInfo
{
    public long Size { get { ... } }
    ...
}

You could either implicitly load the ExtendedFileInfo when the property was requested, or just add a method to populate it explicitly, so that ExtendedFileInfo would return a null reference if it wasn't populated yet.

Upvotes: 1

Related Questions