Saturday, October 9, 2010

AOP with Enterprise Library Policy Injection Block

I used to think aspect oriented programming (AOP) is not a very useful idea because quite often it only saves few keystrokes but requires a massive configuration file. I only came to realise the true value of AOP recently when we have to convert 4+ million lines of legacy COBOL code to .NET with Microfocus’ Visual COBOL compiler. Manually adding exception handling and logging code to these existing COBOL subroutines will not only take a lot of time but also makes code merging task much harder later.

I started experimenting with the policy injection block in the Enterprise Library to see if it will make this job simpler. I tried to google for AOP and Enterprise Library tutorials but I found most tutorials on the web are either old (most were done using Enterprise Library 3) or over-complicated. Especially many of them use the Enterprise Library Logging Block to demonstrate the policy injection concept, which requires lots of configurations itself.

The policy injection in Enterprise Library is actually pretty easy to use and therefore in this tutorial I decided to demonstrate it with a simple custom call handler that requires no configuration. This is done so that you won’t be distracted by those extra settings in the App.config. Also, I’ll be using the Enterprise Library 5 configuration GUI editor, which looks quite different from the old one.

So let’s start with a basic console program with a simple interface IGreeter, which has a single operation SayHello:

public interface IGreeter
{
    void SayHello(string to);
}

and a very basic implementation, which prints a hello message to whatever name that was passed in:

public class Greeter : IGreeter
{
    public void SayHello(string to)
    {
        Console.WriteLine("hello " + to);
    }
}

Next, the Main method simply creates a greeter instance and then says hello to “bob”.

class Program
{
    static void Main(string[] args)
    {
        var greeter = new Greeter();
        greeter.SayHello("bob");
    }
}

Finally, below is a simple console logger call handler. It sits in a separate DLL (CustomCallHandlers.dll) and will be applied to the SayHello method above using the policy injection block.

[ConfigurationElementType(typeof(CustomCallHandlerData))]
public class ConsoleCallLogger : ICallHandler
{
    public ConsoleCallLogger(NameValueCollection collection) 
    { 
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine("Before " + input.MethodBase.Name);
        var returnValue = getNext()(input, getNext);
        Console.WriteLine("After " + input.MethodBase.Name);
        return returnValue;
    }

    public int Order { get; set; }
}

The implementation of the Invoke method should be pretty self-explanatory, it basically prints a before and after message around the method invocation. Check this link out if you would like to know more about how to implement the ICallHandler interface.

A couple of things to note about the ConsoleCallLogger class above. Firstly It needs to implement the ICallHandler interface and have the ConfigurationElementType class attribute. Following assembly references needs to be added to the project in order for Visual Studio to resolve these classes:

  • Microsoft.Practices.EnterpriseLibrary.Common
  • Microsoft.Practices.Unity.Interception
  • System.configuration

Secondly, a custom call handler class is required to have a constructor that accepts a NameValueCollection argument. As you’ll see when we start configuring the App.config file, you can pass name/value pair values to the custom call logger from the configuration file.

Now, to apply the ConsoleCallLogger to the SayHello method, we need to configure the policy injection block with app.config. Before we start let’s have a look what the file will look like at the end (note: I have added space in class names so it will wrap properly):

<configuration>
  <configSections>
    <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration. PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <policyInjection>
    <policies>
      <add name="My Custom Logging">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules. MemberNameMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="Member Name Matching Rule">
            <matches>
              <add match="SayHello" />
            </matches>
          </add>
        </matchingRules>
        <handlers>
          <add type="CustomCallHandlers.ConsoleCallLogger, CustomCallHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="ConsoleCallLogger" />
        </handlers>
      </add>
    </policies>
  </policyInjection>
</configuration>

Other than the ugly long class names, it's not too bad is it? You can pretty much just code this up by hand. However, since the enterprise library comes with a configuration editor, let's make use of it.

First of all we need to add a policy injection settings block in our file:

EntlibConfig1

Next, right click on the policy injection block heading (“My Custom Logging” in my example) and add a member name matching rule block.

EntlibConfig2

Enter our our target method name SayHello into the member name match field.

EntlibConfig3

After telling the policy injection block how to find the target method (i.e. our SayHello method), we need to configure the call handler (i.e. the ConsoleCallLogger class). Again, right clean on our policy injection block header and choose “Add Custom Call Handler”.

EntlibConfig4

This will popup a dialog box and if you choose “Add From File”, you can then select the location of the DLL that contains the ConsoleCallLogger class. After selecting the DLL, the ConsoleCallLogger class should show up on the tree control as shown below:

EntlibConfig5

I did run into few problems when I was adding my DLL. Here are some tips if you run into problems too:

  1. Check your build settings, if you are using 64-bits EntLib configuration editor you should build 64-bits DLL too.
  2. Check your class, is it public? did you implement the ICallHandler interface? did you remember to add the ConfigurationElementType attribute? and did you pass typeof(CustomCallHandlerData) to it?
  3. Save and restart your configuration editor. I think there might be some bugs in the configuration editor because the first time I did this I just couldn’t get the ConsoleCallLogger class to show in the tree control no matter what I did. I end up saving and restarting a couple of times and eventually it came up.

After the call handler has been added, your setting should look something similar to this:

EntlibConfig6

Now, save the app.config to the console program and add it to the project. To enable policy injection block in the console program, we first need to add following assemblies to the project:

  • Microsoft.Practices.EnterpriseLibrary.PolicyInjection
  • Microsoft.Practices.Unity.Interception
  • CustomCallHandlers (the DLL that contains the ConsoleCallLogger class).

Next we need to replace the direct object instantiation:

var greeter = new Greeter();

with PolicyInjection.Create in the Main method:

var greeter = PolicyInjection.Create<Greeter, IGreeter>();

If everything’s been configured properly, when you run the console program it’ll show the following output:

Before SayHello
hello bob
After SayHello

As you can see, the ConsoleCallLogger class has been magically applied to the SayHello method.

AOP is a very useful technique if you have to extend some legacy code without actually having to touch those code. Enterprise library already shipped with a few useful call handlers for common cross cutting concerns such as the logging and security. If none of them fits your needs you can always follow what I did in this tutorial and write your own call handlers.

Finally, you can download codes in this tutorial with the SkyDrive link below:

Thursday, September 30, 2010

COBOL Tutorial 00300 – Edited Fields

As I have mentioned in the last tutorial, you use edited fields in COBOL to format data fields into human-readable display strings. Let’s start with a numeric field:

01 NUMERIC-FIELD PIC 999999V99.

and some COBOL code that set and display the field value:

MOVE 1234.5 TO NUMERIC-FIELD.
DISPLAY NUMERIC-FIELD: ' NUMERIC-FIELD.

As we’ve demonstrated in the previous tutorial, unused digits are padded with ugly zeros:

NUMERIC-FIELD: 001234.50

Let me put my C# programmer hat on again (apologies to Java, ruby, python, C/C++, assembly and many other programmers who don’t like C#), when we have to format a variable for display, we often use the string.Format method with a formatting string containing special formatting characters, which is “0,0.00” in the following example:

// returns 1,234.50
string.Format("{0:0,0.00}", 1234.5)

Now let’s come back to COBOL, an edited field is basically a normal COBOL data field with a formatting string in the picture clause instead of the “A”, “X” or “9” data type specifiers. The edit field's formating string is based on similar ideas as C#’s one. To achieve the same output as the C# code above, I use “ZZZ,ZZZ.99” formating string as shown in the following example:

01 EDITED-NUMERIC-FIELD PIC ZZZ,ZZZ.99.

Unlike the place holding character “9”, each unused “Z” in the picture clause is not filled with “0” and the “,” just inserts a comma in the display value. Therefore, if we move the value of NUMERIC-FIELD to the EDITED-NUMERIC-FIELD and then display the content of EDIT-NUMERIC-FIELD:

MOVE NUMERIC-FIELD TO EDITED-NUMERIC-FIELD.
DISPLAY 'EDITED-NUMERIC-FIELD: ' EDITED-NUMERIC-FIELD.

The result is a much more readable output:

EDITED-NUMERIC-FIELD:   1,234.50

Keep in mind that an edited field is basically an alpha-numeric field, so you cannot perform arithmetic calculation with it. For example if you add following line of code to your program:

ADD 1 TO EDITED-NUMERIC-FIELD.

You will get the following compile error:

Error: 'EDITED-NUMERIC-FIELD' is not numeric name

This is a summary of commonly used formatting special characters:

  • “B” – Inserts a blank space.
  • “Z” – Place holder for a numeric character or space if unused.
  • “,” – inserts a comma.
  • “/” – Inserts a slash.
  • “0” – Inserts a zero.

Here are some examples to demonstrate how to use them:

Picture Clause Input Output
9999/99/99 20100101 2010/01/01
9999B99B99 20100101 2010 01 01
-ZZZ,ZZZ.99 -1234.5 -1234.50
ZZZ,ZZZ.99- -1234.5 1234.50-
X0X0X0X ABCD A0B0C0D

Finally, here's the COBOL program I used to develop the example code in this tutorial.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EDITED-FIELD.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUMERIC-FIELD PIC 999999V99.
       01 EDITED-NUMERIC-FIELD PIC ZZZ,ZZZ.99.
       01 EDITED-NEGATIVE-FIELD1 PIC -ZZZ,ZZZ.99.
       01 EDITED-NEGATIVE-FIELD2 PIC ZZZ,ZZZ.99-.
       01 EDITED-DATE-FIELD1 PIC 9999/99/99.
       01 EDITED-DATE-FIELD2 PIC 9999B99B99.
       01 EDITED-ZERO-FIELD PIC X0X0X0X.

       PROCEDURE DIVISION.
            MOVE 1234.5 TO NUMERIC-FIELD.
            DISPLAY '       NUMERIC-FIELD: ' NUMERIC-FIELD.
            
            MOVE NUMERIC-FIELD TO EDITED-NUMERIC-FIELD.
            DISPLAY 'EDITED-NUMERIC-FIELD: ' EDITED-NUMERIC-FIELD.

            MOVE -1234.5 TO EDITED-NEGATIVE-FIELD1.
            DISPLAY 'EDITED-NEGATIVE-FIELD1: ' EDITED-NEGATIVE-FIELD1.

            MOVE -1234.5 TO EDITED-NEGATIVE-FIELD2.
            DISPLAY 'EDITED-NEGATIVE-FIELD2: ' EDITED-NEGATIVE-FIELD2.

            MOVE 20100101 TO EDITED-DATE-FIELD1.
            DISPLAY 'EDITED-DATE-FIELD1: ' EDITED-DATE-FIELD1.

            MOVE 20100101 TO EDITED-DATE-FIELD2.
            DISPLAY 'EDITED-DATE-FIELD2: ' EDITED-DATE-FIELD2.
            
            MOVE 'ABCD' TO EDITED-ZERO-FIELD.
            DISPLAY 'EDITED-ZERO-FIELD: ' EDITED-ZERO-FIELD.
            STOP RUN.

Sunday, September 26, 2010

COBOL Tutorial 000200 – Data Fields

Variables are called Fields in COBOL and definitions of variables are declared in the Picture clause (can be abbreviated with PIC). Why is it called the Picture Clause? According to the book Sams Teach Yourself COBOL in 24 Hours, this is because it “paints a picture of how a field looks by defining every details and characteristic of the field”, still doesn’t quite make sense to me but anyway.

Let’s start by talking about what data fields (variables) look like in C#. When we declare a variable, the first thing we have to think about is the data type, which determines what kind of data can it hold. Normally we wouldn’t worry about the number of digits or length of the string unless we know their values can get ridiculously large or long.

int integerVariable = 12345678;
string stringVariable = "abcd1234";
decimal decimalVariable = 1234.5678m;

In a COBOL world, however, the size does matter and you have to specify both the type and the size for each data field at the same time with a special character mask in its “Picture Clause” (the PIC keyword) as shown in code below:

01 ALPHA-FIELD         PIC AAAAAAAAAA.
01 NUMERIC-FIELD       PIC 9999999999.
01 ALPHA-NUMERIC-FIELD PIC XXXXXXXXXX.

There are really only 3 types of data field in COBOL: literal, numeric and alpha-numeric and as their names suggest, they hold alphabetical, numeric and alpha-numeric characters respectively.

Let’s look at the ALPHA-FIELD first, its picture clause specifies a “AAAAAAAAAA” mask, each “A” character is a place holder for a single alphabetical character, so the ALPHA-FIELD can be used to store 10 alphabetical characters. Similarly, each “9” is a place holder for a single digit and a “X” is a place holder for a single alpha-numeric character. Therefore, the NUMERIC-FIELD and ALPHA-NUMERIC-FIELD can hold 10 digits and 10 alpha-numeric characters respectively.

To declare a decimal data field, we need to add the special character “V” in the numeric data field mask, which specifies the decimal point location. For example, the DECIMAL-NUMERIC-FIELD data field declared below let you store decimal values with up to 5 digits before and after the decimal point.

01 DECIMAL-NUMERIC-FIELD PIC 99999V99999.

As you have probably noticed, the format mask can get very ugly for large fields and hence COBOL allows you to abbreviate it with the special character followed by the number of appearances in round brackets, so for example we can abbreviate “XXXXXXXXXX” to X(10) and “99999V99999” to 9(5)V9(5).

Let’s put these all together into a small program:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EDITED.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 ALPHA-FIELD PIC A(10).
       01 NUMERIC-FIELD PIC 9(10).
       01 ALPHA-NUMERIC-FIELD PIC X(10).
       01 DECIMAL-NUMERIC-FIELD PIC 9(5)V9(5).

       PROCEDURE DIVISION.
            MOVE 'ABCEFG' TO ALPHA-FIELD
            DISPLAY '         ALPHA-FIELD: ' ALPHA-FIELD.

            MOVE 123456 TO NUMERIC-FIELD.
            DISPLAY '       NUMERIC-FIELD: ' NUMERIC-FIELD.

            MOVE 'ABC123' TO ALPHA-NUMERIC-FIELD
            DISPLAY ' ALPHA-NUMERIC-FIELD: ' ALPHA-NUMERIC-FIELD.


            MOVE 1234.5 TO DECIMAL-NUMERIC-FIELD.
            DISPLAY 'EDITED-NUMERIC-FIELD: ' DECIMAL-NUMERIC-FIELD.
            
            STOP RUN.

Notice that I’ve used the abbreviated picture clause mask (e.g. X(10) instead of XXXXXXXXXX) in the example above. Also, we haven’t covered the MOVE command yet but basically that’s how you move (assign) values into data fields in COBOL. If you compile and run this program you’ll see the following outputs in the console:

         ALPHA-FIELD: ABCEFG    
       NUMERIC-FIELD: 0000123456
 ALPHA-NUMERIC-FIELD: ABC123    
EDITED-NUMERIC-FIELD: 01234.50000

As you have probably noticed, unused digits in numeric fields are padded with zeros, which is quite ugly. We’ll cover how to make it look prettier with Edited Fields in the next tutorial.

Update: This is the 3rd revision of this COBOL data fields tutorial, I’ve decided to cut this tutorial into two parts because it was getting too long and messy.

Thursday, September 23, 2010

COBOL Tutorial 000100 – the ‘Hello World’

Due to a new project at work, I'm starting to learn COBOL (not the sexiest language, I know). I found the hardest part about learning COBOL for me is to know what COBOL keywords mean in terms of more modern programming languages. Therefore, I thought I’ll write a couple of short tutorials here to explain some of these differences in case some other programmers are interested in learning this 50+ years old programming language.

As with learning any other programming language, the first example has to be the “Hello World” and here’s the source code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLOWORLD.
       PROCEDURE DIVISION.
            DISPLAY 'HELLO WORLD'.
            STOP RUN.

Every COBOL program needs an IDENFICATION DIVISION and the PROGRAM-ID (which is HELLOWORLD in our example). All program logic will sit under the PROCEDURE DIVISION. The rest of the program should be pretty self-explanatory.

The full stop (.) is the equivalent of semi-colon (;) in C-derived programming languages, which denotes the end of a coding line.

To compile this code, I used the OpenCOBOL compiler. You can install it under Ubuntu 10.04 by typing the following command in the shell:

sudo apt-get install open-cobol

After installing the compiler, you can then compile the program by running (assuming that you’ve saved the source code in a file called helloworld.cob):

cobc -x -free helloworld.cob

The -free compiler flag tells the cobc compiler to use the free source code format. Without it, the compiler will require you to enter 7 spaces at the beginning of each line. The –x flag, on the other hand, tells the compiler to produce an executable rather than a .so file (we’ll talk about .so files later).

Finally, the compiler may produce few warnings about “dereferencing type-punned pointer” but you can just ignore them. After the compilation finishes, you will find the executable helloworld in your directory.