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
Mex$516.99 Mex$738.99
eBook Nov 2024 166 pages 1st Edition
eBook
Mex$516.99 Mex$738.99
Paperback
Mex$922.99
Subscription
Free Trial
Arrow left icon
Profile Icon Jesse Liberty Profile Icon Joseph Dluzen
Arrow right icon
Mex$516.99 Mex$738.99
eBook Nov 2024 166 pages 1st Edition
eBook
Mex$516.99 Mex$738.99
Paperback
Mex$922.99
Subscription
Free Trial
eBook
Mex$516.99 Mex$738.99
Paperback
Mex$922.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781803231099
Vendor :
Microsoft
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 22, 2024
Length: 166 pages
Edition : 1st
Language : English
ISBN-13 : 9781803231099
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 Mex$85 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 Mex$85 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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.