vendredi 6 mars 2015

Class design and object references

Here is a very basic example of what I am trying to do. In reallity there are more relations but not something extreme or memory heavy.



public class ClassA : ISomething
{
public double property { get; set; }
...
public ClassB classb { get; set; }
}

public class ClassB : ISomething2
{
public double Length { get; set; }
...

}



public class MyProject : BaseProject
{
public IEnumerable<ISomething> ISomethings { get; set; }
....
public IEnumerable<ISomething2> ISomethings2 { get; set; }
...

}


The issue here is that I have to keep a List of ISomethings2 and then classb property of ClassA to reference only an existing item in ISomething2 list. The hard thing here is that :



  • Removing an item from ISomething2 should remove all references to it (classb property of ClassA instances to be set to null)

  • Prevent other developers setting classb property to a non existing object in list or a new user created object.

  • Allow easy serialization via JSON.NET etc

  • Unique id generation

  • These should apply also for eg: ISomething3 etc.. even ISomething if it has a reference to another object of type ISomething


These classes are used for project description not database stuff. Like when you open a project file for an application. So changing a property in classB should be visible to all since it is the same object reference. Is there a pattern to achieve what I want without much coupling ? Alternative designs/approaches are also welcome.


Aucun commentaire:

Enregistrer un commentaire