0

So my question is, I would like to refactor this piece of code. I need to retrieve 3 values ,SpentTimeHours, RemainingTimeHours and InitialEffortEstimateHours that are nullable double (double?), This values must come from the cache (dictionary) or if not exist from the cache, get it from db (repository pattern).

The values from the dictionary are working fine, the problem is to get the values from the repository and to use reflection. The FindFirstOrDefaultAsync return a Task<T> of an object USTask

public partial class USTask
{
    public long Aid { get; set; }
    public Nullable<double> RemainingTimeHours { get; set; }
    public Nullable<double> SpentTimeHours { get; set; }
    public Nullable<double> InitialEffortEstimateHours { get; set; }
}

the class KpiService

 public double GetEstimatedStoryPoint(
            List<CrossReference> result,
            string propertyName,
            string pattern,
            long Aid,
            bool isCache,
            bool isUserStory = false)
        {

            if (!isCache)
            {
                double? value = 0;
                //ERROR - 
                value = _userStoryTaskRepository
                    .FindFirstOrDefaultAsync(id => id.Aid == ids.Aid)
                    .Result
                    .GetType()
                    .GetProperty(propertyName)
                    .GetValue(value , null) as double?;
            }
            else
            {
                //Working fine
                value = GetValuesForEstimatedStoryPoint(GraphTable._USTask, propertyName, ids.Aid);
            }
......

in the KpiVieModel.cs

I inject the kpiService in the constructor, and I call like this , I pass the 3 propertyName as parameter SpentTimeHours, RemainingTimeHours, InitialEffortEstimateHours

then I call like this

// This Work
//In cache
sumSpentTime += _kpiService.GetEstimatedStoryPoint(crosReferenceInCache, "SpentTimeHours", pattern, Aid, true, isUserStory);
sumRemainigTime += _kpiService.GetEstimatedStoryPoint(crosReferenceInCache, "RemainingTimeHours", pattern, Aid, true, isUserStory);
sumEstimated += _kpiService.GetEstimatedStoryPoint(crosReferenceInCache, "InitialEffortEstimateHours", pattern, Aid, true, isUserStory);


This not..
//not in cache , db access 
sumSpentTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "SpentTimeHours", pattern, Aid, false, isUserStory);
sumRemainigTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "RemainingTimeHours", pattern, Aid, false, isUserStory);
sumEstimated += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "InitialEffortEstimateHours", pattern, Aid, false, isUserStory);

The problem is the object does not match the type of destination source.

I don´t want to do this n times.

SpentTimeHours = _userStoryTaskRepository.FindFirstOrDefaultAsync(id => id.Aid == ids.Aid).Result.SpentTimeHours;
RemainingTimeHours = _userStoryTaskRepository.FindFirstOrDefaultAsync(id => id.Aid == ids.Aid).Result.RemainingTimeHours;
InitialEffortEstimateHours = _userStoryTaskRepository.FindFirstOrDefaultAsync(id => id.Aid == ids.Aid).Result.InitialEffortEstimateHours;
jolynice
  • 514
  • 1
  • 8
  • 25
  • What is `foundA`? Dont you need to assign a variable to `Result` then pass that into `GetValue`? – Quantic Feb 17 '17 at 23:36
  • Hello Quantic, sorry bad copy and paste, FoundA, don´t exist, I need in fact to pass the result value to the variable value. – jolynice Feb 17 '17 at 23:39
  • It has to be 2 steps I'm pretty sure: `var myObject = _userStoryTaskRepository.FindFirstOrDefaultAsync(id => id.Aid == ids.Aid).Result; value = myObject.GetType().GetProperty(propertyName).GetValue(myObject, null) as double?;` – Quantic Feb 17 '17 at 23:41
  • Five star Quantic, Is working , I feel so stupid, it was so easy, Thank you. – jolynice Feb 17 '17 at 23:51

1 Answers1

0

Correct me if I am wrong. You want to handle better null results in this case? If that is true I will handle null with null-coalescing operator like in following:

sumSpentTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "SpentTimeHours", pattern, Aid, false, isUserStory) ?? 0;
sumRemainigTime += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "RemainingTimeHours", pattern, Aid, false, isUserStory) ?? 0;
sumEstimated += _kpiService.GetEstimatedStoryPoint(crosReferenceNotInCache, "InitialEffortEstimateHours", pattern, Aid, false, isUserStory) ?? 0; 
kat1330
  • 5,134
  • 7
  • 38
  • 61