Giter Site home page Giter Site logo

romayneeastmond / application-net7-minimal-api-redis Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 1.0 40 KB

An ASP.NET Core project, that uses .NET 7 to demonstrate how to create a minimal Api; with integrations with EntityFramework 7 and StackExchange.Redis to store objects in an Azure Cache for Redis cache.

C# 100.00%
azure minimalapi redis swagger-ui

application-net7-minimal-api-redis's Introduction

.NET 7 Minimal Api, EntityFramework 7, and StackExchange.Redis

An ASP.NET Core project, created in Visual Studio 2022 v17.4.0, that uses .NET 7 Standard Term Support (STS) to demonstrate how to create a minimal Api. The project separates the concerns into the minimal Api layer, the database model layer, and then the business services layer.

Although the Program.cs file can be built without being opininated, there is more flexibility and readability with organizing the project this way.

There are examples below that also demonstrate newer functionality included with this update of .NET 7. In particular the grouping of Api endpoints is achieved by using method groups instead of lambda expressions. There is also an abilty to define method parameters as distinct, primitive types or as objects.

Minimal Api Examples
The MapGroup Extension Method
var group = app.MapGroup("/api").AddEndpointFilter(async (context, next) =>
{
    return await next(context);
});
AsParametersAttribute Type Decorator
group.MapPost("/insert/page", Insert);

static async Task<IResult> Insert(ISitesService sitesService, [AsParameters] Page page)
{
    await sitesService.InsertPage(page);

    return Results.Created($"/pages/{page.Id}", page);
};
Traditional Object Binding
group.MapPost("/set/cache", Set);

static async Task<IResult> Set(ICacheService cacheService, string key, ResultViewModel value)
{
    await cacheService.Set(key, JsonConvert.SerializeObject(value));

    return Results.Ok();
};

How to Use

This project uses the Visual Studio 2022 v17.4.0 which should include the packages for .NET 7 Standard Term Support (STS). Restore any necessary NuGet packages before building or deploying.

Ensure that the connection strings in the appsettings files are changed to point to the applicable instance. Then (optionally) generate or run the migrations found below.

Although Redis is not absolutely necessary for this demonstration; optionally, ensure that the settings in the appsettings.json point to a valid Redis endpoint.

This project uses Swagger to describe the available mimimal Api endpoints.

Generate Migrations

This project contains an extension method called SiteDbContextEnableMigrations that automatically runs any migrations on application start. This is ideal within a production environment. To create and apply migrations, manually, use the commands below.

Use the commands below to manually add and run

Add-Migration InitialDatabase -Context SitesDbContext
Update-Database

Copyright and Ownership

All terms used are copyright to their original authors.

application-net7-minimal-api-redis's People

Contributors

romayneeastmond avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kaayo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.