I’m thinking of writing a domain specific language (DSL) to model business objects. The DSL will not be executed, instead it will be used by a template based code generator (probably CodeSmith) to generate the .NET & SQL.
The DSL will need to support the definition of the following elements:
- Classes (name & description)
- Properties (name, friendly name, type, null/not null)
- Simple validation (required, regex, range, etc..)
- Relationships between classes (1 to 1, 1 to many, many to many)
- Inheritance (ok, maybe in version 2)
Here’s a simple example of what the DSL code might look like:
Class: Insured Desc: 'Represents a person covered by an insurance policy' Prop: FirstName, 'First Name', String(20), not null Prop: LastName, 'Last Name', String(20), not null Prop: MailAddress, 'Mailing Address', Address, not null Prop: SSN, 'Social Security Number', String(9), null Rule: RegEx, SSN, ^\d{9}$ Class: Address Prop: Line1, 'Line 1', String(30), not null Prop: City, 'City', String(30), not null Prop: State, 'State', String(2), not null ...
For the sake of keeping the DSL simple the more complex validation rules will be coded in the target language. The current plan is to make the generated code off limits and add the more complex rules to subclasses.
Has anyone written something similar to this? Can you provide any tips or links to similar solutions?
It’s fairly easy to automatically convert the structure you present into XML. From there, I imagine it’s possible to write some sort of transformation via XSLT or XQuery into whatever end result you desire. I wrote a Visual Studio add-in called CodeGenUtils to facilitate doing the transformations.
If you really feel like writing your own parser, I would suggest looking at existing textual DSL solutions such as, e.g., JetBrains MPS.