Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save C-Duv/80f2d8f205de651fedf8 to your computer and use it in GitHub Desktop.
Save C-Duv/80f2d8f205de651fedf8 to your computer and use it in GitHub Desktop.
How to handle, the unique references/codes/ids in both source code and storage (eg. database, flat file, etc.)?

TL;DR: Say you have, in a website application, a permission system (user can/cannot access specific section). You need a "name" for this permission/right (eg. can_access_admin) which will somehow be used by storage system (to keep track the fact a user has this right) and into source code (to ask fetch into storage to see if user has the right to access admin area). What form this right "name" has? bits? string? How to use it efficiently in source code: as it (copy/paste)? constant?

When designing a website application you oftenly use concepts such as user permission (eg. access to admin area is granted) and preference (eg. maximum number of results to display in projects list) that will be used among all application.

It boils down to a reference/ID in the form of a string (eg. can_access_admin), bit sequence (eg. 10111) or even integer (42) to use in your source code:

  • Where you fetch value/presence from storage
  • Where you check/compare
  • Where you display UI forms to change values

Note that the same concept will surely be used in storage (in the same form or in another):

  • Database: user-rights map table.
  • Configuration file: default values of preferences.

The issue I have is: how to avoid copy/pasting that "reference" over all the source code (which will be a real pain if you ever need to change that in the future)? It can be abstracted into language's constant or even class but it will create more versions of the same concept and this cannot always be used into storage.

See files 2.configuration.xml, 3.database.sql and 4.preferences_view.html.

In 4.preferences_view.html, the actual "XXXXXX" value is either fetched from Storage (when user already has defined preference): 150 or from configuration file (when user has no preference set): 100 The controller populating that view have to deal with:

  • HTML input name: result-display-limit
  • DB Storage code name: result_display_limit
  • Configuration Storage node path: result_display/limit
<?xml version="1.0" encoding="UTF-8"?>
<config>
<default_values>
<result_display>
<limit>100</limit>
</result_display>
</default_values>
</config>
CREATE TABLE `user_preferences` (
`user_id` INT UNSIGNED NOT NULL COMMENT 'The user\'s ID',
`preference_code` VARCHAR(50) NOT NULL COMMENT 'The code of the set preference',
`value` TINYTEXT NULL COMMENT 'Value of that pref for this user'
)
INSERT INTO `user_preferences` (`user_id`, `preference_code`, `value`)
VALUES
(42, 'result_display_limit', '150');
<h2>User preferences</h2>
<form id="user-prefs">
<label for="result-display-limit">Maximum number of results to display&nbsp;:</label>
<input type="text" id="result-display-limit" name="result-display-limit" value="XXXXXX" />
<input type="submit" />
</form>
@C-Duv
Copy link
Author

C-Duv commented Sep 9, 2014

Gisted for better explanation of https://twitter.com/C_Duv/status/509095871531982848.

@tentacode
Copy link

I use const, and labels inside const, no number, much more readable in a database for example

@C-Duv
Copy link
Author

C-Duv commented Sep 9, 2014

And you use the same name for the constant and for the database "key"?

@C-Duv
Copy link
Author

C-Duv commented Sep 9, 2014

Side question: let's say you have a database table with a list of message templates (id, subject, message_template). In some processes for you application, you will send a message using a given template (eg. Notify a salesman a new client has just subscribed).

First, once you do that: your code is tied to a certain dataset right?

Secondly, as templates can be added by the users during production life cycle you cannot depend on a numeric ID to fetch the right template for the event. So you'll use unique code string there too? And fetch the template via this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment