An open API service indexing awesome lists of open source software.

https://github.com/alekshura/sourceconfig

Generates configuration objects in build-time based on *.json config files
https://github.com/alekshura/sourceconfig

Last synced: 7 months ago
JSON representation

Generates configuration objects in build-time based on *.json config files

Awesome Lists containing this project

README

          

# SourceConfig

[![NuGet](http://img.shields.io/nuget/v/Compentio.SourceConfig.svg)](https://www.nuget.org/packages/Compentio.SourceConfig)
![Nuget](https://img.shields.io/nuget/dt/Compentio.SourceConfig)
![GitHub](https://img.shields.io/github/license/alekshura/SourceConfig)
![GitHub top language](https://img.shields.io/github/languages/top/alekshura/SourceConfig)

# Introduction
`SourceConfig` is a code generator for objects that are built on `*.json` configuration files:
when developer adds some file or new properties to existng json configuration file the POCO objects for this configuration generated.

It is based on [Source Generators](https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.md) feature
that has been intoduced with `C# 9.0` and brings a possibility to generate code during build time.

# Installation
Install using nuget package manager:

```console
Install-Package Compentio.SourceConfig
```

or `.NET CLI`:

```console
dotnet add package Compentio.SourceConfig
```

# How to use
During creation of any `*json` file (any `json` files are treated as configuration files), e.g. `apsetting.json`
the POCO representation of this json is generated:

```json
{
"NoteEmailAddresses": [
"admin@test.com",
"technical.admin@test.com",
"business.admin@test.com"
],
"ConnectionTimeout": "30",
"ConnectionHost": "https://test.com",
"DefaultNote": {
"Title": "DefaultTitle",
"Description": "DefaultDescription"
}
}
```
in that case `SourceConfig` generates

```cs
//
//
using System;
using System.Collections.Generic;

namespace Compentio.SourceConfig.App
{
public class AppSettings
{
public IEnumerable NoteEmailAddresses { get; set; }

public string ConnectionTimeout { get; set; }

public string ConnectionHost { get; set; }

public string DatabaseSize { get; set; }

public DefaultNote DefaultNote { get; set; }
}

public class DefaultNote
{
public string Title { get; set; }

public string Description { get; set; }
}
}
```
`AppSettings` is taken from the filename, `Compentio.SourceConfig.App` namespace is inherited from configuration file directory (here, `appsettings.json` is in app root directory, thus main app namespace is used).

>To enable processing `json` files, in `*.cproj` project the configs should be marked as `AdditionalFiles`:
>```xml
>
>
> PreserveNewest
>
>
> PreserveNewest
>
>

If there are few `appsettings` files used for different environments, e.g. `appsettings.development.json` or `appsettings.production.json` etc.
they are merged into one generated class. Merge is based on first prefix in filename - here is `appsettings`.

Now generated class can be used to retreive the configuration:

```cs
var appSettings = _configuration.Get();
```
and should be earlier added to container:

```cs
static IHostBuilder CreateHostBuilder(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();

return Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
services
.Configure(configuration)
.AddTransient());
}
```