Can I use a C # collection to store self-referenced class instances?

I need to simulate collectible web files in memory, but this is the relationship between them. This file A (like html) can contain a link to file B (like css) and file C (like javascript). In addition, file D may also require file B. If I wanted to delete file A, I would need to make sure that any files it uses (like file B) are also not used by another file (like file D) ... Perhaps something like:

  List<WebFile> list_of_webfiles

  public class WebFile
  - string url
  - bool parentFile

  public class FileRelationship
  - private WebFile parentWebFile;
  - private WebFile childWebFile;

      

QUESTION - What would be the best way to model this in C #? (e.g. what type of collection and how to model)

Note. It needs to be modeled in memory (no database) and I need to be able to serialize XML to persist as well. An example of what I mean would be something similar to this ...

        XmlSerializer serializer = new XmlSerializer(typeof(List<WebFile>));
        TextWriter textWriter = new StreamWriter(CONFIG_FILE_PATH);
        serializer.Serialize(textWriter,  list_of_webfiles);
        textWriter.Close();

      

thank

+2


source to share


2 answers


This seems to imply a hierarchical "tree" relationship where you can have

Class WebFile:
- URL : string
- Parent : WebFile
- Children : WebFile[] (could be a list depending on the need)

      

Then you have

List<WebFile> webFiles;

      

This approach makes it easy to navigate the web file tree and find related files, but it is more difficult to list all the files.

Alternatively, you can keep the list of files and relationships separately



Class WebFile
- URL : string

Class WebFileRelationship
- Parent : WebFile
- Child : WebFile

      

And you have 2 containers

List<WebFile> webFiles;
List<WebFileRelationship> relationships;

      

This approach makes it easy to list all relationships or all files, but it is difficult to define individual relationships.

It all depends on your application, do you need more information about individual files or relationships?

+1


source


The fact that you have duplicates (in terms of multiple files requiring B) means that it would be painful to use the most obvious "requires" structure as a tree, as it involves multiple nesting of B (from different parents). Several variants:

  • keep the object references in the object model, but only list the name (or some other reference) in the file; relatively easy to do, but needs fixing after deserialization
  • lists only the name (or some other reference) in the relation and mirrors that in the object model - that is, "file.Parent" is a key, not another object
  • have a complete object model and use a graph serializer, for example DataContractSerializer

    with persistence-objects enabled

I would choose between the latter two; the latter has a "not very nice" xml, but is relatively easy to implement. But I would be tempted to just use the middle option and only have key references in the object model, i.e.



[XmlType("file"), XmlRoot("file")]
public class File {
    [XmlAttribute("name")]
    public string Name {get;set;}
    [XmlElement("ref")]
    public List<string> References {get;set;}
    public File() {References = new List<string>();}
}

      

maybe not pure OO, but simple to do. Also - avoid duplicate data; if you store it the same as above, you can always browse to see "what is using this file" (with some indexing if you need to). But trying to maintain a relationship in both directions (ie "UsedBy") is a nightmare.

+1


source







All Articles