如何使用从 Task<T> 结果返回的 GetProperty 中的 getValue

How to use getValue from GetProperty returned from the result of Task<T>

所以我的问题是,我想重构这段代码。 我需要检索 3 个值,SpentTimeHoursRemainingTimeHoursInitialEffortEstimateHours,它们可以为 nullable double(double?), 该值必须来自缓存(字典),或者如果缓存中不存在,则从数据库(存储库模式)中获取它。

字典中的值工作正常,问题是从存储库中获取值并使用反射。 FindFirstOrDefaultAsync return一个对象Task<T>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; }
}

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);
            }
......

在KpiVieModel.cs

我在构造函数中注入了 kpiService,我这样调用, 我将 3 propertyName 作为参数传递 SpentTimeHoursRemainingTimeHoursInitialEffortEstimateHours

然后我这样调用

// 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);

问题是对象与目标源的类型不匹配。

我不想这样做 n 次。

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;

如果我错了请纠正我。在这种情况下,您想处理更好的 null 结果吗?如果这是真的,我将使用 null-coalescing operator 处理 null,如下所示:

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;