Programming APIs with C# and .NET - Jesse Liberty - E-Book

Programming APIs with C# and .NET E-Book

Jesse Liberty

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 161

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Programming APIs with C# and .NET

Develop high-performance APIs that ensure seamless application communication and enhanced security

Jesse Liberty

Joseph Dluzen

Programming APIs with C# and .NET

Copyright © 2024 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Associate Group Product Manager: Kunal Sawant

Book Project Manager: Prajakta Naik

Publishing ProductManager: Srishti Seth

Book Coordinator: Manisha Singh

Senior Editor: Rounak Kulkarni

Technical Editor: Vidhisha Patidar

Copy Editor: Safis Editing

Indexer: Rekha Nair

Production Designer: Gokul Raj S.T

Business Development Executive: Sonia Chauhan

First published: November 2024

Production reference:1251024

Published by Packt Publishing Ltd.

Grosvenor House

11 St Paul’s Square

Birmingham

B3 1RB, UK.

ISBN 978-1-83546-885-2

www.packtpub.com

To my 106-year-old mother, my wife, and especially my children. I am a very lucky man.

– Jesse Liberty

To my wife, children, and parents.

– Joseph Dluzen

Contributors

About the authors

Jesse Liberty specializes in C#, Git, and building world-class APIs. He hosts the popular Yet Another Podcast (https://jesseliberty.com/podcast/) and is the author of more than two dozen best-selling programming books (https://packt.link/FVtEr).

Jesse was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS, and Vice President of Information Technology at Citibank. He was also on the teaching staff at Brandeis University. Jesse has been a Microsoft MVP for 13 years.

Jesse is a recognized expert and has spoken at conferences worldwide. His website is https://jesseliberty.com.

I have so many people to thank, starting with my co-author, Joe Dluzen, without whom this book would literally have been impossible. I’d also like to thank all the people at Packt, especially Rounak, Prajakta, Kunal, and Vidhisha who stuck in there with us, as well as the technical reviewer. The quality of this book is due to their diligence, though any mistakes are ours. I’d also like to thank my boss for his encouragement and for teaching me so much about creating world-class enterprise APIs. Finally, as ever, I wish to thank my wife, without whose patience and encouragement I could never have written any of my books.

Joseph Dluzen has been working in C# and associated frameworks for almost 20 years. He is currently in a software architecture role for one of the world’s largest agricultural machine manufacturers. Formerly a senior consultant at Accenture, he has experience in a wide range of industries, including government identity, autonomous vehicle tooling, and enterprise auction management systems.

I would like to thank Jesse, without whom this book would never have happened.

About the reviewer

Matheus de Campos , based out of Brazil, is a software engineer with a Bachelor's degree in Computer Science. He has extensive international experience and specializes in Microsoft technologies. Currently, he works as a consultant for tech organizations, focusing on delivering impactful solutions that optimize technology strategies.

Table of Contents

Preface

1

Getting Started

Technical requirements

Where does the API fit in?

The players

Getting set up

Downloading the free software you need

Summary

You try it

2

What We’ll Build

Technical requirements

What is an API and what is it for?

Creating the database

The application

Creating the Car Table

Database structure

Car object

The ASP.NET application

Program.cs

Connecting to the database

Folders

Summary

You try it

3

Implementing with REST

Technical requirements

REST

Client/server

Using web protocols

Statelessness and caching

Implementing REST in ASP.NET Core

DTOs

Dapper

Examining the SQL

Putting it all together (inserting a car)

Creating the body in Postman

Get all

Update

Soft delete

Summary

You try it

4

Documentation with Swagger

Technical requirements

Setting up the Swagger documentation

Swagger for the controller

Swagger out of the box

Running your API in Swagger

param tag

Response codes

Summary

You try it

5

Data Validation

Technical requirements

Tuning your API

Paging

Validation

Capturing errors

Built-in validators

Summary

You try it

6

Azure Functions

Technical requirements

Understanding Functions

Hosting

Application packaging

Billing

Project walkthrough

Starting up

Options

Routing

Deploying

Azure resource creation

Publishing from Visual Studio

Post-deployment reconfiguration

Summary

You try it

7

Azure Durable Functions

Technical requirements

Overview of Durable Functions

Orchestrators

Activities

Programming and debugging walk-through

Additional options

Exceptions

Summary

You try it

8

Advanced Topics

Technical requirements

Implementing an advanced API

Updating the database

Creating the classes

Supporting implementation

Azure Storage Tables

Exploring the essentials

TableModel

StorageTableService

Populating the table in Azure

Introducing Microsoft Aspire

Sample program

Exploring the Aspire dashboard

What have you learned, Dorothy?

Adding Aspire to an existing app

Getting oriented with our new project

Adding Aspire

Summary

You try it

9

Authentication and Authorization

Technical requirements

Introduction to authentication and authorization

Overview of A&A

Enabling authentication on existing functions

Programmatically accessing your API

Authorizing additional Azure Resources

Summary

You try it

10

Deploying to Azure

Technical requirements

Getting started

Using tests to ensure code quality

Using the same binaries

Deploying from DevOps

Importing sample code

Creating the build pipeline

Publishing an artifact

Deploying your artifact to Azure

Continuous deployment

Testing the release pipeline

End-to-end testing

Summary

You try it

Congratulations!

11

What’s Next?

C#

SQL

Database

API architecture

Dapper

AutoMapper

Visual Studio

Git

Housing your version control

Summary

Index

Other Books You May Enjoy

1

Getting Started

Let’s start by making sure you’re in the right place. This is a book about creating Application Programming Interfaces (APIs) using .NET. Along the way, we will look at a very simple backend (database) and frontend (user interface) and the tools we use to create and test APIs.

The core responsibility of an API is to decouple an application (web, mobile, and so on) from the backend (database, another API, and more). The API sits between the frontend (the user interface to your application) and the backend (for our purposes, a database).

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

What software you will needHow to obtain the software you needInstalling Visual Studio

Technical requirements

To proceed through this book, you will need a computer running Windows (10 or 11) and an internet connection. It is best if your computer has at least 16 GB of memory, and, of course, you’ll need room on your disk for the software and for the code you will write.

You can create APIs on any platform (e.g., Linux) and use any development environment (e.g., Visual Studio Code instead of Visual Studio). This book, however, will focus on Visual Studio and Windows as they are the most popular and arguably the most powerful way to create .NET APIs.

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.

Where does the API fit in?

When you are creating a decoupled application, your principal parts are:

The front endThe back endThe middleware

A typical front end might be a web application, a mobile application, or other ways of displaying data.

A typical backend might be a database or another service.

The middleware sits between the front end and the back end. The most important middleware is the API. The job of the API is to ensure that the front end and back end are decoupled – that is, you can modify one without affecting the other. This is simply good programming practice and will save you hours (or months) of rewriting should either of these change (which they will!).

The players

Typically, the backend and the frontend are created by different teams, though, of course, you can have one developer creating it all. We’re also going to restrict our backend to a simple database, though any source of data can be a backend. Finally, we’re not going to build a full frontend (that would be a distraction from the point of this book) but, rather, we’re going to use a tool, Postman, to mimic a frontend. There will be more on Postman throughout this book.

Getting set up

You can create your backend, API, and frontend on any operating system you like. For this book, we’ll be creating all three on Windows, using Visual Studio 2022, the latest Postman, and Dapper as a simple Object Relational Mapper (ORM) to make our lives easier. There are a few other simple tools we’ll be using, so let’s get you set up.

Downloading the free software you need

To get started, if you don’t have Visual Studio installed, go to https://visualstudio.com and click on Downloads (this site changes frequently but the basic steps remain the same). You have three choices of which version to download: Community, Professional, and Enterprise. The Community version is free and will provide all you need to follow along in this book.

When you click on your choice, Visual Studio Setup will download to your downloads directory. Double-click on it and click on Yes at the security prompt. The installer will update itself and then will begin the installation. This can take a little while, but don’t go away as you have some selections to make.

Note

If you have Visual Studio installed but for some reason want to install the Community Edition as well, that is no problem as they can run side by side.

A menu will appear similar to Figure 1.1 (if it does not, click Modify).

Figure 1.1 – Setting up Visual Studio: please note that this screenshot intends to show the layout, so text readability is not essential

Be sure that ASP.NET and web development is checked. Scroll down and check Data storage and processing (if you are short on disk space, skip this one). Once you are satisfied, click on Install while downloading and then click on Modify.

SQL Server will be installed for you, as will SQL Server Management Studio (SSMS). You will typically interact with SQL Server through SSMS. We will look at how to work with this tool, and all the others, as we go.

Your next tool is Dapper. This is a small, lightweight ORM (often called a micro-ORM) that does one important part of the work of the much bigger SQL platform, Entity Framework, but with much less overhead. Specifically, Dapper will map queries to objects.