none
Hierarchy Class RRS feed

  • Question

  • Hello,

    I have the following enumeration:

      public enum RoleEnum {
        Admin = 1,
        Leader = 2,
        Editor = 3,
        Collaborator = 4,
        Designer = 5
      }

    Usually, the higher rank is 1 and the lowest is 5 so something like:

    Admin > Leader > Editor > Collaborator > Designer

    But sometimes I have a different hierarchy. Something like:

                              > Editor

    Admin > Leader > Designer

                              > Collaborator

    So Editor, Designer and Collaborator are at the same level.

    I was trying to created this relation in a class maybe in a fluent way.

    Then have a two methods that given a role value would return the higher and lower roles.

    How would you do this?

    Thank You,

    Miguel


    Thursday, December 6, 2012 9:28 PM

Answers

  • In this case your relationship is sufficiently comlpex that it would be best to not implement it as an "enum".  An enum is designed to be (mostly) just a number that has meaning to the programmer.

    Either have a "Role" type that has both a string name and an integer permissions level, or create a hierarchy that's separate from the type.  The latter would basically be a collection of roles (which themselves could be enums, roles without any way of comparing hierarchy, etc.) and then some sort of collection.  A List<List<Role>> seems like it might apply, with all roles in the same inner list being of the same level, and with higher/lower lists having different permission levels.

    Thursday, December 6, 2012 9:55 PM

All replies

  • For the first example, just use the built-in operator LessThan: <.

    For the second example, the following code IS LEGAL and RUNNABLE:

    public enum Test {
       one,
       two,
       three
    }
    public static class EnumExtensions {
       public static int CompareTo(this Test @this, Test other) {
          if (@this == other) return 0;
          else return (@this < other) ? -1 : 1;
       }
    }
    
    public class SomeClass {
       public SomeMethod() {
          Test t = Test.one;
          IComparable ic = t;
          Console.ReadLine();
       }
    }

    Note that the process of implementing all public members of the interface IComparable is an acceptabel way of having the enum implement that interface.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    Thursday, December 6, 2012 9:47 PM
  • In this case your relationship is sufficiently comlpex that it would be best to not implement it as an "enum".  An enum is designed to be (mostly) just a number that has meaning to the programmer.

    Either have a "Role" type that has both a string name and an integer permissions level, or create a hierarchy that's separate from the type.  The latter would basically be a collection of roles (which themselves could be enums, roles without any way of comparing hierarchy, etc.) and then some sort of collection.  A List<List<Role>> seems like it might apply, with all roles in the same inner list being of the same level, and with higher/lower lists having different permission levels.

    Thursday, December 6, 2012 9:55 PM
  • In this case your relationship is sufficiently comlpex that it would be best to not implement it as an "enum".

    I would like to use an enum because it is a way of avoiding mistakes on the application.

    But after thinking I can simplify my initial idea and maybe explain it better.

    Consider the example of a school with the following roles:

      Director, Manager, Teacher, Researcher, Student

    So I would have the following (From highest permission (1) to lowest permission (2)):

      Level 1 > Director

      Level 2 > Manager

      Level 3 > Teacher, Researcher

      Level 4 > Student

    A user can have one or more roles. Let's say John, is the Director and he is a Teacher.

    As a teacher some information on the site is formatted specifically for him.

    As a teacher he can edit his documents ... As a director he can edit other teachers documents.


    In some parts of the site I only check if the user is a Teacher or a Student.

    In others I check its role and what is its higher permission level.

    Does this make sense?


    So maybe I could simply have a List<Int32, List<RoleEnum>> Permissions;

    This would give me the permissions levels I mentioned before.

    Then I would have methods such as:

      GetRolesWithLowerPermission(RoleEnum.Manager) >> Result: Teacher, Researcher, Student

    I would find where RoleEnum.Manager is ... Get its Int32 which is 2.

    Then get all list rows where Int32 > 2 and get roles in it.

    Let me know what do you think and if this can be simplified.

    Thank You,

    Miguel

    Thursday, December 6, 2012 10:41 PM