CHash11
CHash11

Reputation: 855

What is the ideal design for creating master tables in database and using them in application

I am designing a web application, as a first step I am creating a database. Now very basic table called Role which needs to be managed through database. This table will hold:

RoleID (Identity and Primary Key)
RoleName (Varchar)

Now in application if I need to check that for "Admin" role, enable one of the button else disable it. Then how should I write a code without hard coding "Admin" as Role?

What could be a good design in this case for storing Master like data in database and refer to it in application?

I used to do it with enum earlier but it seems its not much maintainable because if I add new Role in database then I also need to change the enum in code, e.g.

public enum Role
{
  Admin=1,
  Normal=2
}

Upvotes: 0

Views: 335

Answers (3)

the_lotus
the_lotus

Reputation: 12748

You're button don't need to check for Role but instead Permission. ex: If you page allow to view/modify/add/delete items then you'll have 4 permission

public enum Permission
{
  View = 1
  Modify = 2
  Add = 4
  Delete = 5
}

In the database, you would have a Role table that indicate which permission they have.

Admin could have View/Modify/Add/Delete Normal could have view/modify

You then set the Add button state depending on the permission of the current role. That way you don't need to care how many and which role exists. To go a bit further, each Permission could be attached to an entity.

ex: Normal could have Add/Modify/View on the Clients entity but not the Invoice entity.

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

The simplest solution would be to extend your table structure like this:

RoleID INT 
RoleName NVARCHAR
IsAdmin BIT

Or you could go with user1795177's approach of creating special permissions you assign to each role.

And please use NVARCHAR instead of VARCHAR in SQL Server to support unicode.

Upvotes: 1

Bruce The Dragon
Bruce The Dragon

Reputation: 9

Creating a permission table would be better. But simple way is just to add a field '

PermissionType  int 

and then you can assign permissions to different roles

Upvotes: 0

Related Questions