Do you make this mistake in your .NET Core API project?
By: Daniel Kreider
Tags: Dot Net Framework, Dotnet Core, C Sharp Programming, Api Development, Dotnet

Data models are the backbone of any .NET Core API server.

At least, that’s been my experience.

And building on the data models are the API controllers that give the world access to your data.

A common mistake I often see is a .NET API server project that has repeated CRUD operations for each data model. In other words, you have CRUD functions for each data model. And every time a new data model is added to the project you have to create a new controller and write the CRUD end-points from scratch.

Why is this a mistake?

Because it adds repetitive code to your .NET Core application. And because any developer worth his dough knows that the fewer lines of code it takes the better.

Elon Musk says that he would pay developers at least twice as much for removing lines of code from an application without removing functionality.

So, wouldn’t it be clever to create a base controller with CRUD (Create, Read, Update, Delete) functions for every data model you have?

Depending on the size of your project you could save yourself thousands of lines of code.

And become the hero of your development team? 🤩

Today, I’ll give you the complete guide on how to create CRUD controllers for your data models. Without breaking DRY principles. 🥳

Yes buddy, using a base controller in your .NET Core API application will make you shine. And it’s not complicated. In fact, it’s so easy to do it’s underwhelming.

So, how do we create a CRUD controller for every data model in our .NET Core project?

Create the demo API server with .NET Core

For this post, I’ll create a demo project using the .NET CLI.

dotnet new webapi -o BaseController

This will generate a basic API server with one controller called the WeatherForecastController.cs .

On to the next important step!

Installing and configuring Entity Framework Core

To keep this demo simple, we’ll use the InMemory package that allows us to spin up a fake database in memory. Here’s how we install it.

dotnet add package Microsoft.EntityFrameworkCore.InMemory --version 3.1.15

Next, we’ll create our database context. I put the code below in a file called BaseControllerDBContext.cs .

And last of all, we’ll need to add our database context to the Startup.cs file.

Good going! This is coming together lickety-split! 🎺

Create the data models

Our next step will be to create a base class that all other data classes will inherit from.

Here’s what it looks like.

To keep the example simple, I’ve only included a Id field. But for other scenarios it's common to also add a Created and Updated and anything else that you want all of your data models to include.

Now we need to update the WeatherForecast class to inherit from our base model.

Bravo! Our data models are ready! The next step is to create a base controller with CRUD operations.

Create base controller

We’ve reached the secret sauce.

This, my friend, is where you will start to shine. So sit up and pay attention.

In our Controller's folder we’ll create a file called BaseController.cs . This API controller is going to have 5 different CRUD functions.

Create a record

Get a record

Get all records associated with type

Delete a record

Update a record

Here’s the code.

Nice going! Things are coming together fast. And we’re almost done!

Extend that controller

Now, we’ll grab the WeatherForecastController.cs file and change it to inherit the functionality of the base controller.

Here’s what it needs to look like.

BOOM! 💥

You’ve created an end-point for the WeatherForecast data model without having to re-create CRUD functions for that model.

Conclusion

In this article we’ve discovered how to create a base controller with CRUD operations. We then took that base controller and used it to create an API end-point for our other data model without re-coding the CRUD operations.

This approach could save you thousands of lines of code. And it will make you a more productive developer. Cool stuff if you want to know what I believe!

If you enjoyed this article please share the energy and bang the 👏 👏 👏 button. And don’t forget to follow me for more cool articles like this one.

Follow Me: GitHub, Medium, Personal Blog