AppConfigurationEFCore 1.0.1

dotnet add package AppConfigurationEFCore --version 1.0.1
                    
NuGet\Install-Package AppConfigurationEFCore -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AppConfigurationEFCore" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AppConfigurationEFCore" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="AppConfigurationEFCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AppConfigurationEFCore --version 1.0.1
                    
#r "nuget: AppConfigurationEFCore, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AppConfigurationEFCore@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AppConfigurationEFCore&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=AppConfigurationEFCore&version=1.0.1
                    
Install as a Cake Tool

AppConfigurationEFCore

Github

Configurable, type-aware service for storing application configuration in database table.

Dependencies

Entity Framework Core 6.0.4,

Microsoft.Extensions.DependencyInjection.Abstractions

Usage

Table in database and DbSet<>

It is important to add DbSet<AppConfig> to DbContext used in your application (and create database table with migration, Add-Migration AppConfiguration in console):

public class YourDbContext : DbContext {
  public SADbContext(DbContextOptions<SADbContext> options) : base(options) { }
  
  ...
  
  protected DbSet<AppConfigurationEFCore.Entities.AppConfig> _Config { get; set; } = null!;
  ...
}
Library setup

In Program.cs add using AppConfigurationEFCore.Setup; at the top and budilder.Services.AddAppConfiguration<YourDbContext, YourRecordsConfiguration>(options => { ... }); somewhere below:

using AppConfigurationEFCore.Setup;
...
using Microsoft.EntityFrameworkCore;
...

builder.Services.AddAppConfiguration<YourDbContext, YourRecordsConfiguration>(options =>
{
    options.AddVT<DateTime>(x =>
    {
        if (DateTime.TryParse(x, out var date))
            return date;
        return null;
    });
});
Records configuration

Type YourRecordsConfiguration is where you define records used in your application. There are rules, how this class should be structured (it is also explained in summary of AddAppConfiguration method):

TRecords type must have properties of type RecordHandler<T> (for records that represent reference types) or VTRecordHandler<T> (for records that represent value types, like int, decimal).

Additionaly you can have properties of own type for grouped records, for more see below.

Each property must have attribute RecordKeyAttribute with the key of that record.

Example:

public class AppConfigRecords 
{
    [RecordKey("name")]
    public RecordHandler<string> ApplicationName { get; private set; } = null!;
    
    [RecordKey("maxItemsPerPage")]
    public VTRecordHandler<int> MaxItemsPerPage { get; private set; } = null!;
    
    [RecordKey("defaultValueOfSth")]
    public VTRecordHandler<int> SomeDefaultValue { get; private set; } = null!;
    
    [RecordKey("initialDate")]
    public VTRecordHandler<DateTime> InitialDate { get; private set; } = null!;
    ...
}
Records group

It is possible to have in Records Configuration own class with properties like in Records Conrifuration (of type RecordHandler<T>, VTRecordHandler<T>, or another nested record group).

Example:

public class AppConfigRecords {
    [RecordGroup(GroupKey = "inner")]
    public NestedRecordsConfig Inner { get; private set; } = null!;
}
...
public class NestedRecordsConfig {
    [RecordKey("name")]
    public RecordHandler<string> Name { get; private set; } = null!;
    
    [RecordKey("amount")]
    public VTRecordHandler<int> Amount { get; private set; } = null!;
}

Or:

public class AppConfigRecords {
    public NestedRecordsConfig Inner { get; private set; } = null!;
}
...
[RecordGroup(GroupKey = "inner")]
public class NestedRecordsConfig {
    ...
}
Record types

By deafult only int, decimal and string record handlers are available. To add any desired type, use customRecordTypesAction parameter in AddAppConfiguration. You can add support for your own reference types (with Add<T>(...) method) or readonly structs (with AddVT<T>(...)). Those methods require conversion function from string? to [yourType]?, the opposite direction is optional ([...].ToString() will be used by default).

Instead of functions, you can also pass an object whose class implements interface IRecordHandlerRule<T> or IVTRecordHandlerRule<T>:

class CharHandler : IVTRecordHandlerRule<char>
{
    public string? FromType(char? en) => en is not null ? $"{en}" : null;

    public char? ToType(string? db) => db?.FirstOrDefault();
}

...
budilder.Services.AddAppConfiguration<YourDbContext, YourRecordsConfiguration>(options => {
    options.AddVT(new CharHandler());
});

or even built-in JsonRecordHandlerRule<T>:

private class SimpleClass
{
    public string Name { get; set; }
    public int Number { get; set; }
}

...
budilder.Services.AddAppConfiguration<YourDbContext, YourRecordsConfiguration>(options => {
    options.Add(new JsonRecordHandlerRule<SimpleClass>());
});
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 658 6/12/2022
1.0.0 576 6/5/2022