Dynamic Controller Routing in .NET Web API

Dynamic Controller Routing in .NET Web API

A Beekeeping Management App Case Study (Backend Development)

Introduction

Implementing efficient routing strategies in Web API applications is crucial for creating organized and readable code. This strategy is the use of Dynamic Controller Routing (DCR). Unlike traditional static routing, DCR facilitates the dynamic mapping of HTTP requests to controller actions based on conventions, also reducing the volume of routing code.

Why use Dynamic Controller Routing?

reducing the code clutter led to a more organized structure, my intention was to make it simpler for other developers to understand and contribute to the project in the future. The conventional routing strategy sets a clear pathway for handling HTTP requests efficiently, which can be useful once the decision to scale this application arrives.

Use case

Beekeeping Management System, I employed DCR to streamline the HTTP routing points. This not only reduced the codebase but also increased its readability for other developers who want to dive into the project.

Challenges Encountered

DataTypes of the properties in the Models for BeeColony (QueenAge, BeePopulation, Class_Bee, ClassHive) were very tricky to define in the context of the beekeeping management system because I made use of enums to create a short list of the types of bees(BeeTypeClass) and the type of activity(HiveActivityClass).

this will have an output of the following:

Age_of_Queen is the age of the queen in the hive which cannot be a number higher than 6 because queen bees typically do not live for more than 5 to 6 years.

Population is the approximate number of bees found in the specific hive.

Bee_Type_Class is the Enum list of the different types of bees. 1 = African_Killer_Bee, 2 = Sweat European Bee, 3 = Asian Honey Bee, so in the case of the example shown above, the Sweat European Bee is what is chosen.

Hive_Activity_Class is also an Enum list which has the following: 1 = Active, 2 = Non_Active, 3 = Semi_Active. Hive activity represents the overall busyness of the hive which can also indicate the general health of the hive.

The error that I had to deal with was trying to display the various options in the Enum Classes of Bee_Type_Class and Hive_Activity_Class which only appeared as numbers when the data was executed/submitted, so I made use of a JSON serialization, which looked like this: "[JsonConverter(typeof(JsonStringEnumConverter))] ", the Enum looks at the text which is assigned to the specific number and outputs the text of that number.

Pros and Cons of Dynamic Controller Routing

Pros

Code Reduction: By removing the need for declaring numerous route templates, DCR significantly trims down the code.

Enhanced Readability: The conventional nature of DCR predictably organizes the code, making it easier to look through the HTTP routing points.

Ease of Maintenance: With fewer routing declarations, maintaining and updating the routes becomes a less daunting task.

Adherence to Convention: DCR promotes a convention-over-configuration principle, encouraging developers to follow a consistent coding standard.

Cons

Learning Curve: Developers accustomed to static routing may find the conventions of DCR challenging to grasp.

Less Explicit Control: With conventions doing the heavy lifting, developers might feel a loss of control over the routing process.

Potential for Conflicts: In complex applications, the dynamic nature of DCR could lead to route conflicts that are tricky to debug.

Conclusion

In conclusion, Dynamic Controller Routing is a powerful tool for .NET developers seeking to minimize code while maintaining a clean, organized structure. While it comes with its set of challenges, the benefits it brought to the Beekeeping management project are undeniable, showcasing the potential of DCR in real-world applications.

Link to repo: https://github.com/taumang/bee_app_test_version