Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

How to create your own EnumValueObject

Steven Giesel edited this page Jul 31, 2020 · 1 revision

How To Create

There are only small steps to create your own EnumValueObject

  1. Derive from the base class with: public class MyEnumValueObject : EnumValueObject<MyEnumValueObject>
  2. And provide a private or protected constructor which is passing the key to the base class and add your string keys as public 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)
    {
    }
}

Good practices

Parameterless constructor

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.

Visibility of the constructor

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.