11# Enum.Ext
22
3- [ ![ Build Status] ( https://mauracher.visualstudio.com/enum_ext/_apis/build/status/simonmau.enum_ext?branchName=master )] ( https://mauracher.visualstudio.com/enum_ext/_build/latest?definitionId=20&branchName=master )
3+ [ ![ Build Status] ( https://mauracher.visualstudio.com/enum_ext/_apis/build/status/simonmau.enum_ext?branchName=master )] ( https://mauracher.visualstudio.com/enum_ext/_build/latest?definitionId=20&branchName=master ) [ ![ Version ] ( https://img.shields.io/nuget/v/Enum.Ext.svg )] ( https://www.nuget.org/packages/Enum.Ext )
44
5- Enum.Ext provides a TypeSafeEnum that has many advantages compared to the normal .NET Enum value type
5+ Enum.Ext provides a ` TypeSafeEnum ` that has a bunch of advantages compared to the normal .NET ` Enum ` value type.
6+
7+ For example is it possible, to store additional information directly with the enum. You are also able to
8+ query an enum based on the information stored with it.
9+
10+ There is also a Json-Serializer Implemented, so you dont have to cast from DTOs manually.
611
712### Installation
813https://www.nuget.org/packages/Enum.Ext/
@@ -13,3 +18,73 @@ Enum.Ext can be installed using the following command via the NuGet package mana
1318
1419
1520### How to use
21+
22+ Simply inherit your class from ` TypeSafeEnum ` or ` TypeSafeNameEnum ` and adjust everything to your needs.
23+
24+
25+
26+
27+
28+ #### Want a weekday enum with a special string representation for each day?
29+ ``` C#
30+ public sealed class Weekday : TypeSafeNameEnum <Weekday , int >
31+ {
32+ public static readonly Weekday Monday = new Weekday (1 , " --Monday--" );
33+ public static readonly Weekday Tuesday = new Weekday (2 , " --Tuesday--" );
34+ public static readonly Weekday Wednesday = new Weekday (3 , " --Wednesday--" );
35+ ....
36+
37+ public Weekday (int id , string name ) : base (id , name )
38+ {
39+ }
40+ }
41+ ```
42+
43+ Use the enum just like the native one
44+
45+ ``` C#
46+ var day = Weekday .Monday ;
47+
48+ // Assigns Tuesday
49+ day = (Weekday )2 ;
50+ ```
51+
52+ and access the additional information easily
53+
54+ ``` C#
55+ var day = Weekday .Monday ;
56+
57+ // Prints out '--Monday--'
58+ Console .WriteLine (day .Name );
59+ ```
60+
61+ #### A fixed price that is valid for a certain time period
62+ ``` C#
63+ public sealed class YearlyPrice : TypeSafeEnum <YearlyPrice , int >
64+ {
65+ public decimal Price { get ; private set ; }
66+
67+ public DateTime ValidFrom { get ; private set ; }
68+
69+ public DateTime ValidTo { get ; private set ; }
70+
71+ public static readonly YearlyPrice Price_2018 =
72+ new YearlyPrice (1 , 15 . 99 m , new DateTime (2018 , 1 , 1 ), new DateTime (2018 , 12 , 31 ));
73+
74+ public static readonly YearlyPrice Price_2019 =
75+ new YearlyPrice (2 , 16 . 99 m , new DateTime (2019 , 1 , 1 ), new DateTime (2019 , 12 , 31 ));
76+
77+ public YearlyPrice (int id , decimal price , DateTime validFrom , DateTime validTo ) : base (id )
78+ {
79+ ValidFrom = validFrom ;
80+ ValidTo = validTo ;
81+ Price = price ;
82+ }
83+
84+ public static YearlyPrice GetPriceByDate (DateTime date )
85+ {
86+ return List .FirstOrDefault (x => x .ValidFrom <= date && date <= x .ValidTo );
87+ }
88+ }
89+ ```
90+
0 commit comments