AppConstant.AspNetCore
1.0.0
dotnet add package AppConstant.AspNetCore --version 1.0.0
NuGet\Install-Package AppConstant.AspNetCore -Version 1.0.0
<PackageReference Include="AppConstant.AspNetCore" Version="1.0.0" />
paket add AppConstant.AspNetCore --version 1.0.0
#r "nuget: AppConstant.AspNetCore, 1.0.0"
// Install AppConstant.AspNetCore as a Cake Addin #addin nuget:?package=AppConstant.AspNetCore&version=1.0.0 // Install AppConstant.AspNetCore as a Cake Tool #tool nuget:?package=AppConstant.AspNetCore&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 | 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. |
-
net6.0
- AppConstant (>= 1.0.0)
- Swashbuckle.AspNetCore (>= 5.0.0)
- System.Text.Json (>= 4.6.0)
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 | 236 | 3/14/2023 |
Initial release