简体   繁体   English

用于接受和输出数据的Web API设计模式

[英]Web API design pattern for accepting and outputting data

I'm building a .NET Web API that would accept data from the client. 我正在构建一个.NET Web API,它将接受来自客户端的数据。 It would execute some calculation on the data and then return the results of the calculation to the client. 它将对数据执行一些计算,然后将计算结果返回给客户端。

For instance: 例如:

Model structure outline for ModelData: ModelData的模型结构大纲:

Id,
ValueA,
ValueB

Model structure outline for ModelResults: ModelResults的模型结构大纲:

Id,
ResultingValue

API: API:

ModelResults ExecuteCalculation(ModelData modelData)

I'm trying to figure out a proper design pattern that would fit this type of behavior. 我试图找出适合这种行为的合适设计模式。 At 1st I thought the Repository Pattern would suffice as it would cover many design patterns associated with this project. 在第一天,我认为存储库模式就足够了,因为它将涵盖与该项目相关的许多设计模式。 But I'm not totally sure, since by my understanding a repo. 但我不完全确定,因为我理解一个回购。 pattern is sort of a top down approach (you can correct me if I'm wrong). 模式是一种自上而下的方法(如果我错了你可以纠正我)。

Let me know what your thoughts are on this. 让我知道你对此有何看法。 Much appreciated. 非常感激。

I'm not sure there's a right answer to this, because choosing a design pattern is pretty subjective and developer preference, plus we don't have 100% of the details on what your calculating. 我不确定是否有正确的答案,因为选择一个设计模式是非常主观的和开发人员的偏好,而且我们没有100%的详细信息来计算你的。

If you're making a simple calculation that's just that of what your question is, then I think your first go to would work. 如果你正在进行一个简单的计算,就像你的问题那样,那么我认为你的第一个去做就行了。

var calculator = new Calculator();
var result = calculator.Calculate(data);

My preference is builder patterns for complex calculations. 我的偏好是用于复杂计算的构建器模式。 Allows flexibility into a calculation by applying process specific logic to a step. 通过将特定于过程的逻辑应用于步骤,可以灵活地进行计算。 And readiblity looks cleaner when digging into a problem in the calculator. 在挖掘计算器中的问题时,readiblity看起来更干净。

var result = new Calculator(data)
       .GetBaseNumbers()
       .CalculateYearly()
       .CalculateMonthly()
       .ApplyOverrides()
       .GetResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM