lundi 28 mars 2016

c# method looks very redundant what can i do to fix it design pattern , method?

I have this method in C# that looks like I should really refactor it . Should I use a design pattern ? Too much repetition is what I see NOW and especially as MORE conditional if statements get added

Change to a method?

public void CreateOrUpdateReportDefinition(ReportGroupSubReport reportGroupSubReport, bool isNew, int report)
    {

        if (report == 1)
        {
            var entity = _envyUnitOfWork.ReportDefinitions.GetById(reportGroupSubReport.Id) ?? new ReportDefinition();
            if (isNew)
                entity.SetNew();

            _envyUnitOfWork.ReportDefinitions.InsertOrUpdate(entity, true);
        }
        else if (report == 2)
        {
            var entity = _envyUnitOfWork.TraxReports.GetById(reportGroupSubReport.Id) ?? new TraxReport();

            if (isNew)
                entity.SetNew();

            _envyUnitOfWork.TraxReports.InsertOrUpdate(entity, true);

        }


        Mapper.Map(reportGroupSubReport, entity);
        _envyUnitOfWork.Commit();


    }

Aucun commentaire:

Enregistrer un commentaire