You are currently viewing App Configuration, Mock Data, and .NET 7

App Configuration, Mock Data, and .NET 7

Part 3 of 5 of the .NET series

Summary

WhatThe following is an example of how to ingest mock data from a json file into a .NET application through app configuration
WhyWhen building an app iteratively, or just doing a proof-of-concept spin-up, we can use mock data to build out the app; this approach gets to done faster
HowThe approach uses .NET 7, the web host builder, and a json file loaded as part of the app’s configuration. We can use this approach in any .NET app using Microsoft’s chained configuration — apps with web host builder, the generic host builder, or just using ConfigurationManager
This is not the only approach; mock data can be hard-coded a number of ways, including an in-memory collection add to the configuration.
MoreIf you want to understand Microsoft’s chained configuration approach, I have an article and code you can dive into
In later articles in this series, this demo app will be explored in more depth

Scaffold the App

Portfolio Project

First, spin up a scaffolded (from a template) .NET app. In this example, I spun up an ASP.NET Core Web API.

Example app uses the template, ASP.NET Core Web API, in Visual Studio 2022, with EnableOpenApi support checked

The Mock Data as Json File

Then, we represent the data in a simple json file:

The file, mockdata.json, is what I used to provide mock data in json format.

Pull Json into the App Configuration

With that in place, next, in the Program.cs after the creation of the builder and before services configuration, we add the json file to the ConfigurationManager:

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("mockdata.json");
builder.Services.AddEndpointsApiExplorer();

Typically, a .NET app may have `appsettings.json` or even corresponding files `appsetings.{environment}.json` that are applied almost at the end of the chained configuration (Azure settings, if available, will override appsettings).

Model for Data

Add a model for representing the json data array elements, then access the data wherever we want from the configuration:

public class FlattenedLabRecord
{
public string Name { get; set; } = string.Empty;
public DateTime DateMeasured { get; set; } = DateTime.MinValue;
public float Value { get; set; } = 0;
public string Units { get; set; } = string.Empty;
}
var records = builder.Configuration.GetSection("LabRecords").Get<List<FlattenedLabRecord>>();

CR Johnson

As a software engineer with over a decade of experience working for Fortune 50 companies developing software for Windows, the web, and a few interplanetary spacecraft, she's programmed in a plethora of languages including the C#/ASP.NET stack and, recently, Rails. She has tweaked more CSS files than she can count and geeks out a little on data and SQL databases. In her spare time she works on her first novel and enjoys bicycling and dark chocolate.

This Post Has One Comment

Comments are closed.