
Mads Torgersen, lead designer of C# joins me to talk about what’s new in C# 13 and much more
Mads Torgersen, lead designer of C# joins me to talk about what’s new in C# 13 and much more
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.
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 (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.
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 readingThis 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 readingAn experiment in supplementing the API material with video. This will be rough at first…
In the previous posting we saw how to create an API to get all the cars in our database. In this posting we’ll look at the remaining CRUD (Create Review Update Delete) operations.
As you may remember, we created a controller named CarController. ASP.NET will strip off the word Controller, leaving us with Car, which we will use to access the endpoints of our API.
An endpoint is just a URL that takes us to the operation we want.
We looked at GetAll, let’s take a look at Get. In this case, we have an id for the car we want, but we want all the details of that car. Simple!