concept treatment in category .net

appears as: treatment, treatment
DSLs in Boo: Domain-Specific Languages in .NET

This is an excerpt from Manning's book DSLs in Boo: Domain-Specific Languages in .NET.

For example, we could show the user what will happen to a preferred customer with a low credit rating, or what the treatment is for all customer types with a low credit rating. We looked at how to do such things in chapter 10, and we’ll see another example of that in section 13.7.2.

The public methods are how we’ll work with the policy. As you can see in figure 13.3, this is mostly a simple matter. The only interesting tidbit here is the TreatmentForState() method’s result, the Treatment class, which is shown in figure 13.4.

Figure 13.4. The Treatment class diagram

The Treatment class holds the Description (a string representation of the treatment code), and the delegate held in the Execute property will be executed to perform the action this treatment is supposed to have.

13.5.1. Exploring the treatment of statement’s implementation

As promised, we’re going to dig into the implementation of the treatment of statement.

Listing 13.9. Converting treatment of into a TreatmentOf method call
public class TreatmentOfToMethodCall: AbstractTransformerCompilerStep { private readonly Define[] defines; public BehaviorOfToBehaviorMethodCall(Define[] defines) { this.defines = defines; } public override void Run() { Visit(CompileUnit); } public override void OnGenericReferenceExpression( GenericReferenceExpression node) { // Verify that we are indeed trying to work with // 'treatment of' generic reference expression var methodName = node.Target as ReferenceExpression; if (node.GenericArguments.Count != 1 || methodName == null || methodName.Name != "treatment") return; // Get the generic argument, which is the business definition name var genericArg = ((SimpleTypeReference)node.GenericArguments[0]); // Find a business-level definition matching the arg name var define = defines.Where(x => x.Name == genericArg.Name) .First(); // Create a method call for this var replacement = new MethodInvocationExpression( new ReferenceExpression("TreatmentOf"), define.ExpressionAsFunction, new StringLiteralExpression(define.Expression.ToCodeString()) ); ReplaceCurrentNode(replacement); } }