0

所以我的问题是,我想重构这段代码。我需要检索 3 个可以为空的双精度值(双精度值?) SpentTimeHours,这些值必须来自缓存(字典),或者如果缓存中不存在,则从 db(存储库模式)中获取。RemainingTimeHoursInitialEffortEstimateHours

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

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 作为参数传递SpentTimeHours,,RemainingTimeHoursInitialEffortEstimateHours

然后我这样打电话

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

1 回答 1

0

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

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; 
于 2017-02-17T23:46:00.810 回答