AppConstant 1.0.0
dotnet add package AppConstant --version 1.0.0
NuGet\Install-Package AppConstant -Version 1.0.0
<PackageReference Include="AppConstant" Version="1.0.0" />
paket add AppConstant --version 1.0.0
#r "nuget: AppConstant, 1.0.0"
// Install AppConstant as a Cake Addin #addin nuget:?package=AppConstant&version=1.0.0 // Install AppConstant as a Cake Tool #tool nuget:?package=AppConstant&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:
- You can pass any value to a method that expects a constant, and the compiler will not complain.
- There is no direct relation between the entity and the constant. It just lives on the entity as a primitive type.
- 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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on AppConstant:
Package | Downloads |
---|---|
AppConstant.AspNetCore
Model binding, JSON serialization and Swagger bindings for AppConstant |
|
AppConstant.EntityFrameworkCore
ValueConverter and configuration for using AppConstant with Entity Framework Core |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 289 | 3/14/2023 |
Initial release