0

I am building a web page for a business application. The page displays the information of a project. There are several buttons whose display depend on the data of the project, let's say it depends on codeC.

I am wondering what is recommended to handle the business rules of these buttons.

Option 1 :

Java Enumeration that lists the valid codeC values or exposes a method that calculates whether the button should be displayed.

  • Pros: fast implementation
  • Cons: the codeC values may change or new codeC values may appear... this would require a rebuild. This rebuild would probably be needed for other reasons anyway, but still... it is not as scalable as the database solution.

Option 2 : Database table with the name of the button, its title, the codeC values that allow its display... + a helper class that would calculate for any button if it should be displayed...

  • Pros: scalability, factorization.
  • Cons: logic is held in different places, not as fast as option 1
Adrien Gorrell
  • 1,291
  • 3
  • 14
  • 28
  • @LuiggiMendoza: but within that security manager, Lyth's code still has to make decisions. I feel like he's asking where to store those decisions: an enum? the dbase? inline code in the view? Etc. – Charles Wood Jun 16 '14 at 15:28
  • I have not been thinking of a security manager because I don't actually have a proper user... Maybe my description of the problem is wrong. Let me edit... – Adrien Gorrell Jun 16 '14 at 15:32
  • @CharlesWood view logic belongs to the view itself. Depending your requirements, you will decide to store the data to fulfill the permissions in a plain properties file or in an external database that can only be accessed through web services. Since there's no specific requirement (or quality attribute) that shows the limitations for this application, there's no exact answer. – Luiggi Mendoza Jun 16 '14 at 15:33
  • Forget everything about permissions... it does not depend on a permission in that particular case. – Adrien Gorrell Jun 16 '14 at 15:39
  • @LuiggiMendoza this is something I've been thinking about, actually. Doesn't "should I display this button" count as view logic? – Charles Wood Jun 16 '14 at 15:46
  • @Lyth sorry if we are hijacking your question :) Are you using MVC? – Charles Wood Jun 16 '14 at 15:46
  • @Lyth buttons are used to allow actions for users. This is globally known as permissions for users. I guess you should explain more on the problem to know these *specific business rules*. – Luiggi Mendoza Jun 16 '14 at 15:52
  • @Charles Wood : Yes, I am using GWT... so well, MVP more than MVC. – Adrien Gorrell Jun 16 '14 at 15:55
  • @LuiggiMendoza At the current state of the project we don't have users. No one is logged in, we have context information coming in though. Our webservice is called to create a context on the server for a given project id. Then when the person accesses the specific url of the project, the context is loaded. – Adrien Gorrell Jun 16 '14 at 16:02
  • @LuiggiMendoza in the loaded context, I have information that could be relative to some user notion, like callerTypeId. So I have two separate info that condition the display of buttons : callerTypeId (could be a user notion), codeC (only a project notion). – Adrien Gorrell Jun 16 '14 at 16:10
  • @LuiggiMendoza Maybe I should just create a user-session with just a callerTypeId as user-info... and then what ? I'd have to have a switch on the callerTypeIdEnum... so the user-session doesn't bring anything for the moment. – Adrien Gorrell Jun 16 '14 at 16:22
  • Then you're looking for a `` in your JSP... or probably a ``. Just to know, are you using ajax for this request as well? – Luiggi Mendoza Jun 16 '14 at 16:23
  • No, the page is generated as is in that specific case. GWT creates it through java. Soooo, well, I guess this solves callerTypeId's problem. And about codeC it seems a database is still recommenced considering the answers. Thanks for your help. – Adrien Gorrell Jun 16 '14 at 16:26
  • Again, it will depend on many factors that are covered in your specific requirements. For example, if the rules don't change too much and a change on these rules means a project redeploy, then use a properties file; if the changes should be done *on the fly* then use a database, and you can load the data in a cache when the application starts and define a cache update based on database updates (more complex scenario but better performance) or just load the permissions per request (easier to maintain and handle permission changes but worse performance). Again: it's all up to requirements. – Luiggi Mendoza Jun 16 '14 at 16:31
  • 1
    The lesson here: there's no generic answer for this question. – Luiggi Mendoza Jun 16 '14 at 16:31

2 Answers2

2

Typically what I do in this sort of scenario is Option 2, but with a website cache so that the DB reads are just as fast as the compiled code, except for the first read after the DB has been modified. Pseudocode:

if (Cache["Statuses"] is null)
{
    Cache["Statuses"] = LoadStatusesFromDB();
}
return Cache["Statuses"]; // this might be a Dictionary

The trick is to figure out how to delete the Cache every time the Statuses are updated in the DB. That would obviously be application specific.

BTW, I've never done this in Java, but the concept should be language independent. I do it all the time in ASP.NET to increase performance of retrieving data that is needed quite often in comparison to how often it changes.

TTT
  • 22,611
  • 8
  • 63
  • 69
0

I think this is a simple cost/benefit analysis. The real question to ask is, "How often am I really going to be updating codeC? If you know you will be changing it a lot, use a database. If you're just "planning for the future" wherein it might change a lot, it might be better to do the simpler implementation and then save the more flexible one for the future as well.

The nice thing about programming for a business is that you have a manager (I presume), and that manager might be better equipped to decide how flexible the application needs to be and how they would like you spending your time, so that you don't spend all day going down a rabbit hole.

Charles Wood
  • 864
  • 8
  • 23
  • Yes... The problem is... all the income isn't known as of yet. If I ask now, I know for sure they'll tell me that it won't change so much. And suddenly later they'll tell me something that will absolutely need a database table this time... – Adrien Gorrell Jun 16 '14 at 16:29
  • 1
    And if you implement it with a database, they'll never change any value again. Welcome to enterprise development! :D – Charles Wood Jun 16 '14 at 16:36
  • 1
    @CharlesWood you may use a properties file instead of a database and you're still doing enterprise development... – Luiggi Mendoza Jun 16 '14 at 16:58
  • @LuiggiMendoza sure, I was just referring to working for a large company ;) – Charles Wood Jun 16 '14 at 17:28
  • @CharlesWood using properties file approach works for a large company as well, and it has a better performance than storing it from database and has better scalability. Again, as noted in my comments in OP's question, the decision depends on your specific non-functional requirements i.e. your [quality attributes](http://www.sei.cmu.edu/reports/95tr021.pdf). – Luiggi Mendoza Jun 16 '14 at 17:30