How to define DSL over C #
For a small overnight project, I would like to write a validation component that could be used in a .NET application to perform the usual and tedious validation of an object, props, and message conditions.
My first idea was to dump all this validation setup logic into an XML config file and provide a fluid interface for people who would like to have it in code.
Since I would like to provide something that can actually be used, I thought about providing a specialized DSL (domain specific language). The question is, what tools should I use for this?
I was thinking about parsing it by hand using a regex. But personally, I would like to have something more ... useful.
So what would you suggest?
source to share
It sounds like you are talking about implementing one of the .NET 4.0 features, code contracts .
So, I believe VS.Net 2010 will be my recommended tool.
source to share
The thing with DSls is that they are rarely effective in isolation. To be useful for writing real software, you really need to be able to embed the DSL inside the host language. Compare, for example, how Linq works against plain SQL. Another good example is XML literal in VB. Both allow you to write real code in general-purpose PL and combine it with simpler declarative DSL code.
The result is something much more powerful than standalone SQL or a simple XML editor.
The downside to this, unfortunately, is that neither C # nor VB offer any metaprogramming functionality, so the only way for mainstream .net developers to do this is to create their own language. If this is something you are doing just for fun, you can modify the mono C # compiler to add the functions of interest to the language. Another alternative might be trying ruby. It has flexible syntax that allows you to get away with a lot of craziness. Personally, however, I would prefer the hacky C # approach.
source to share
You might want to check out Creating Domain Languages ββin Boo (Boo is a CLR language, concepts should carry over to C #). Sample project Simple state machine .
source to share