AppConstant.EntityFrameworkCore 1.0.0

dotnet add package AppConstant.EntityFrameworkCore --version 1.0.0
NuGet\Install-Package AppConstant.EntityFrameworkCore -Version 1.0.0
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="AppConstant.EntityFrameworkCore" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AppConstant.EntityFrameworkCore --version 1.0.0
#r "nuget: AppConstant.EntityFrameworkCore, 1.0.0"
#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.
// Install AppConstant.EntityFrameworkCore as a Cake Addin
#addin nuget:?package=AppConstant.EntityFrameworkCore&version=1.0.0

// Install AppConstant.EntityFrameworkCore as a Cake Tool
#tool nuget:?package=AppConstant.EntityFrameworkCore&version=1.0.0

AppConstant

AppConstant is a simple type-safe constant* library for C# and ASP.NET Core. It allows you to define type-safe constants that translates to web and database friendly values.

<sub><sub>* technically not a constant</sub></sub>

Installation (coming soon)

Usage

Defining a constant

To define a constant, you need to create a class that inherits from AppConstant<TConst, TValue>. The first type parameter is the type of the constant itself, and the second type parameter is the type of the value that the constant represents.

public class MediaType : AppConstant<MediaType, string>
{
    public static MediaType Image => Set("image");
    public static MediaType Video => Set("video");
    public static MediaType Audio => Set("audio");
}

Using a constant

To use a constant, just use the static property that is defined on the constant class.

var image = MediaType.Image;

Using a constant on an entity

To use a constant on an entity, you need to define a property of the constant type.

public class Media
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MediaType Type { get; set; }
}

Using AppConstant with ASP.NET Core

To use AppConstant with ASP.NET Core, you need to register the AppConstant service after the AddControllers method.

builder.Services.AddControllers().AddAppConstant();

If your constants are defined in a different assembly, you can specify that assembly as well.

builder.Services.AddControllers().AddAppConstant(typeof(MediaType).Assembly);

Using AppConstant with Entity Framework Core

To use AppConstant with Entity Framework Core, you need to register the AppConstant in the ConfigureConventions override method in the DbContext.

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
    configurationBuilder.AddAppConstantConverters();
}

Motivation


Having worked on a few projects where constants were used, I've noticed that there are a few problems with them:

  1. You can pass any value to a method that expects a constant, and the compiler will not complain.
  2. There is no direct relation between the entity and the constant. It just lives on the entity as a primitive type.
  3. There aren't any simple implementations of constant like behavior in C#.

Inspiration


The package is heavily inspired by the SmartEnum package by ardalis. A lot of implementation details are used from that package.

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. 
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.0 177 3/14/2023

Initial release