Hierarchical Object and AutoFixture

I implemented a class for storing tags, the collection of tags should be hierarchical, so my class:

public class Tag
{
    public int Id { get; set; }
    public int Description { get; set; }
    public Tag ParentTag { get; set; }
    // … (methods for get children, add and remove children, etc.)
}

Thus, root tags (the user wants to have many different trees) does not have a parent element, while tags other than the root must have a parent tag.

  • Is this a good way to implement a hierarchy? I find the Composite Pattern, but in my domain all the tags are just tags, for a domain expert, there is no difference between the parent and child tags.

  • The problem occurs when using AutoFixture in the test; when I need to create a simple tag, it causes this error:

    Disclaimer:: Ploeh.AutoFixture.ObjectCreationExceptionAutoFixture was unable to instantiate the type Ploeh.AutoFixture.Kernel.SeededRequestbecause the object's trace graph contains a circular reference.

: AutoFixture, : , 2, , autofixture , node

+4
1

?

: , , , .

:

1.. , . , ParentTag Tag. , int ( Id), , ParentTagId... Tag.

2. . , Desc . ( , . - , .)

, , . , , .

, , . - ; T, T , , "", ; .

3. . :

Ploeh.AutoFixture.ObjectCreationException [...], .

Tag , . , .

, C P , P C. , ParentTag, C, P, C, , .

AutoFixture, , .

(DAG) — "" - . Tag ; , .

:

1. ParentTag:

public Tag ParentTag
{
    set
    {
        if (!IsOrIsAncestorOf(value))
        {
            parentTag = value;
        }
        else
        {
            throw new ArgumentException("ParentTag", "would cause a cycle");
        }
    }
}
private Tag parentTag;

private bool IsOrIsAncestorOf(Tag other)
{
    return this == other || IsOrIsAncestorOf(other.Parent));
    //     ^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //          Is   …   Or    …    IsAncestorOf
}

2. ParentTag readonly, . - — , :

public Tag(Tag parentTag)
{
    this.parentTag = parentTag;
}

private readonly Tag parentTag;

public Tag ParentTag
{
    get
    {
        return parentTag;
    }
}

.

+5

Source: https://habr.com/ru/post/1545339/


All Articles