liveBook · Manning

Chapter 3: Using database and storage components

What you will learn in chapter 3 of Building Distributed Applications that Work

The introduction explains why the e-commerce app needs databases and establishes that Aspire integration libraries support both relational and NoSQL databases.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why the e-commerce app needs databases and establishes that Aspire integration libraries support both relational and NoSQL databases.

Key terms: Aspire integration libraries, SQL-based relational database management systems (RDBMSs), NoSQL databases, document stores, key-value stores, e-commerce app, products, orders, databases

3.1 Connecting Aspire to an existing database

This section explains how an Aspire application connects its services to an existing database through an AppHost connection-string resource.

Key terms: AppHost.cs, AddConnectionString(), ConnectionStrings, resource name, service discovery

3.2 SQL database components basics

This section explains how Aspire SQL integrations connect applications to relational databases through service discovery, host and client libraries, and a choice of data-access approaches.

Key terms: Aspire integrations, RDBMS, database connection strings, service discovery, host library, consumer library, client libraries, container, persistent managed database instance, object-relational mapper

3.2.1 Working with the SQL data model

This section defines the product database entity and the DTO used to send product information from the API to the UI.

Key terms: entity model, Product, Id, Key attribute, MaxLength attribute, data transfer object, DTO, ProductDto, same contract, OnlineShop.ServiceDefaults

3.2.2 Displaying the DTO information in the UI

The section explains why the online shop UI remains independent of its database and describes the changes that make it display products instead of sample weather data.

Key terms: database-agnostic, separation of concerns, DTO, infrastructure layer, presentation layer, OnlineShop.Web, ProductsApiClient, builder.Services.AddHttpClient(), Home.razor, NavMenu.razor

3.3 Hosting a SQL Server component

This section shows how to host a SQL Server container and database in Aspire, configure their names and connection settings, preserve development data, and make the database available to a service.

Key terms: Aspire.Hosting.SqlServer, AppHost.cs, AddSqlServer(), sa, AddParameter(), AddDatabase(), database resource, service discovery, sqldb, master

3.3.1 Consuming a SQL Server integration

Shows how an Aspire-hosted API uses the ADO.NET SQL Server integration to connect to a database, work with product data, and return it through an endpoint.

Key terms: Aspire.Microsoft.Data.SqlClient, SqlConnection, dependency injection, connection string, ADO.NET, Shop, USE Shop, products, API endpoint, service layer

3.3.2 Using Entity Framework

This section shows how the API service uses Entity Framework with its existing Aspire-hosted SQL Server, from registering a DbContext to creating and querying the database.

Key terms: Entity Framework, Aspire.Microsoft.EntityFrameworkCore.SqlServer, OnlineShop.ApiService, DbContext, ProductsDbContext, DI, sqldb, connection string, EnsureCreated(), Database

3.3.3 Integrating with Azure SQL Server

This section explains how to register Azure SQL Server in an Aspire host, how that resource behaves locally and in Azure, and when existing SQL Server clients can use it.

Key terms: Aspire.Hosting.Azure.Sql, AddAzureSqlServer(), AddDatabase(), Docker container, managed Azure SQL Server instance, resource group, SQL Server client libraries, AddConnectionString(), connection string resource, reference

3.4 Hosting an Oracle component

This section explains how to host and consume an Oracle database in Aspire, why its container should persist during local development, and how the same client registration can connect to an existing Oracle database.

Key terms: Aspire.Hosting.Oracle, database server container, database reference, persistent lifetime, Oracle container, Aspire.Oracle.EntityFrameworkCore, DbContext, Entity Framework, LINQ, AddConnectionString()

3.5 Hosting a PostgreSQL component

This section shows how to host PostgreSQL in Aspire, connect to it through raw SQL or Entity Framework, and obtain its connection string.

Key terms: Aspire.Hosting.PostgreSQL, AddPostgres(), AddDatabase(), Aspire.Npgsql, NpgsqlDataSource, Aspire.Npgsql.EntityFrameworkCore.PostgreSQL, DB context, LINQ, connection string, database reference

3.6 Introduction to NoSQL

Defines NoSQL, introduces four ways NoSQL databases organize data, and explains which kinds of problems suit each.

Key terms: NoSQL, relational structure, SQL, document store, key-value store, graph database, column-family store, distributed cache, data warehousing, connection maps

3.6.1 Introduction to MongoDB

Introduces MongoDB’s document model and explains when it may be useful instead of a relational database, including its tradeoffs for relationships and application objects.

Key terms: MongoDB, NoSQL, document store, BSON, collections, documents, relationships, foreign-key constraints, iterative development, real-time analytics

3.6.2 Hosting a MongoDB component

Shows how to register a MongoDB container and make its database available to an Aspire-hosted service.

Key terms: Aspire.Hosting.MongoDB, AppHost.cs, MongoDB server instance, database reference, WithReference()

3.6.3 Working with a MongoDB client

This section shows how an Aspire-hosted service connects to MongoDB, represents documents, seeds an empty collection, and exposes its data through an API endpoint.

Key terms: Aspire.MongoDB.Driver, AddMongoDBClient(), database reference, connection string, MongoDB C# Driver, BSON document, Id, ObjectId, ProductReviewsDocument, IMongoClient

3.6.4 Other NoSQL components

This section introduces Azure Cosmos DB as a globally distributed, multimodel NoSQL service that Aspire can emulate locally.

Key terms: globally distributed, data replication, Azure regions, active-active, active-passive, multimodel, data models, API-agnostic, SQL API, MongoDB API

3.6.5 Hosting a Cosmos DB component

The section shows how to register a Cosmos DB component in an Aspire host, choose between Azure and an emulator, and reference it from a service.

Key terms: Aspire.Hosting .Azure.CosmosDB, AppHost.cs, AddAzureCosmosDB(), emulated mode, Cosmos DB component variable, WithReference()

3.6.6 Connecting to Cosmos DB

This section explains how an Aspire-hosted service registers a Cosmos DB client and names an Entity Framework alternative.

Key terms: Aspire.Microsoft.Azure.Cosmos, AddAzureCosmosClient(), CosmosClient, connection string, Entity Framework, Aspire.Microsoft.EntityFrameworkCore.Cosmos

3.7 Azure Storage integration

Introduces Azure Storage accounts and distinguishes their table, blob, and queue storage services by what each stores or does.

Key terms: Azure Storage account, subservices, emulated locally, Table Storage, NoSQL, reference data, Blob Storage, Queue Storage, message broker

3.7.1 Hosting Azure Storage components

This subsection shows how an Aspire host provides Azure Table Storage, either through an emulator or real Azure resources, and passes its reference to a service.

Key terms: storage account, Aspire.Hosting.Azure .Storage, AppHost.cs, RunAsEmulator(), AddAzureStorage(), AddTables(), Table Storage reference, service

3.7.2 Interacting with Azure Table Storage

This section shows how an Aspire-hosted service connects to Azure Table Storage, models its entities, seeds a table, and exposes its data through an API.

Key terms: Aspire.Azure.Data.Tables, Program.cs, PartitionKey, RowKey, ETag, Timestamp, TableServiceClient, seed data, API endpoint, HTTP response

3.7.3 Integrating with Azure Blob Storage

This section shows how an Aspire application hosts emulated Azure Blob Storage, seeds a CSV file of product specifications, and makes the file downloadable through an API endpoint.

Key terms: Blob Storage, Aspire.Hosting.Azure.Storage, Aspire.Azure.Storage.Blobs, emulated mode, products, product-specs.csv, CsvHelper, seed, CSV file, memory stream

3.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Managing infrastructure components with containers and integrations Chapter 4: Adding order-tracking functionality with SignalR →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.