Enum “Inheritance”

后端 未结 15 1483
野性不改
野性不改 2020-11-22 07:21

I have an enum in a low level namespace. I\'d like to provide a class or enum in a mid level namespace that \"inherits\" the low level enum.

namespace low
{
         


        
15条回答
  •  感情败类
    2020-11-22 08:20

    Alternative solution

    In my company, we avoid "jumping over projects" to get to non-common lower level projects. For instance, our presentation/API layer can only reference our domain layer, and the domain layer can only reference the data layer.

    However, this is a problem when there are enums that need to be referenced by both the presentation and the domain layers.

    Here is the solution that we have implemented (so far). It is a pretty good solution and works well for us. The other answers were hitting all around this.

    The basic premise is that enums cannot be inherited - but classes can. So...

    // In the lower level project (or DLL)...
    public abstract class BaseEnums
    {
        public enum ImportanceType
        {
            None = 0,
            Success = 1,
            Warning = 2,
            Information = 3,
            Exclamation = 4
        }
    
        [Flags]
        public enum StatusType : Int32
        {
            None = 0,
            Pending = 1,
            Approved = 2,
            Canceled = 4,
            Accepted = (8 | Approved),
            Rejected = 16,
            Shipped = (32 | Accepted),
            Reconciled = (64 | Shipped)
        }
    
        public enum Conveyance
        {
            None = 0,
            Feet = 1,
            Automobile = 2,
            Bicycle = 3,
            Motorcycle = 4,
            TukTuk = 5,
            Horse = 6,
            Yak = 7,
            Segue = 8
        }
    

    Then, to "inherit" the enums in another higher level project...

    // Class in another project
    public sealed class SubEnums: BaseEnums
    {
       private SubEnums()
       {}
    }
    

    This has three real advantages...

    1. The enum definitions are automatically the same in both projects - by definition.
    2. Any changes to the enum definitions are automatically echoed in the second without having to make any modifications to the second class.
    3. The enums are based on the same code - so the values can easily be compared (with some caveats).

    To reference the enums in the first project, you can use the prefix of the class: BaseEnums.StatusType.Pending or add a "using static BaseEnums;" statement to your usings.

    In the second project when dealing with the inherited class however, I could not get the "using static ..." approach to work, so all references to the "inherited enums" would be prefixed with the class, e.g. SubEnums.StatusType.Pending. If anyone comes up with a way to allow the "using static" approach to be used in the second project, let me know.

    I am sure that this can be tweaked to make it even better - but this actually works and I have used this approach in working projects.

    Please up-vote this if you find it helpful.

提交回复
热议问题