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;