Want an easy way to build a CRUD API?
Building an API with .NET can be quite time consuming as there is a lot of boilerplate code you usually have to deal with. Controllers, EntityFramework, Models, Routes....
and whatever else comes to your mind. However, it doesn't have to be like this and can actually be done lightning fast!
What if someone told you, this snippet of just a class is already a fully working API:
[Api("/courses")]
public class Course : IObjectBase<int>
{
public int Id { get; set; }
public List<Student> Students { get; set; }
public Teacher Teacher { get; set; }
public List<DateTime> Schedule { get; set; }
}
Yes, this is the FULL code you have to write to get a working CRUD API with everything handled out of the box and working magically. Database, Routing, Caching, OpenAPI Definition...its all there, just based on this class.
The same can be done with pure JSON as well!
{
"name": "Car",
"route": "/cars",
"idType": "int",
"Fields": [
{
"name": "Name",
"type": "String"
},
{
"name": "Description",
"type": "String"
},
{
"name": "Year",
"type": "int"
},
{
"name": "Make",
"type": "virtual Make"
},
{
"name": "MakeId",
"type": "int"
}
]
}
How is this done? What is this magic?
Creating APIs that fast and simple can be done using my OS Project "APIGenerator". You can find it on Github here -> https://github.com/DeeJayTC/net-dynamic-api
A few more details can also be found here -> https://www.tcdev.de/instant-crud-apis-with-net
The APIGenerator basically turns any class or JSON Definition that follows my schema, into a full blown API with everything working out of the box.
It can be used with any Database supported by EntityFramework.
Getting Started
Start either a new WebAPI or WebApp project with .NET 6
Download the package:
dotnet add package TCDev.ApiGenerator
The APIGenerator comes with a few optional dependencies out of which you need to pick at least the database ones to use the API properly.
Install either of these for the matching database:
TCDev.APIGenerator.Data.SQL
TCDev.APIGenerator.Data.SQLite
TCDev.APIGenerator.Data.Postgres
TCDev.APIGenerator.Data.InMemory
And highly recommended to use the OData package as well
dotnet add package TCDev.APIGenerator.OData
Setting things up:
Add the library to program.cs (or startup.cs if you're using the old way!)
builder.Services.AddApiGeneratorServices()
.AddConfig(NameOfRootNodeInAppSettings)
or
.AddConfig(new ApiGeneratorConfig() { ... })
or
.AddConfig()
Creating your first API
The first and most important thing to note is, you need to implement the "IObjectBase<T>" interface for your primary key. This is made to allow you to select the type of primary key (int, string or guid) you want to use but is also used to generate the API Endpoints later on. This is the minimum requirement you have to do.
Lets write a class as a sample:
public class Person : IObjectBase<int> {
public int Id {get;set;}
public string Name {get;set;}
public DateTime DateOfBirth {get;set;}
}
Now, this is enough for our underlying systems to generate the database tables for this specific class and you can theoretically query and store data for it. Yet this is not an API Endpoint yet.
Lets further extend the class to work as an API
[Api("/people")
public class Person : IObjectBase<int> {
public int Id {get;set;}
public string Name {get;set;}
public DateTime DateOfBirth {get;set;}
}
See how we only added that attribute? Yes, this is enough to turn the class into a full API! If you start your App now you should be greeted by Swagger with a fully working API!
For more details and guides just jump straight to the docs! ->
https://www.tcdev.de/
Let me know what you think, any questions..just jump in the comments below!