Until this week I’ve kept posts and podcasts going back 12 years. I’m now faced with doubling the cost of my host, or cutting back… I chose to cut back. You’ll find all the posts from the past 2-3 years, but before that, my posts have been consigned to the dustbin of history.
API #9 – Tables
An important part of creating APIs is interacting with data storage. While you can use any number of database programs, a common way to store simple data is in data tables. This is particularly useful when you are recording API calls as part of your telemetry.
Storage tables are not inherently relational. The goal is to keep storage tables simple. This makes them ideal for keeping lists, logging, creating progress entries, and so forth.In this demo, we’ll create a storage table that tracks exceptions thrown during execution of our program. Let’s create a console app that just throws exceptions every few seconds.
Continue reading1,000,000 Views!
jesseliberty.com just crossed 1MM views (lifetime). So pleased you’ve stuck with me. See find me in menu to continue the dialog.
Mads Torgersen and C# 13
Mads Torgersen, lead designer of C# joins me to talk about what’s new in C# 13 and much more
Creating APIs Part 8: Validation
Performance is king (well, after accuracy) in APIs. Sending a request to the Database only to discover that the request is invalid is a waste of resources. Thus, we want to validate the request as soon as it hits the endpoint.
FluentValidation is a great tool for creating validators. It installs as a NuGet package. Also grab the version for ASP.Net.
To get going, add a using statement.
Using FluentValidatio
n
Next, create a class that derives from AbstractValidator.
public class CarNotDeletedValidator : AbstractValidator<Car>
This example uses the Car class from my forthcoming book Creating .NET APIs with C#.
Put your validation rules in the constructor. Each rule is created by using the keyword RuleFor and a lambda expression that indicates which property you want to validate and the validation rule.
using Cars.Data.DTOs;
using FluentValidation;
namespace Cars.Validators
{
public class CarNotDeletedValidator : AbstractValidator<CarDto>
{
public CarNotDeletedValidator()
{
RuleFor(x => x.Is_Deleted).Equal("0");
}
}
}
The Equal operator is one of many that you can find at the FluentValidation documentation page: https://docs.fluentvalidation.net/en/latest/built-in-validators.html.
We’ll test the data and then either compare it to what is valid and return an error if appropriate, or, more commonly, we’ll throw an exception if the data fails validation.
Assuming that it passes validation we can continue to the Database. If however it fails, we can return a meaningful error to the sender.
Reboot!
This site has been quiet for a few months while I finish up my book (.NET APIs with C#). I’m happy to reboot both the blog and Yet Another Podcast, starting soon with an interview of Mads Torgerson (lead designer of C#)!
To get things rolling, I’m going to pick up my series on creating APIs, based on my forthcoming book (scheduled to be in bookstores mid-November).
I will be speaking at Boston Code Camp this November as well. In fact, once the election is passed (I’m spending 10+ hours/week on campaigning) I expect to be putting in a lot of time on this blog.
I hope you will join me as I get back to work providing (hopefully useful) information to the community.
As always, you can reach me at jesseliberty@gmail.com
Scott Hunter – Aspire and more
Scott Hunter (VP Microsoft) joins Yet Another Podcast’s reboot to talk about Aspire, Azure and much more. Do not miss this episode… Aspire will blow you away!
Links to follow.
API Part 7 – Swagger Comments
In the previous post, I introduced Swagger and showed how to set up your project for Swagger. In this post I will show how to add Swagger comments to annotate your program.
In earlier posts we looked at the database of cars and the Get method that retrieves the entire list. That can be quite a lot of data going over the wire. What we want instead is to send pages. I’ll show that briefly and then we’ll annotate that code.
In the CarController we’ll have a Get method that takes three parameters: showDeleted, pageNumber and pageSize. The first we’ve seen before, it determines whether the list returned to the caller will include our deleted records. The second, pageNumber, will designate which page of data we want to return (zero based). The third parameter, pageSize, will designate how many records to return per page.
Thus, if we were to write
Get(false,3,4)
we would expect to get back records 17, 18, 19 and 20, because page 0 would have records 1-4, page 1 would have 5-8, etc.
Continue reading.NET APIs Part 6 – Swagger
This is part 6 in a series about building APIs in .NET using C#. The previous (part 5) entry is here, and the series starts here.
As you know, an API sits between a client and the back end. It is imperative for the client programmer to know not only what an API does, but what the URL is, what verbs it supports and what parameters are available.
Fortunately, there is an open standard and free server: Open API and Swashbuckle. You install these once for each project and then you can just use them, as we’ll see.
This post will show you how to install them, using Visual Studio. You’ll obtain the bits you need using NuGet. Start by installing Swashbuckle.AspNetCore.
Go to your project’s properties and choose Application and Console Application. Then choose Output under Build and scroll down to where you can check “Generate a file containing API documentation”
Continue reading