Let’s explore! I’m running the latest .NET 6 and you can run it on Windows, Mac, or Linux and I cloned it to a folder locally. There’s two versions of a complete Todo API in this sample, one using Entity Framework Core and one using Dapper for data access. Both are lightweight ORMs (object relational mappers). Let’s explore the Dapper example that uses SQLite. The opening of the code in this example doesn’t require a Main() which removes a nice bit of historically unneeded syntactic sodium. The Main is implied. using System.ComponentModel.DataAnnotations; At this point we’ve got a SQLite connection string ready to go scoped in the Services Dependency Injection Container (fancy words for “it’s in the pile of stuff we’ll be using later”) and we’ve told the system we want a nice UI for our Open API (Swagger) web services description. It’s WSDL for JSON, kids! Then a call to EnsureDb which, ahem, ensures there’s a database! await EnsureDb(app.Services, app.Logger); What’s it look like? Just a little make this table if it doesn’t exist action: async Task EnsureDb(IServiceProvider services, ILogger logger) Next we’ll “map” some paths for /error as well as paths for our API’s UI so when I hit /swagger with a web browser it looks nice: if (!app.Environment.IsDevelopment()) Then sprinkle in a little Hello World just to give folks a taste: app.MapGet("/", () => "Hello World!") You can see how /hello would return a JSON object of Hello: “World”
Now let’s get some Todos from this database, shall we? Here’s all of them and just the complete ones: app.MapGet("/todos", async (SqliteConnection db) => Lovely. But what’s this Todo object? We haven’t seen that. It’s just a object that’s shaped right. Perhaps one day that could be a record rather than a class but neither Dapper or EFCore support that yet it seems. Still, it’s minimal. public class Todo Let’s get a little fancier with an API that gets a Todo but it might not find the result! It may produce an HTTP 200 OK or an HTTP 404 NotFound. app.MapGet("/todos/{id}", async (int id, SqliteConnection db) => Don’t be sad if you don’t like SQL like this, it’s just a choice amongst many. You can use whatever ORM you want, worry not.
Go explore the code and check it out for yourself! Check out our Sponsor! YugabyteDB is a distributed SQL database designed for resilience and scale. It is 100% open source, PostgreSQL-compatible, enterprise-grade, and runs across all clouds. Sign up and get a free t-shirt! © 2021 Scott Hanselman. All rights reserved. A .NET 6 Minimal API Todo example Playground published first on http://7elementswd.tumblr.com/ via Tumblr A .NET 6 Minimal API Todo example Playground
0 Comments
Leave a Reply. |
About UsIf you want to keep up with the latest in web design, you have to be willing to study and learn on an ongoing basis. If you are a web developer or web designer, you’ll find a lot of useful tutorials and code snippets collection here. Furthermore, we also discuss more general topics of web design and how the role of a web designer has evolved over the years.
|