Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i simplify these methods to avoid duplicate code?

Is there a way to simplify these methods? They are the same except for a different method being called.

private async Task ChangePresence(bool args, int voteId)
    {
        if (GeneralMeetingState.GeneralMeeting != null)
        {
            _spinnerVisible = true;

            await AgendaItemsDetailState.ChangePresence(args, GeneralMeetingState.GeneralMeeting.ID,
                AgendaItemId, voteId);
            await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
            await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);

            _spinnerVisible = false;
        }
    }

    private async Task VoteFor(bool args, int voteId)
    {
        if (GeneralMeetingState.GeneralMeeting != null)
        {
            _spinnerVisible = true;

            await AgendaItemsDetailState.ChangeVoteFor(args, GeneralMeetingState.GeneralMeeting.ID,
                AgendaItemId, voteId, GeneralMeetingState.IsUserOwner);
            await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
            await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);

            _spinnerVisible = false;
        }
    }

    private async Task VoteAgainst(bool args, int voteId)
    {
        if (GeneralMeetingState.GeneralMeeting != null)
        {
            _spinnerVisible = true;

            await AgendaItemsDetailState.ChangeVoteAgainst(args, GeneralMeetingState.GeneralMeeting.ID,
                AgendaItemId, voteId, GeneralMeetingState.IsUserOwner);
            await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
            await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);

            _spinnerVisible = false;
        }
    }

    private async Task ChangeVoteAbstinence(bool args, int voteId)
    {
        if (GeneralMeetingState.GeneralMeeting != null)
        {
            _spinnerVisible = true;

            await AgendaItemsDetailState.ChangeVoteAbstinence(args, GeneralMeetingState.GeneralMeeting.ID,
                AgendaItemId, voteId, GeneralMeetingState.IsUserOwner);
            await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
            await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);

            _spinnerVisible = false;
        }
    }

I was looking into passing a method as a parameter and using Action<bool, int> but i couldn’t figure it out. Any help is much appreciated

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The idea to pass Action<bool, int> seems to be almost the right one. The easiest one would be passing just Func<Task>. Something along this lines:

// TODO - give more meaningful name
private async Task DoSomething(Func<Task> something)
{
    if (GeneralMeetingState.GeneralMeeting != null)
    {
        _spinnerVisible = true;

        await something();
        await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
        await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);

        _spinnerVisible = false;
    }
}

And methods changed to the following:

private async Task ChangePresence(bool args, int voteId)
{
    await DoSomething(() => AgendaItemsDetailState.ChangePresence(args, GeneralMeetingState.GeneralMeeting.ID, AgendaItemId, voteId))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading