Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Programming APIs with C# and .NET
Programming APIs with C# and .NET

Programming APIs with C# and .NET: Develop high-performance APIs that ensure seamless application communication and enhanced security

Arrow left icon
Profile Icon Jesse Liberty Profile Icon Joseph Dluzen
Arrow right icon
$19.99 per month
Paperback Nov 2024 166 pages 1st Edition
eBook
$24.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Jesse Liberty Profile Icon Joseph Dluzen
Arrow right icon
$19.99 per month
Paperback Nov 2024 166 pages 1st Edition
eBook
$24.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$24.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Programming APIs with C# and .NET

What We’ll Build

In this chapter, we will provide a context for the rest of the book. That context is a simple application for buying and selling cars. We will not build out this application but, in fact, we’ll have only one type of object (Car) and one table in our database. This will allow us to focus on the API rather than getting hung up on database design.

In this chapter, we’re going to cover the following main topics:

  • What an API is and what it is for
  • The backend database that we will use throughout the book
  • The application that we will build throughout the book
  • The Car object that we will use to demonstrate CRUD operations

We will use only free software, as shown in the Technical requirements section, and we will take advantage of open source utilities such as Dapper and AutoMapper, both introduced in Chapter 1.

Technical requirements

For this chapter, you’ll need Visual Studio and SQL Server Management Studio (SSMS), as well as Postman. Remember that SSMS was installed with Visual Studio.

Note that you can also manage your database from within Visual Studio using the Server Explorer and the SQL Server Object Explorer.

The code files for this book are available in the book’s GitHub repository : https://github.com/PacktPublishing/Programming-APIs-with-C-Sharp-and-.NET/tree/main/Chapter02

What is an API and what is it for?

The purpose of an API is to decouple the backend of an application (e.g., a database) from the frontend (e.g., a web application or mobile application), as illustrated in Figure 2.1.

Figure 2.1 – Separation of concerns

Figure 2.1 – Separation of concerns

The client does not talk directly to the database but rather talks to the API, which, in turn, talks to the database. The huge advantage of this approach is that you can modify the database and the client does not have to change. Alternatively, you can modify the front end (for example, a website) without changing the database.

These modifications take place throughout the development of a large project and continue after delivery. It is imperative, especially with a mobile application, that you can modify the backend without forcing the user to update the application. Even with web applications, however, this can be critical, as the team maintaining the database may well not be the team maintaining...

The application

The database we’ll be creating will be used in a simple (fictional) application for buying and selling cars. The database will keep a list of automobiles along with details of their engine, performance, and so on. A short example of this data is shown in Figure 2.2.

Figure 2.2 – A subset of the data table we’ll be using

Figure 2.2 – A subset of the data table we’ll be using

Creating the Car Table

The data for the Car table is from the free Automobile dataset at https://kaggle.com. Download the data and import it into a database named Cars and a table named Car. The columns should take care of themselves.

The datasets on Kaggle change frequently, so your list of cars may look a bit different. All the datasets are all presented as .csv files, so just download one and open SSMS in order to do the import. Here are the steps:

  1. Log into SSMS and create a database.
  2. Create a table named CARS.
  3. Start the Import Data Wizard. To do so, right-click on the table...

Database structure

Our database consists of just one table: Car. As shown in Figure 2.2, that table must store various attributes of each car (name, miles per gallon, number of cylinders, and so on). Figure 2.3 shows the table:

Figure 2.3 – Car table columns

Figure 2.3 – Car table columns

Notice the is_deleted column. We’ll be using “soft delete” – that is, rather than removing a row on deletion we’ll just set is_deleted to true (1). That allows us to easily restore that column just by changing that value back to 0 (false).

Other than id, all of the columns are strings, which will make working with them easier.

Car object

Corresponding to the Car table, our code has a Car entity (Cars/Data/Entities/Car.cs):

namespace Cars.Data.Entities;
public class Car
{
    public int Id { get; set; }
    public string name { get; set; } = null!;
    public string mpg { get; set; } = null...

The ASP.NET application

To get started, create a new ASP.NET project using the ASP.NET Core Web API template. Put the files wherever is convenient for you and choose the latest version of .NET (this book was written with .NET 8).

The basic structure of our application will be as follows:

  • Controllers with endpoints
  • Services
  • Repositories

We’ll review endpoints and all the rest of this as we go.

Program.cs

You won’t have to edit Program.cs for this application but it is worth a few minutes of your time to review it:

using Cars.Data;
using Cars.Data.Interfaces;
using Cars.Data.Repositories;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Load DB configuration and register the connection factory for 
...

Folders

To organize our application, we will create the following folders:

  • Controllers
  • Data
  • Interfaces
  • Repositories
  • Services

Let’s briefly explain what these are:

  • The client is the application calling the API (for example, the website or mobile application). The Controllers folder will hold methods that act as endpoints (an endpoint is what the client connects to via a URL).
  • The Data folder will hold the definition of our entities – in our case, the Car object shown earlier.
  • The Interfaces folder is just what it sounds like: it will hold the interfaces to our C# objects.
  • The Repositories folder will hold the code between our methods and the database calls.
  • The Services folder will hold supporting code.

The flow will be as follows:

  1. The client calls a method in a controller.
  2. The method calls a service to handle the business logic.
  3. The service calls a method in the repository, which, in turn,...

Summary

In this chapter, you saw what an API is and how it is used to separate the front end (e.g., a web site) from the backend (e.g., a database). We also looked at the simple database and application that we’ll use throughout the book.

In order to demonstrate CRUD operations, we’ll build a simple application focused on a Car object as if we were working with an inventory of cars. We imported it from https://kaggle.com.

This lays the groundwork for the rest of the chapters and for the simple application that we will build. Our focus will be strictly on creating APIs, so we will spend little time on database technology or even on the frontend.

You try it

This is a good time to create your application and the database. If you are adventurous, create a similar but different application, database, and data entity. This will ensure that you cement the elements we’ll be using.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn API design principles and architecture for high-performance applications
  • Develop robust APIs using best practices for security, scalability, and maintainability
  • Implement both traditional and minimal APIs with real-world scenarios
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

APIs are at the heart of modern software development, enabling seamless communication between applications. In the C# and .NET ecosystem, mastering API development is key to building scalable, reliable systems that meet both client and enterprise needs. Written by C# and .NET experts Jesse Liberty and Joseph Dluzen, this book draws on their extensive experience as hands-on programmers to equip you with the tools, techniques, and best practices for creating powerful APIs. Starting with API fundamentals, it covers everything you need to design, build, and consume APIs using .NET technologies such as ASP.NET Core, and explores both controller and Function API implementations. You'll integrate essential tools such as Postman for testing to streamline your development workflow. Through practical examples and exercises, the book guides you from database creation to deploying APIs on Azure. By the end of this book, you’ll have gained real-world skills to develop secure, high-performance APIs that scale with your applications for both personal projects and enterprise-level systems.

Who is this book for?

This book is for .NET developers, software engineers, and backend developers looking to master API development using C# and ASP.NET Core. It caters to those with a basic understanding of C# and familiarity with the .NET ecosystem. Even though no prior experience with API frameworks is required, knowledge of ASP.NET Core will be helpful.

What you will learn

  • Understand core API concepts and how to apply them in .NET projects
  • Design RESTful APIs with a focus on scalability
  • Build APIs with ASP.NET Core
  • Explore alternative options for API development using Azure Functions, including stateful workflows with Durable Functions
  • Integrate databases into API projects, ensuring efficient data flow
  • Implement security measures such as authentication and authorization
  • Deploy APIs to Azure and learn the essentials of cloud integration

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 22, 2024
Length: 166 pages
Edition : 1st
Language : English
ISBN-13 : 9781835468852
Vendor :
Microsoft
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 22, 2024
Length: 166 pages
Edition : 1st
Language : English
ISBN-13 : 9781835468852
Vendor :
Microsoft
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Table of Contents

13 Chapters
Chapter 1: Getting Started Chevron down icon Chevron up icon
Chapter 2: What We’ll Build Chevron down icon Chevron up icon
Chapter 3: Implementing with REST Chevron down icon Chevron up icon
Chapter 4: Documentation with Swagger Chevron down icon Chevron up icon
Chapter 5: Data Validation Chevron down icon Chevron up icon
Chapter 6: Azure Functions Chevron down icon Chevron up icon
Chapter 7: Azure Durable Functions Chevron down icon Chevron up icon
Chapter 8: Advanced Topics Chevron down icon Chevron up icon
Chapter 9: Authentication and Authorization Chevron down icon Chevron up icon
Chapter 10: Deploying to Azure Chevron down icon Chevron up icon
Chapter 11: What’s Next? Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.