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
- Host: GitHub
- URL: https://github.com/alekshura/sourceconfig
- Owner: alekshura
- License: mit
- Created: 2021-10-13T07:41:05.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-09T21:27:58.000Z (over 1 year ago)
- Last Synced: 2025-03-12T11:20:47.526Z (7 months ago)
- Language: C#
- Homepage:
- Size: 132 KB
- Stars: 12
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
#
SourceConfig
[](https://www.nuget.org/packages/Compentio.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());
}
```