【问题标题】:Generating different results from Web API call using an Anonymous type使用匿名类型从 Web API 调用生成不同的结果
【发布时间】:2018-01-13 22:21:20
【问题描述】:

我正在尝试找出从 API 调用返回散点图数据的最佳方法。这是一个绘制所选员工得分的图表。

我的要求是调用者可以为每个轴指定多个分数之一,命名结果分数、attributeId 分数或 domainId 分数。

所以返回的结果总共有 30 种可能性。

API 调用:

[HttpPost("{id}/scatterplot")]
public ActionResult Scatterplot([FromRoute] int id, [FromBody] ScatterplotAxis axis)
    

散点图轴.cs

public class ScatterplotAxis
{
    public Axis XAxis { get; set; }
    public Axis YAxis { get; set; }
}

public class Axis
{
    public int? TeamId { get; set; }  // optional
    public string NamedScore { get; set; }
    public int? AttributeId { get; set; }
    public int? DomainId { get; set; }
}

所以想法是您可以使用以下数据调用它:

{
  "xAxis": {
    "teamId": 1362,
    "namedScore": "NamedScore2",
    "attributeId": null,
    "domainId": null
  },
  "yAxis": {
    "teamId": 1362,
    "namedScore": "",
    "attributeId": 35,
    "domainId": 0
  }
}

所以在上面的例子中,我想返回一个结果,在 X 轴上带有“NamedResult2”的得分,在 Y 轴上带有给定 attributeId 的得分。

我的问题是我试图从发布的 json 中获取已选择的内容并调整结果:

到目前为止我有:

 var employees = ... // omitted for brevity
 // get employees and filter by teamId if it is supplied...

 var xAxisSelection = FindAxisSelection(axis.XAxis);
 var yAxisSelection = FindAxisSelection(axis.YAxis);

 if (xAxisSelection == "NotFound" || yAxisSelection == "NotFound")
     return BadRequest("Axis Selection not Found");

        // e.g. would have to do something like this for all 30 combinations using if/else
        // if(xAxisSelection == "NamedScore2" && yAxisSelection == "Attribute")

        var results = employees.Select(e => new
        {
            Id = e.Id,
            FullName = e.FullName,
            TeamName = e.MemberTeams.FirstOrDefault() == null ? "" : e.MemberTeams.FirstOrDefault().Name,

            // how to do this for all combinations???
            XAxis = e.NamedScores.NamedResult2,  
            YAxis = e.AttributeResults.Where(a => a.AttributeId == axis.YAxis).Score
        }).ToArray();

        return Ok(results);
    }

    private string FindAxisSelection(Axis axis)
    {
        if (axis.AttributeId != null || axis.AttributeId > 0)
            return "Attribute";
        else if(axis.DomainId != null || axis.DomainId > 0)
            return "Domain";
        else if(axis.NamedScore == "NamedScore1")
            return "NamedScore1";
        else if (axis.NamedScore == "NamedScore2")
            return "NamedScore2";
        else if (axis.NamedScore == "NamedScore3")
            return "NamedScore3";
        else if (axis.NamedScore == "NamedScore4")
            return "NamedScore4";

        return "NotFound";
    }

所以我的问题是关于生成结果。我真的不想为 30 种组合中的每一种都使用大量的 if else 语句块。有什么设计模式我应该使用来使这个更清洁、更高效。

我认为最好使用匿名类型,这样我就不必为 30 种组合中的每一种都创建具体类型?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api design-patterns anonymous-types


    【解决方案1】:

    规则设计模式非常适合这个用例。看看下面的链接。

    http://www.michael-whelan.net/rules-design-pattern/

    简而言之,您可以做的是创建具有自己要求的对象数量(如果在您的情况下满足条件),如果在对象中满足条件,他们将执行他们的代码,否则它将调用下一个对象。这种方法也称为责任链

    请求的示例可以让您了解如何避免多个 if 条件:-

    public class Program
    {
        public static void Main()
        {
            new AllSteps().Process(1);
        }
        public interface IStep
        {
            void Process(int x);
        }
        public class StepOne:IStep
        {
            public void Process(int x)
            {
                // do something with x
            }
        }
        public class StepTwo:IStep
        {
            public void Process(int x)
            {
                // do something with x
            }
        }
        public class AllSteps:IStep
        {
            readonly List<IStep> steps= new List<IStep>();
    
            public AllSteps()
            {
                this.steps.Add(new StepOne());
                this.steps.Add(new StepTwo());
    
            }
    
            public void Process(int x)
            {
                steps.ForEach(y=>y.Process(x));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多