jeudi 1 octobre 2015

Is there a better way than writing multiple If statements that almost do same thing

I have multiple if statements in one of the function, 22 to be exact. All if statements almost do same thing which makes function looks very big.

For e.g

        if( filterData.ActivityIds != null )
        {
            foreach( var activityId in filterData.ActivityIds )
            {
                var activity = DWReportData.DimActivities.FirstOrDefault( a => a.ActivityID == activityId );

                if( activity != null )
                {
                    activitySKs.Add( activity.ActivitySK );
                }
            }

            skCollection.Add( "ActivitySK", activitySKs );
        }

        // From
        if( filterData.FromSiteIds != null )
        {
            foreach( var siteId in filterData.FromSiteIds )
            {
                var site = DWReportData.DimSites.FirstOrDefault( s => s.SiteID == siteId );

                if( site != null )
                {
                    srcSiteSKs.Add( site.SiteSK );
                }
            }

            skCollection.Add( "SrcSiteSK", srcSiteSKs );
        }

        if( filterData.FromLocationIds != null )
        {
            foreach( var locationId in filterData.FromLocationIds )
            {
                var siteLocation = DWReportData.DimSiteLocations.FirstOrDefault( sl => sl.LocationID == locationId );

                if( siteLocation != null )
                {
                    srcSiteLocationSKs.Add( siteLocation.SiteLocationSK );
                }
            }

            skCollection.Add( "SrcLocationSK", srcSiteLocationSKs );
        }

        if( filterData.FromMaterialIds != null )
        {
            foreach( var materialId in filterData.FromMaterialIds )
            {
                var material = DWReportData.DimMaterials.FirstOrDefault( m => m.MaterialID == materialId );

                if( material != null )
                {
                    srcMaterialSKs.Add( material.MaterialSK );
                }
            }

          skCollection.Add( "MaterialSK", srcMaterialSKs );
        }

        if( filterData.FromPeriodIds != null )
        {
            foreach( var periodId in filterData.FromPeriodIds )
            {
                var workPeriod = DWReportData.DimWorkPeriods.FirstOrDefault( wp => wp.WorkPeriodID == periodId );

                if( workPeriod != null )
                {
                    srcWorkPeriodSKs.Add( workPeriod.WorkPeriodSK );
                }
            }

            skCollection.Add( "WorkPeriodSK", srcWorkPeriodSKs );
        }

filterData is a custom chart filter and I cannot apply a foreach loop on it as it doesn't have GetEnumerator() function.

What actually is happening that I am going through all the 22 DataMembers(Properties) of filterData and depending upon the DataMember, extracting the data from specific table. So if I am checking filterData.ActivityIds than data will come from DimActivities table, if filterData.FromSiteIds than data will come from DimSites table, etc.

I tried to search on google but as it is specific scenario, I am not able to find any way to achieve this. I find out Reflection and tried to use the same here to reduce the code but no success.

Thanks in advance to all.

Aucun commentaire:

Enregistrer un commentaire