A dive into an amazing program for developers: NDepend
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 readingAPI – Video 1
An experiment in supplementing the API material with video. This will be rough at first…
.NET APIs Part 5 – All the CRUD APIs
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!
.NET APIs Part 4 – Creating the APIs
We are, finally, ready to create our ASP.NET Core application that will host our traditional and our minimal APIs. (This series begins here.)
The code for this blog post is available here:
git clone https://github.com/JesseLiberty/Cars.git
Please note that WordPress seems to be broken and so the layout will be imperfect.
To get started, open Visual Studio 2022 and make sure you are fully up to date. Click on Create A New Project and select ASP.NET Core Web App, making sure that C# is the selected language
File Comparison
In a previous post I said I was still looking for the right file comparison tool. I may have found it! I returned to ExamDiff Pro and voilà! the perfect combination of power and ease of use. I integrated it with Visual Studio and life is good.
.NET APIs Part 3 – Dapper
In part 2 of this series we created a simple database. In this part we’ll look at how to perform CRUD operations against that DataBase in anticipation of creating APIs for these operations.
Dapper is a micro-ORM (Object Relational Mapper) for .NET that simplifies data access against a database. In this blog post, we’ll focus on a code example that employs Dapper to manage a Car entity. This example uses a custom DatabaseConnectionFactory to handle database connections. Let’s dive right in.
Database configuration
You will need a working MS Sql database running to test the code. In the sample code, you can set your connections details in the appsettings.json file.
Create the database and car table
Create your database and then you can use the following script to initialize the database
USE [Cars]
GO
/****** Object: Table [dbo].[car] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[car](
[id] [int] IDENTITY(1,1) NOT NULL,
[make] [varchar](50) NULL,
[model] [varchar](50) NULL,
[model_year] [int] NULL,
[price] [decimal](18, 0) NULL,
[deleted] [int] NULL,
CONSTRAINT [add_identity] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Required NuGet Packages
We are using the following NuGet packages:
- Dapper - Dapper.SqlBuilder
You can install these packages using the NuGet Package Manager Console:
Install-Package Dapper Install-Package Dapper.SqlBuilder
Setting Up the Database Connection Factory
Before we get into the CarRepository class, let’s discuss the DatabaseConnectionFactory class. This class is responsible for creating and opening database connections. It leverages DbSettings to get the connection string:
DatabaseConnectionFactory takes an IOptions<DbSettings> dependency, which allows it to read the database settings from a configuration source (like appsettings.json). The GetConnection method uses this information to create and open a SQL Server connection, which it then returns.
The Car Repository
Initialization and Dependency Injection
The CarRepository class takes a DatabaseConnectionFactory as a constructor argument, indicating dependency injection:
Methods in Car Repository
GetAll Method
This method returns all the car records. Optionally, it can also return records marked as deleted.
The GetAll method uses Dapper.SqlBuilder to construct the SQL query dynamically. This allows for more flexibility, as you can conditionally add WHERE clauses, joins, or other SQL constructs.
Get Method
This method retrieves a single car record based on its ID:
UpsertAsync Method
The UpsertAsync method handles both insert and update operations:
DeleteAsync Method
The DeleteAsync method marks the record as deleted:
Resources
Dapper GitHub Repository Dapper SqlBuilder
Conclusion
Dapper offers a powerful, yet simple way to perform CRUD operations in .NET applications. The code example in this post demonstrates how to make the most of Dapper in a practical scenario involving a Car entity. The DatabaseConnectionFactory class helps to manage database connections efficiently, ensuring that the rest of the code can focus on business logic rather than connection management. Whether you need to build SQL queries dynamically or perform simple operations, Dapper has tools to help you get the job done efficiently.
Rodrigo Juarez is a full-stack developer who has specialized in Xamarin in recent years and is now focusing on MAUI.
He is also a book author. With over 25 years of experience, Rodrigo has contributed to a diverse array of projects, developing applications for web, desktop, and mobile platforms. Specialized in Microsoft technologies, he has expertise across various sectors, including management, services, insurance, pharmacy, and banking. Rodrigo Juarez can be reached at info@rodrigojuarez.com