Frage

Is there a better design, when no circular referece occurs? Is it a problem at all? The Classes:

public class Stat
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public List<Quantity> Quantities { get; set; }
    public List<Hit> Hits { get; set; }
}
public class Hit
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public string Comment { get; set; }

    public virtual Stat Stat { get; set; }
    public List<HitComponent> HitComponents { get; set; }
}
public class HitComponent
{
    public int Id { get; set; }
    public float Amount { get; set; }

    public virtual Hit Hit { get; set; }
    public virtual Quantity Quantity { get; set; }
}
public class Quantity
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual Stat Stat { get; set; }
    public virtual Unit Unit { get; set; }
    public List<HitComponent> HitComponents { get; set; }
}
public class Unit
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }

    public List<Quantity> Quantities { get; set; }
}

Class diagram A Stat is for a statistics of something, for example a training exercise like weight lifting. A Quantity can be something that can be measured with a number, for example the weight of the barbell used (in kilograms - the unit is stored in the Unit class), or the number of repeats. In this case the Stat (weight lifting) has two Quantities (weight, reps). A Hit is one event of a Stat (a weight lifting training done once). A HitComponent belongs to a Hit, and it contains the amount of one Quantity. Every Hit must contain as much HitComponent, as much Quantities a Hit's Stat has. (For example every "weight lifting" Stat's Hit must contain two HitComponents, one for the "weight" Quantity, one for the "reps" Quantity. I suspect, that maybe this prerequisite can cause some problem...)

I use the design shown above, and there weren't too much problems - just a bit awkward with the circular reference - as long as I wanted to serialize some classes to Json string, because it caused circular reference exception.

My first question is that there is any problem with this design at all? I googled a lot, and didn't find a clear and defined answer to this kind of circular reference (some say it is not a real circular reference, because the direction is not "circular", others say that this solution is very problematic)? The other question is that somebody can suggest a better design?

War es hilfreich?

Lösung

Circular references aren't so evil. If you look, your references are just virtual (your List should also be virtual) so really it is more along the lines of reserving the ability to follow a reference in either direction. That this creates a "circular reference" by EF's definition or by design is just a side affect.

The only time you are going to have issues with these circular references is when you try to serialize an object which contains both navigation properties. In this case, you will have to instruct the serializer to skip one of the navigation directions in order to remove the circular reference.

Depending on the serializer, ignoring the navigation properties will be done differently. With the vanilla serializer (JavaScriptSerializer) which is used when Json(var) is used, you can use the [ScriptIgnore] attribute on your properties which you wish not to be followed during serialization.

For example, to remove the circular reference from Stat to Hit

public class Stat
{
 public int Id { get; set; }
 public string Name { get; set; }
 public string Description { get; set; }

 public virtual List<Quantity> Quantities { get; set; }
 [ScriptIgnore]
 public virtual List<Hit> Hits { get; set; }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top