简体   繁体   中英

Web API design pattern for accepting and outputting data

I'm building a .NET Web API that would accept data from the client. 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:

Id,
ValueA,
ValueB

Model structure outline for ModelResults:

Id,
ResultingValue

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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