This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
How to create your own EnumValueObject
Steven Giesel edited this page Jul 31, 2020
·
1 revision
There are only small steps to create your own EnumValueObject
- Derive from the base class with:
public class MyEnumValueObject : EnumValueObject<MyEnumValueObject>
- And provide a
private
orprotected
constructor which is passing the key to the base class and add your string keys aspublic static readonly
fields:
public class MyEnumValueObject : EnumValueObject<MyEnumValueObject>
{
public static readonly MyEnumValueObject One = new MyEnumValueObject("one");
public static readonly MyEnumValueObject Two = new MyEnumValueObject("two");
private MyEnumValueObject(string key) : base(key)
{
}
}
In its current state the base class (EnumValueObject
) does have a parameterless constructor. This is due to the reason that EF6.x and NHibernate needs them. So you are not forced to implement any constructor. Obvious if you are not providing a constructor which passes the key to the base class, you can not create any enum with that. Only implement the parameterless constructor if for example your ORM need that.
Unfortunately C# gives you no way of forcing a certain visibility. The base constructor is protected
(of course, it is an abstract class) but so should be the constructor of the derived class. You don't want your consumer to bypass the static EnumValueObject.Create
method.