User/Roles design


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: User/Roles design

  1. #1
    Join Date
    May 2006
    Posts
    1

    User/Roles design

    This may be quite trivial for many of you, but it's not my case...

    I am trying to implement roles on a user class I've created. What I thought of was creating 3 classes:
    - Role
    - Roles (a container for all the existing roles)
    - User

    Each User should be able to contain none, one or many Role objects. My idea was to create a member in the User class, which consists of a collection where I can store each Role.

    I also thought of having only one Role object per specific role, so that when, for example, the name of a Role in the Roles container is changed, the change is reflected in each User belonging to that Role (just because those are two references to the same object).

    If someone wants to delete a Role, he should call a remove method from the Roles container (that removes the Role from the container and from every User). However, and this is what I don't like about this design, someone (just making a mistake) may try to do that by getting the Role from the container and setting it to null. I don't want to rely on the fact that the programmer will do things as they are suposed to be done, so... is there any way in which I can be sure that a Role object won't be set to null?

    It's obvious that that doesn't make sense inside theses 3 classes (User, Role, Roles), but I'd really like to be able to achive that from outsider classes that I may not implement.

    Though they don't convince me, my thoughts for solving this were:
    - Never return the unique Role objects, but just clones of them. The drawback for this is that to keep the User roles synchronized I must update changes when modifying a Role property, or adding the Role object from the Roles container that is equal to the clone created.
    - Creating an id for each Role and returning that instead of the Role object. In that case, whenever I need to calculate the roles of a User I would have to look up each id in the Roles container.

    I hope my question is clear enough... any answer is really welcome.

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    However, and this is what I don't like about this design, someone (just making a mistake) may try to do that by getting the Role from the container and setting it to null. I don't want to rely on the fact that the programmer will do things as they are suposed to be done, so... is there any way in which I can be sure that a Role object won't be set to null?
    you don't have to worry. there is no possibility for a programmer to set your role to null untill invoking the method on the container.
    imagine:
    Code:
    myrole = container.getRole("X");
    myrole = null;
    this will not delete or set the null value in the container. myrole is just a reference to the main role, not the only one.
    for null values you should check the container.addRole()-Method, so that it will not accept any null values, eg:

    Code:
    public void addRole(Role role){
      if (role==null) throw new IllegalArgumentException("no null allowed");
    }
    also your Role object should have no delete method, instead the delete method should be contained in the container.

    and at last: if you don't want your programmers to modify any predefined roles, define a read-only class for Roles, which only contain getters but no setters. but this is not a very proper aproach.

    Code:
    public class UnmodifyableRole{
      public String getName()...
    }

Similar Threads

  1. Database Design Help needed
    By Brian Pittman in forum Database
    Replies: 2
    Last Post: 04-29-2007, 08:23 AM
  2. Design is the devil, heard of intellectual property rights
    By John Galt in forum Architecture and Design
    Replies: 1
    Last Post: 03-20-2002, 07:39 AM
  3. Microsoft needs to visit Design school !
    By Franklin in forum .NET
    Replies: 3
    Last Post: 03-04-2002, 05:27 PM
  4. Replies: 0
    Last Post: 12-15-2000, 05:08 PM
  5. Design patterns
    By Ronadl in forum VB Classic
    Replies: 1
    Last Post: 09-15-2000, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center