Hello Dear Readers,
As Analog Devices GUI Team, we had a great event this Tuesday. We wrote a Web API that can change its behavior at run-time according to a flag.
As a team, we loved this event and called it a mini-hackathon.
We first understood the problem in the morning. Our problem is as follows: We will write a Web API This API will have an endpoint called getweather. This endpoint will generate three types of random weather reports. Celsius, Fahrenheit and Kelvin. It will decide the type of calculation according to the flag set in the runtime.
We aimed to develop a loosely coupled code without code duplication. We also designed the behavior of enpoint to change when the flag is changed without deploying the code.
After understanding the problem as a team, we started coding. We took a break from our development for lunch and ate together.
After the team members completed their development, everyone presented their solution. We argued over it. Finally, we focused on what is the best apporach.
We are planning to repeat our mini-hackathon, which was very productive and enjoyable. Let's look at the problem together and understand its solution.
WebAPI output The default output when you produce a WebAPI in Visual Studio. We didn't change the script too much to focus on problem solving rather than operation.
First we need to create classes for the Three calculation methods and these classes should derive from an interface.
Below you can see the interface and three different classes. Since it is a question of which of these classes will be decided in run-time, we should use the Factory Design Pattern, so let's create our Factory Class.
As you can see, according to the flag value, the switch case structure determines which class instance will be created. Here we benefited from .Net's Dependency Injection infrastructure.
Hiç yorum yok:
Yorum Gönder