3

GitHub - csharpfritz/InstantAPIs: A library that generates Minimal API endpoints...

 2 years ago
source link: https://github.com/csharpfritz/InstantAPIs
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

InstantAPIs

A proof-of-concept library that generates Minimal API endpoints for an Entity Framework context.

For a given Entity Framework context, MyContext

public class MyContext : DbContext 
{
    public MyContext(DbContextOptions<MyContext> options) : base(options) {}

    public DbSet<Contact> Contacts => Set<Contact>();
    public DbSet<Address> Addresses => Set<Address>();

}

We can generate all of the standard CRUD API endpoints using this syntax in Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSqlite<MyContext>("Data Source=contacts.db");

var app = builder.Build();

app.MapInstantAPIs<MyContext>();

app.Run();

Now we can navigate to /api/Contacts and see all of the Contacts in the database. We can filter for a specific Contact by navigating to /api/Contacts/1 to get just the first contact returned. We can also post to /api/Contacts and add a new Contact to the database. Since there are multiple DbSet, you can make the same calls to /api/Addresses.

Community

This project is covered by a code of conduct that all contributors must abide by. Contributions are welcome and encouraged..


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK