Separate big Enum list in different file in C#

111 Views Asked by At

I am facing a problem of scalability with a big Enum file, which has more than 1700 records.

I have to add more records to this Enum, and I am thinking to create a new Enum file to avoid adding more bloat the existing one. This allows as well to separate the Enum type by domain.

The problem is that this does not work, I have to change or duplicate or cast every function that takes this Enum type when I call the function in my new code.

This old Enum file has been created and updated manually overtime, it is not generated from a database. The project has a very very long age (probably 15+ years).

What can be a solution?

namespace SomeNameSpace
{

    public enum OldBigListDoneBySomeIrresponsiveDev
    {

        [FriendlyName("Unknown")]
        Unknown = 0,
        [FriendlyName("Page1")]
        Page1_Summary = 100000,
        [FriendlyName("Page2")]
        Page2_Summary = 100101,
        [FriendlyName("Page3")]
        Page3_Summary = 100102,
        // .....
        Page1700_Summary = 107102,
    }
}

namespace SomeNameSpace2
{

    public class SomeAttribute: FilterAttribute
    {
        public CheckEnumAttributeAttribute()
        {
            this.Order = 0;
        }

        /// <summary>
        /// Gets or sets the OldBigListDoneBySomeIrresponsiveDev
        /// </summary>
        public OldBigListDoneBySomeIrresponsiveDev AttrID { get; set; }
    }
}


// attempt to improve
namespace SomeNameSpace
{

    public enum SmallerNicerEnumList
    {

        [FriendlyName("Unknown")]
        Unknown = 0,
        [FriendlyName("Page2000")]
        Page2000_Summary = 100000,
        [FriendlyName("Page2001")]
        Page2001_Summary = 100101,
        [FriendlyName("Page2002")]
        Page2002_Summary = 100102,
    }
}
// tenative usage of the attribute with new Enum
SomeAttribute(AttrID = SmallerNicerEnumList.Page2000_Summary)
public void SomeController(){}

Raises error: Cannot implicity convert type from SmallerNicerEnumList to OldBigListDoneBySomeIrresponsiveDev

1

There are 1 best solutions below

0
Marius On

You can convert the enum to a record and use the record instead, which can be made partial, it can also be extended with implicit and the like to make it work like an enum.

Here is a code example of a console app:

Console.WriteLine(BigEnum.Unknown.ToString());
Console.WriteLine((string)BigEnum.Unknown);
Console.WriteLine((int)BigEnum.Unknown);


Console.WriteLine(BigEnum.Page2_Summary.ToString());
Console.WriteLine((string)BigEnum.Page2_Summary);
Console.WriteLine((int)BigEnum.Page2_Summary);

public partial record BigEnum(int Id, string Name)
{
    public static BigEnum Unknown => new(0, "Unknown");
    public static BigEnum Page1_Summary => new(100000, "Page1");

    public override string ToString() => Name;

    public static implicit operator int(BigEnum bigEnum) => bigEnum.Id;
    public static implicit operator string(BigEnum bigEnum) => bigEnum.Name;
}

public partial record BigEnum
{
    public static BigEnum Page2_Summary => new(100101, "Page2");
}