Z
Z
ZelibobA17062017-05-01 10:50:37
OOP
ZelibobA1706, 2017-05-01 10:50:37

How to correctly change the list of one entity from another?

There is some kind of trade union and it has a list of events, this list can only be edited by trade unionists. How to change the list of events correctly?

class ProfOrg
    {
        public ProfOrg(List<UnionEvent> events)
        {
            this.events = events;
        }

        private List<UnionEvent> events;

        public void AddEvent(UnionEvent unionEvent)
        {
            events.Add(unionEvent);
        }
    }

class Union
    {
        public Union()
        {
            events = new List<UnionEvent>();
            profOrg = new ProfOrg(events);
        }

        private List<UnionEvent> events;

        private ProfOrg profOrg;

        public List<UnionEvent> Events { get { return new List<UnionEvent>(events);} }
    }

Or add methods for adding/editing events to the union class itself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WarFollowsMe, 2017-05-02
@ZelibobA1706

There is no need for a trade union to have its own list of events. If the events are assigned to the trade union, then the trade union should work directly with this list, and not duplicate its own. The fewer lists you create, the easier it will be to navigate them later. One event in Union is enough here.

class ProfOrg
{
    private Union _union;

    public ProfOrg(Union union)
    {
        _union = union;
    }

    public void AddEvent(UnionEvent unionEvent)
    {
        _union.Events.Add(unionEvent);
    }
}

class Union
{
    private ProfOrg _profOrg;
    private List<UnionEvent> _events;

    public List<UnionEvent> Events { get { return _events; } }
    public ProfOrg ProfOrg { get{ return _profOrg; }

    public Union()
    {
        events = new List<UnionEvent>();
        profOrg = new ProfOrg(this);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question