In previous exercises you defined manual some Rules and Bags in the Target- and  Source Map. Before you are ready to deploy the engines in the Director Runtime environment, these must be implemented.


  • Target Map
    • Validation Rule String1ProvidedNotString2
    • Mapping Rule GetCardStatus
  • Source Map
    • Bag method StringHandling.TruncateString


Of course you also defined some Lookup Rules, but you don't have to worry about them. The Engine code generator has already generated full implementations of these.


To start implementing the manual rules, please ensure that you have the Workshop solution open in Visual Studio. In addition please make sure you have enabled import of Rule implementations in Studio by performing these steps in previous exercise 0.2 - Set up the Workshop Environment 

  • Open Studio
  • Go to Tools > Options
  • Click on Manual method implementation > General
  • Check Enable import of manual method implementation
  • For Project folder, navigate to and select: Documents\MigFx\VisualStudio\Workshop\Projects\Workshop\Workshop.TargetEngine (full path: D:\MigFx\Workshop\Training\Trainees\TraineeXX\MigFx\VisualStudio\Workshop\Projects\Workshop\Workshop.TargetEngine, where XX is the trainee number)



Implement Validation Rule String1ProvidedNotString2

These are the steps to implement the Validation Rule in the Workshop.TargetEngine class library project

  • In Visual Studio
  • Locate and right-click the folder Workshop.TargetEngine\FieldValidationRule
  • In the context menu, select Add -> Class...
  • Name the new class String1ProvidedNotString2
  • Click Add

Visual Studio will create the new class for you with this content 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MigFx.Engine.Project.Target.FieldValidationRule
{
  class String1ProvidedNotString2
  {
  }
}


There are a few vital modifications you need to make at this point

  1. Visual Studio will automatically place the new class in the namespace MigFx.Engine.Project.Target.FieldValidationRule. Unfortunately this does not match the generated code. You must delete FieldValidationRule from the namespace - ending up with MigFx.Engine.Project.Target
  2. In fact you are not creating a class here, even though it looked like it. You are extending a class (FieldValidationRules) already created by the Engine Code generator in a different file. The generator marked this class as partial, making use of the ability of the c# compiler to implement the same class in multiple files. So you must modify your file to extend this class using the partial keyword

After these 2 modifications your class should look like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MigFx.Engine.Project.Target
{
  partial class FieldValidationRules
  {
  }
}


Now you can implement the Rule. In fact, you can let Visual Studio and Intellisense help you out here. Place the cursor just after the opening curly brace of the partial class FieldValidationRules insert a new line and type override followed by a space. This will bring up the Intellisense list of methods available for override:

If the method does not show up, you probably did not modify the namespace as described above. Now you can just select the correct method in the list and hit enter. Visual Studio will create the new, empty method for you with the correct method signature matching the generated code

namespace MigFx.Engine.Project.Target
{
  partial class FieldValidationRules
  {
    public override void String1ProvidedNotString2(FlagHandler flag, string String1, string String2)
    {
      base.String1ProvidedNotString2(flag, String1, String2);
    }
  }
}


All you have to do now is implement the method. Open Studio and find the code specification for the Rule in order to see what the rule is supposed to do. Below is a sample implementation of the rule

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MigFx.Engine.Project.Target
{
  partial class FieldValidationRules
  {
    public override void String1ProvidedNotString2(FlagHandler flag, string String1, string String2)
    {
      // String 1 provided, String 2 is not
      if (!string.IsNullOrEmpty(String1) && string.IsNullOrEmpty(String2))
      {
        flag(1);
      }
    }
  }
}


All done. 


Remember to save your new Rule implementation in both Studio and Visual Studio.


Common stumbling blocks here are

  1. Remember to modify the namespace
  2. Remember that the method implementing the Rule must be within the partial class FieldValidationRules


Import implementation to Studio

Now the rule is implemented, but before you are completely done you must import the implementation back into Studio and mark the Rule implemented.

  • Open the Workshop Target Map in Studio
  • Locate and Open and Check out the Rule
  • Select the Code tab and the Implementation sub-tab
  • Click the Import button. 
    • If the Import button is not visible or the correct file location is not already selected in the File dialog, you have probably not performed these steps in the previous exercise 0.2 - Set up the Workshop Environment 
      • Go to Tools > Options
      • Click on Manual method implementation > General
      • Check Enable import of manual method implementation
      • For Project folder, navigate to and select: Documents\MigFx\VisualStudio\Workshop\Projects\Workshop\Workshop.TargetEngine (full path: D:\MigFx\Workshop\Training\Trainees\TraineeXX\MigFx\VisualStudio\Workshop\Projects\Workshop\Workshop.TargetEngine, where XX is the trainee number)
  • Now the new implementation is imported into Studio
  • Check the Implemented checkbox
  • Save and Check in

What happened here?

Working in Studio in earlier exercises, you created some manual Rules for later implementation. You have now implemented the first of these. 


The Engine code generator actually did a few things on its own. It created a virtual method implementation for you in a base class. If you are interested, you can find this base class and the method in Workshop.TargetEngine\_Generated\FieldValidationRules.cs:


public virtual void String1ProvidedNotString2(FlagHandler flag, String String1, String String2)
{
    throw new NotImplementedException("The rule FieldValidationRules.String1ProvidedNotString2 is not implemented.");
}


It also included a call to this virtual method in the generated class to import the Business Object Card in the Target Engine at the point where the Interface Field CardHolderName is validated.


So when the engine executes, it will call this method. Since the method is marked as virtual - and you have overridden the virtual method - it is your implementation that will execute, not the generated implementation. But if the method is not implemented (and thus overridden), the generated implementation will execute and throw an exception to signal that fact. The generated engine will catch this exception, transform it into a System Event to eventually be reported in Tracker - and keep going.


Finally you imported your implementation back into Studio and marked the Rule implemented. By marking it Implemented, it will no longer show up in the validation list as Not implemented, so you will not go to implement it in vain the next time around.


While importing the implementation back into Studio may seem trivial for such a simple implementation, it does allow Studio to parse the implementation in order to:

  1. Perform deep validation, for instance that the Rule actually raises the all Flags specified in Studio - and none other
  2. Perform deep cross reference. Studio will pick up references to Valuesets, Constants, Relationships, other Rules and Bags etc   


In addition, other members of your team not doing actual implementations of rules may not even have Visual Studio installed on their workstations, but can nevertheless see your implementation directly in Studio.