How do you share configuration information or business rules between languages
Asked Answered
T

5

8

I'm looking for best practices for using the same data in different places without repeating yourself - this could include configuration or business rules.

Example 1. Data validation rules where you want to validate on the client using javascript, but you want to make sure by validating on the server.

Example 2. Database access where your web server and your cronjobs use the same password, username.

Ease of processing and a human-readable solution would be a plus.

Teodora answered 27/10, 2008 at 13:11 Comment(2)
This isn't "configuration" in the usual sense. This is code. Please drop "configuration" and put "code" or "business rules" or something else in the question.Nosography
I've added business rules to title and tags, but I think configuration applies to the second example. MySQL, for example, refers to its .my.cnf files (with user, password, host, etc) as configuration files.Teodora
G
6

Encode your data in JSON. There's a JSON library for pretty much any language you'd care to think of, or if not, it's pretty easy to code one up. If JSON is not enough, perhaps look at YAML.

Glendon answered 27/10, 2008 at 13:14 Comment(3)
I think this works well only for Example 2 (data), not for Example 1 (logic).Rob
If you want to be totally language-agnostic, you will have to encode the logic in some sort of data anyway. Of course you could go all the way and use Lisp S-expressions, so your code is data. :-)Jame
Thanks Jouni - sorry for the late acceptance. I was holding out for something that would handle example 2 as well, but yours is a good fit for now.Teodora
C
3

XML is pretty globally used. Easy to read, easy to write, and human readable. If you're concerned about the space overhead (which you actually aren't if you want human readable) then just compress it before you send it out, XML compresses quite well.

Careen answered 27/10, 2008 at 13:18 Comment(1)
Text files all generally compress well.File
C
2

See answers to this question. I think they are applicable here, especially the one with a DSL.

Chapbook answered 27/10, 2008 at 13:21 Comment(0)
B
0

As much hate as they get, for sharing data validation rules, I'm going to have to say Regular Expressions.

I know, I know, everyone hates them, but they are (generally) language-agnostic.

Bamako answered 27/10, 2008 at 14:5 Comment(1)
They're language-agnostic, only if you use a small subset of features.File
P
0
  1. Use O/S Environment Variables (envvars) to store application configuration info (such as db passwords)

  2. Validation rules often require logic. You could write your rules in JavaScript, and then run them in the browser, server (using Nashorn), and database (PLV8 with Postgres).

Photoelectric answered 1/12, 2014 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.