0

Is it acceptable to use id in HTML forms and subsequent code processing (controller, view, model, repository layers)?

For example I need to show something like this on a web page:

Select Motor Choice:
* 460/50/3
* 380/50/3
* 460/60/3

In HTML this can be done with a radio control, where each line has a value attribute, which is not shown, and text, which is shown on the screen.

I have above encoded in the database like so:

id, motor_description
1, "460/50/3"
2, "380/50/3"
3, "460/60/3"

I can use the id for the value in HTML radio control, and i can use that id internally as well in my Controller where I process the input to do lookup on the motor.

But since id is a surrogate key, and it doesn't mean anything, will I do better instead to avoid using it, and instead to perhaps use more user-meaningful information, such as the motor_description field to look up the data in the table and do other processing related to my business needs?

My concern here is ... even though I am using surrogate key in the database table, do I continue using it as well in the rest of my application, or is it best to avoid doing so?

In other words, do I let the database's surrogate key "spill out" into other aspects of my code, or do I avoid doing so?

4

Yes. You need a unique Id for these things and the DB id fits the role.

Ideally the unique id is independent of the database though. Have a GUID on your business object and use that as the DB key rather than the other way around. But 99.9% of the time its going to be a purely conceptual difference.

1

Using surrogate keys outside of a database is fine if

  • those keys are 100% immutable and are guaranteed to never change afterwards (since you typically cannot update them in external systems)

  • even though those keys get more visibility this way, you can restrain any stakeholders from inventing new "business requirements" for these keys (like "lets use a magic ID 9999 for a motor template")

  • the value of those IDs does not provide internal information to the outside world you don't want to show anyone (like a number which lets competitors see how many offers or orders of a certain kind your company gets per month)

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.