C#C
C#13mo ago
6 replies
sonodan.

Controlling state changes from the root object

Hey all. I have the following structure of objects, which basically consists of a PhaseHolder, which contains a collection of Phase. Each Phase can hold a Comment, and each Comment can hold a list of Reply.
public class PhaseHolder
{
    private readonly List<Phase> _phases = new();

    public IReadOnlyCollection<Phase> Phases => _phases.AsReadOnly();

    public bool IsCommentable { get; set; }

    public void AddPhaseComment(string comment)
    {
        var phase = _phases.First();

        if (IsCommentable)
        {
            phase.AddComment(comment);
        }
    }

    public void AddPhase()
    {
        _phases.Add(new Phase());
    }
}

public class Phase
{
    private readonly List<Comment> _comments = new();

    public IReadOnlyCollection<Comment> Comments => _comments.AsReadOnly();

    public void AddComment(string comment)
    {
        _comments.Add(new Comment(comment));
    }
}

public class Comment
{
    private readonly List<Reply> _replies = new();

    public IReadOnlyCollection<Reply> Replies => _replies.AsReadOnly(); 

    public string Description { get; set; }

    public Comment(string comment)
    {
        Description = comment;
    }

    public void AddReply(string reply)
    {
        _replies.Add(new Reply(reply));
    }
}

public class Reply
{
    internal string Description { get; set; }

    public Reply(string description)
    {
        Description = description;
    }
}


The PhaseHolder object contains a bool property that should control adding comments, but this can be avoided:

var phaseHolder = new PhaseHolder();
phaseHolder.AddPhase();
phaseHolder.AddPhaseComment("Adding comment in desired way");
var phase = phaseHolder.Phases.FirstOrDefault();
phase?.AddComment("Avoiding IsCommentable check");


I could create an read-only IPhase interface , but these models also serve as my EF core models, which need a concrete type, e.g. IReadOnlyCollection<Phase>
Was this page helpful?