Exercise 3.2 – Implement Manual Validation Rule

Modified on Sun, 04 Jun 2023 at 04:01 PM

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 Portal Operations and 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 the previous exercise 0.2 - Set up the Workshop Environment 


Normally we have set this up correctly, but 

  • 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 Rule...
  • Name the new rule String1ProvidedNotString2
  • Click Ok


Visual Studio will create a new file for you with this content:


using System;
using System.Collections.Generic;
using System.Linq;

namespace MigFx.Engine.Project.Target
{
    partial class FieldValidationRules
    {
        // Override the String1ProvidedNotString2() method here
    }
}
C#


Now you can implement the Rule. In fact, you can let Visual Studio and Intellisense help you out here. Delete the guiding comment and instead 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);
    }
  }
}
C#


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;

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);
            }
        }
    }
}
C#


All done. 


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


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.");
}
C#


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 the Portal Operations- 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 all the 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.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article