views:

650

answers:

2

Hi,

the question is maybe composite, let me expand it:

  • does it exists a designer (stub/framework/meta-designer) to create AND/OR based rules based on .NET object public bool properties? Saved as any DSL/Boo/... output.
  • is it possible to compile the DSL output into C# expressions?

Our main problem is the gap between the documentation and the code. Our product based on hundreds of user definied rules and we want to speed up the change requests.

If we are able to give a simple designer to the users and grab the output, then after translating/compiling it into C#/IL code we have a fast change request cycle.

I know that our problem it's to specific but any "bricks in the wall" are welcome!

Example:

A C# class, subject of :

public class TestA
{
     public bool B {...}
     public bool C {...}
}

In the designer, we should able to create

  • any type of graphics designers (ie. dropdown to select public properties)

Output in DSL:

If TestA.B AND TestA.C Then Return True;

Output in C#:

if (testA.B && testA.C) { return true; }

Update #1

I would be glad with a DSL language that support using of static-typed .NET classes. I mean if the user can check the code ("Output in DSL" in the example), we don't need the designer.

Update #2

Based on the tipp, I stared with expression trees. After few days I ran into DLinq - I never was a big fan of DLinq but in this case fits the problem domain very well.

  • Easy to parse (A > 2 AND B < 4) OR C = 5 into expression trees
  • Easy to create expressions like that
  • Very easy to serialize/deserialize
  • GUI based on FlowLayoutPanel works fine as "expression builder"
+3  A: 

You could build something like this your self.

You can get a list of all the public properties for a class using Type.GetMembers()

However, instead of generating C# code, I would use expression trees.

That way you don't need to involve the C# compiler when the users change rules. Instead, you can store the rules in a database, load them at runtime, and then use the Expression.Compile() method to create a delegate you can invoke to run the code.

Update:

In the comments someone asked "What is the difference between Expression Tress and domain specific languages?"

Here's the answer:

Expression trees and domain specific languages are orthogonal things.

Expression tress are just an API for representing C# expressions, that conveniently can be converted into a delegate dynamically at runtime.

A DSL, or domain specific language, is a programing language designed to solve a narrow class of problems.

They are, essentially, completely different things.

You can use expression trees as part of a DSL implementation if you like. Linq uses them for for that purpose.

In your case, however, you don't need a DSL. What you need is a user interface that generates rules (similar to the way outlook works), and then a way of executing those rules.

Creating the UI is just normal UI development.

Expression trees are what you can use to implement the rules.

Scott Wisniewski
Our domain-object-tester application uses reflection to fill public properties and to create an instance of that objects, so thats okay.Expression tree sounds good - would you please give me some links about DSL vs. ET? Thank you
boj
I like this Expression Tree-based approach, thank you for the first shot.
boj
A: 

It's a little-known fact that the designer for Windows Workflow Foundation and its Rules Engine in particular can be hosted in a Windows Forms application separate from Visual Studio. The rules authored in this way can similarly be evaluated independent of an actual workflow.

See WF Scenarios Guidance: Workflow Designer Re-Hosting and Tutorial: Hosting the WF Designer.

John Saunders