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 ?
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:
(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
Userclass that is given a bunch of roles. Here’s a simple one: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:
The only thing we’re really missing, is actions that can be performed. So, let’s define a new interface:
That’s about as simple as it gets for an action. We’ll make a dummy one:
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
RoleValidationclass. You can probably think of a better name than this.Ok, what does this allow us to do? It allows us to use our RoleValidation class to associate actions with roles….
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:
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
CanPerformfunction in our user class: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 🙂