How to auto populate a property within a List property of a Class

1.2k Views Asked by At

Firstly, thanks in advance for anyone who can help.

I have a header and lines scenario. I want the lines object to auto-populate with the headerId from the Header. Please can someone help?

public class Header
{
    public int headerId { get; set; }
    public List<Lines> lines { get; set; }
}

public class Lines
{
    public int lineId { get; set; }
    public int headerId { get; set; }   //<<< want to autopopulate this from Header object
}
2

There are 2 best solutions below

2
On BEST ANSWER

There's a number of ways you can do this.

Option 1 - Fix on collection set.

public class Header
{
    private List<Lines> _lines
    public int HeaderId { get; set; }
    public List<Line> Lines 
    { 
        get { return _lines; }
        set 
        { 
            _lines = value;
            if(_lines != null)
            {
                foreach(var line in _lines)
                    line.HeaderId = HeaderId;
            }
        }
    }
}

public class Line
{
    public int LineId { get; set; }
    public int HeaderId { get; set; }  
}

The problem with this route is that the HeaderId is only set when you set Lines collection. That means you can change it after the fact or any new items you add to Lines will not be corrected.

Option 2 - Pass in the Header when constructing the Line

public class Header
{

    public int HeaderId { get; set; }
    public List<Line> Lines { get; set; }
}

public class Line
{
    public Header ParentHeader { get; }
    public int LineId { get; set; }
    public int? HeaderId { get { return ParentHeader?.HeaderId; }  

    public Line(Header header)
    {
        ParentHeader = header;
    }
}

As long as you have the same Header instance for all of the lines, if you were change the Header.HeaderId all of the lines will automatically be updated.

Option 3 - Don't add an instance of Line directly to the collection

public class Header
{
    List<Line> _lines = new List<Line>();

    public int HeaderId { get; set; }
    public ReadOnlyCollection<Line> Lines { get {return _lines.AsReadOnly(); }


    public void AddLine(Line line)
    {               
        line.HeaderId = HeaderId;
        _lines.Add(line);
    }
}

This will have the same issue as Option 1.

0
On

You could do something like this...

public class Header
{
    static int _headerIdSeed = 0;

    public int HeaderId { get; internal set; } = _headerIdSeed++;
    public IList<Line> Lines { get; } = new List<Line>();

    public Header Add(Line line)
    {
        line.HeaderId = this.HeaderId;
        this.Lines.Add(line);
        return this;
    }

    public Header Add(params Line[] lines)
    {
        return this.AddRange(lines);
    }

    public Header AddRange(IEnumerable<Line> lines)
    {
        foreach (var line in lines)
        {
            line.HeaderId = this.HeaderId;
            this.Lines.Add(line);
        }
        return this;
    }
}

public class Line
{
    static int _lineIdSeed = 0;
    public int LineId { get; internal set; } = _lineIdSeed++;
    public int HeaderId { get; internal set; }
}

... and you can use it like this ...

var header = new Header
{
    HeaderId = 1,
}.Add(new Line { },
      new Line { },
      new Line { });

foreach (var line in header.Lines)
    Console.WriteLine($"{line.HeaderId}-{line.LineId}");
/*
1-0
1-1
1-2
*/