Circular reference in domain model using c# struct

烂漫一生 提交于 2019-12-06 12:01:10

The Options field in MultipleChoiceQuestion is just a reference here, which will be null by default. The Question field in Option is a value of type MultipleChoiceQuestion which will have its default value by default. There's no problem here (other than a questionable design in more ways than one).

A genuine circular reference leading to a logical memory problem will fail to compile:

struct Foo
{
    Bar bar;
}

struct Bar
{
    Foo foo;
}

Test.cs(6,9): error CS0523: Struct member 'Foo.bar' of type 'Bar' causes a cycle in the struct layout Test.cs(11,9): error CS0523: Struct member 'Bar.foo' of type 'Foo' causes a cycle in the struct layout

Question (property) is encapsulation; the question is part of the outer struct, via a field.

Options, however, is a reference to an external object that yields one or more Options; these will be copied each time.

There are several other mistakes here:

  • mutable structs: EVIL
  • public fields: EVIL
  • the Options would be better as a list; not all enumerables are repeatable
  • they aren't "values" - so they should not be structs; use classes instead

IMO:

public class MultipleChoiceQuestion
{
    private readonly List<Option> options
        = new List<Option>();
    public IList<Option> Options {get { return options; } }
}

public class Option
{
    public MultipleChoiceQuestion Question {get;set;}
}

The MultipleChoiceQuestion will simply have a reference (pointer) to an object that is IEnumerable, so the size of that struct on the stack will be a single IntPtr (4 bytes on an x86 machine).

The Option struct will have the same size, because it contains only the MultipleChoiceQuestion struct.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!