Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6789099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:33:49+00:00 2026-05-26T17:33:49+00:00

I’m developing a system in C# (.NET 4.0) which will be used by various

  • 0

I’m developing a system in C# (.NET 4.0) which will be used by various kind of users.

Each user type will only be allowed to interact with the application in a specific way. For example, user A should only be able to do operations 1, 2 and 3. User B should only be able to do operation 2, 3 and 4. Etc.

I decided to implement an interface called “Role”, which is implemented by various classes such as “Administrator”, “Reader”, etc. A user may have multiple roles (defined as a list of “Roles”).

I’d like each role (“Administrator”, “Reader”, etc.) to have a list of allowed operations. I was thinking of defining an enum somewhere which will contain all operations possible within the application. Each “Role” would then have a list of items found in that enum.

I find this rather unoptimized, since I will have to always make sure to keep this enum up-to-date as I implement new functionalities in my application.

What approach would you recommend that I use ? Are there any design patterns that can help me out ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:33:49+00:00Added an answer on May 26, 2026 at 5:33 pm

    I like this question, it’s a good one. Whenever I come across problems like this, I always start to think about interfaces, actions and how they work together. Usually a factory or two suddenly decides that its presence needs to be felt in the final solution. So let’s take a look at some interfaces that your problem consists of.

    First up, we have a role. A role can be anything and is entirely business-specific as your question states. So, the role can actually be quite simple:

    public interface IRole
    {
        public string Name { get; }
    }
    

    (As you can probably tell, I am keeping this very simple). That’s all a role is, really. A title. You might think it overkill just to have an interface that has a name parameter, but roles might well get more complex in at a later point.

    Ok, we’ve defined an abstract concept of a role. I guess we’re going to have some kind of a User class that is given a bunch of roles. Here’s a simple one:

    public class User
    {
        private List<IRole> roles_ = new List<IRole>();
    
        /* .. usual suspects */
    
        public void AddRole(IRole role)
        {
            Debug.Assert(role != null);
            Debug.Assert(!HasRole(role));
            roles_.Add(role);
        }
    
        public void RevokeRole(IRole role)
        {
            Debug.Assert(role != null);
            roles_.Remove(role);
        }
    
        public bool HasRole(IRole role)
        {
             Debug.Assert(role);
             return roles_.Contains(role);
        }
    
    
        public bool CanPerform(IAction action)  /* to come */ 
    }  // eo class User
    

    Right now, we have a definition of a role, a concrete User class and the ability to add and revoke roles to a particular user. Time to define some concrete roles:

    public class AdminRole : IRole
    {
        public string Name { get { return "Admin"; } }
    }
    
    public class ReaderRole : IRole
    {
        public string Name { get { return "Reader"; } }
    }
    

    The only thing we’re really missing, is actions that can be performed. So, let’s define a new interface:

    public interface IAction
    {
        string Name { get; }
        void Perform();
    }
    

    That’s about as simple as it gets for an action. We’ll make a dummy one:

    public class InvoiceAction : IAction
    {
         public string Name { get { return "Invoicing"; } }
         public void Perform()
         {
             /* Invoice somebody! */
         }
    
         public InvoiceAction(/* Some pertinent details*) { }
    }
    

    Right, we’ve got our basic interfaces and concrete classes in place. We now need to come up with a way that actions can be associated with roles. For the purposes of this example, I’m going to go with a RoleValidation class. You can probably think of a better name than this.

    public sealed class RoleValidation
    {
        private Dictionary<System.Type, List<IAction>> roleMap_ = new Dictionary<System.Type, List<IAction>>(); // this is our mapping of roles to actions
    
        // ctor ignored for brevity
    
        public void AssignActionToRole<ROLE>(IAction action)
        {
            Debug.Assert(action != null);
            if(roleMap_.ContainsKey(typeof(ROLE)))
                roleMap_[typeof(ROLE)].Add(action);
            else
            {
                List<IAction> actions = new List<IAction>();
                actions.Add(action);
                roleMap_[typeof(ROLE)] = actions;
            }
        }
    
        // Nice generic function for ease of use
        public List<IAction> GetActions<ROLE>() where ROLE : IRole
        {
            foreach(KeyValuePair<System.Type, IAction> pair in roleMap_)
            {
                if(typeof(ROLE).IsAssignableFrom(pair.Key.GetType()))
                    return pair.Value;
            }
            return null;
        }
    
        // for everyone else, non-explicit
        public List<IAction> GetActions(System.Type type)
        {
            foreach(KeyValuePair<System.Type, IAction> pair in roleMap_)
            {
                if(type.IsAssignableFrom(pair.Key.GetType()))
                    return pair.Value;
            }
            return null;
        }
    } // eo class RoleValidation
    

    Ok, what does this allow us to do? It allows us to use our RoleValidation class to associate actions with roles….

    RoleValidation rv = new RoleValidation();
    rv.AssignActionToRole<Administrator>(new InvoiceAction());
    rv.AssignActionToRole<Reader>(new OtherAction());
    

    I realise that there could be duplication of actions, and that is left as an exercise for the reader, as you could potentially create an action factory and use two generics here instead, e.g:

    cv.AssignActionToRole<Administrator, InvoiceAction>();
    

    And basically have a map of types, rather than concrete instances. That would be my preferred way of doing things.

    Now it’s type to implement that CanPerform function in our user class:

    public class User
    {
        /* you know the rest */
        public bool CanPerform(IAction action)
        {
            /* code assumes RoleValidation is available via some method or whatever */
            foreach(IRole role in roles_)
            {
                if(RoleValidation.Instance.GetActions(typeof(role)).Contains(action))
                    return true;
            }
            return false;
        }
    }
    

    I admit, I have thrown this answer together without actually compiling it and constructing a working example, so I apologise if there are errors. This is one approach you could take to solving this and having dynamic actions, assigned to arbitrary roles and users with multiple roles. It could get more interesting if we consider roles might derive from one another, but that’s a different story 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.