Tag Archives: Visual COBOL

AzureKit and COBOL

AzureKit is a nice piece of source code released on CodePlex by Mark Rendle. The AzureKit makes life much easier for creating, searching, deleting elements in your Azure’s Table Storage.

My idea was that if the AzureKit was easy enough to use, then it could give us a nice way of accessing Azure’s table storage without to hosted in Azure itself.

Given, this idea I recently spend a bit of time exploring the APIs and how they could be used from COBOL and to be honest it was a pleasure to use, so thank you Mark for the good work.

For anyone not familar with Azure Table Storage, it is a structured storage that uses tables.. sounds obvious. It does not have a SQL interface, so it is marked as a NoSQL storage mechanism.

The normal Azure Table Storage APIs are fine but reasonably complicated… perhaps too complicated for most people to use.

The APIs Mark has put together really does make life easier.

The AzureKit provides you with an IDictionary, IEnumerable and IQueryable style interface which for anyone familar with .Net this API style will feel very natural to them, so I think it gives you a nice programming model.

This combined with Visual COBOL’s support for doing ‘perform thru’ on IQueryable objects make its easy to use.. Anyway enough talking time for some code…

      program-id. Program1 as "AzureKitExample.Program1".

       data division.
       working-storage section.
       01 movieTable    type AzureKit.Table.
       01 movie         type IDictionary[string, object].
       01 movieQuery    type IEnumerable[
                                   type IDictionary[string, object]].
       01 movieIQuery   type System.Linq.IQueryable[
                                   type IDictionary[string, object]].
       
       procedure division.
           set type Azure::Account to "mystorageaccount"
           set type Azure::SharedKey to "myverylargesharedkey.........."
           set movieTable to new type AzureKit.Table("Movie",
                            type AzureKit.IfTableDoesNotExist::CreateIt)
           
           *> Delete ALL
           perform varying movie thru movieTable::GetAllRows()
               invoke movieTable::Delete(movie)
           end-perform
           
           *> All two items
           set movie to type AzureKit.Table::NewRow(
                                   "Jaws: The Revenge",
                                   type Guid::NewGuid()::ToString())
           set movie::Item("Year") to 1987
           set movie::Item("Director") to "Joseph Sargent"
           set movie::Item("Rating") to "Worst Movie Ever!"
           invoke movieTable::InsertRow(movie)
           
           set movie to type AzureKit.Table::NewRow(
                                   "Empire Strikes Back",
                                   type Guid::NewGuid()::ToString())
           set movie::Item("Year") to 1982
           set movie::Item("Director") to "George Lucas"
           set movie::Item("Rating") to "Best Movie Ever!"
           invoke movieTable::InsertRow(movie)
           
           *> query
           set movieQuery to movieTable::Query(
                                 "PartitionKey eq 'Jaws: The Revenge'")
           
           set movieIQuery to type System.Linq.Queryable::AsQueryable[
                                 type IDictionary[
                                     string, object]](movieQuery)
           
           set movie to type System.Linq.Queryable::FirstOrDefault[
                                 type IDictionary[
                                     string, object]](movieIQuery)
           
           display movie::Item("PartitionKey") " -> "
                                   movie::Item("Director") " -> "
                                   movie::Item("RowKey")      
         
           *> show the results
           perform varying movie thru movieQuery
                  display movie::Item("PartitionKey") " -> "
                                  movie::Item("Director") " -> "
                                  movie::Item("RowKey")              
           end-perform
           
           goback.
           
       end program Program1.

That’s pretty easy… now what shall I do with the APIs… answers on the comment strip!

http://blog.markrendle.net/

http://azurekit.codeplex.com/

http://msdn.microsoft.com/en-us/library/dd179423.aspx

Method Chaining

Creating objects with a complex constructor can be a bit of a pain in any language. One technique I have used is method chaining. It is not applicable to every type of class but it can be useful.

Method chain can help simply the use class and allows more complex object initialisation without having to worry about the order of the parameters and be done inline.

Consider the use of an “Account” class that takes Name, Address, Telephone and Country. All of which are strings, the constructor with four strings would not be a great constructor.

So you could have a simple constructor and four properties/methods.. however so set up the object would mean you have spread the setup over multiple lines.

Using method chain you can overcome this and even space in the “value” area of the object in your storage area.

set x to new type Account::Name("xx")::Address("yy") ::Telephone("yy")
     ::Country("zz")::World("Earth")

The trick of the pattern is to provide methods that always return this/self, so we can change the invokes together… For example:

This technique could even be used to build up a series of items required, for example, the preparation and execution of a sql statement comes to mind… in a similar fashion to Linq.

So… lets look at an example:

$set ilusing"System.Collections.Generic"
program-id. Program1 as "MethodChaining1.Program1".
data division.
working-storage section.
01 accounts type List[type Account] value new type List[type Account].      
01 jAccount type Account value
   new type Account::Name("Mr Johnson")::Address("Somewhere, some place")
    ::Telephone("+44 1234 4321").
01 sAccount type Account value
   new type Account::Name("Mr Smith")::Address("Nowhere place")
     ::Telephone("+44 1234 4321")::Country("Wales").                
01 lAccount type Account.
procedure division.
 invoke accounts::Add(jAccount)
 invoke accounts::Add(sAccount)
 
 perform varying lAccount through accounts
  display lAccount
 end-perform

 goback.
end program.

WIth the class being:

class-id Account.

working-storage section.
01 wName       string property as "Name".
01 wAddres     string property as "Address".
01 wCountry    string property as "Country".
01 wTelephone  string property as "Telephone".
01 wEmail      string property as "Email".

method-id New.
local-storage section.
procedure division.
  set wCountry to type System.Globalization.RegionInfo::CurrentRegion::DisplayName
end method.

method-id Name public.
procedure division using uName as string
 returning ret as type Account.
  set self::Name to uName
  set ret to self
end method.

method-id Address public.
procedure division using uAddress as string
 returning ret as type Account.
  set self::Address to uAddress
  set ret to self
end method.

method-id Telephone public.
procedure division using uTelephone as string
 returning ret as type Account.
  set self::Address to uTelephone
  set ret to self
end method.

method-id Email public.
procedure division using uEmail as string
 returning ret as type Account.
  set self::Address to uEmail
  set ret to self
end method.

method-id Country public.
procedure division using uCountry as string
 returning ret as type Account.
  set self::Country to uCountry
  set ret to self
end method.

method-id ToString public override.
procedure division returning ret as string.
set ret to String::Format("Name:{0}, Address:{1}, Telephone:{2}, Email:{3}, Country:{4}",
     self::Name, self::Address, self::Telephone,
     self::Email, self::Country)
end method.
end class.

Which when run with Visual COBOL gives:

Name:Mr Johnson, Address:+44 1234 4321, Telephone:, Email:, Country:United Kingdom
Name:Mr Smith, Address:+44 1234 4321, Telephone:, Email:, Country:Wales

Visual COBOL @ Microsoft Teched

The last couple of weeks have been very busy and it has unfortunately affected the amount of blog entries I have done but the good news I have plenty of new material..

So, what’s my excuse.. Well we have been counting down the internal builds of Visual COBOL and we are pretty much ready to ship which is a relief since we are going to launching it at the Microsoft’s Visual Studio launch parties in Las Vegas and TechEd in Bangalore April 12-14.

Personally for me I am quite excited because I will be going to Teched in Bangalore to help show off Visual COBOL.

If would like a look at some of the Visual Studio 2010 integration Micro Focus has is about to release, pop along to http://vs2010.microfocus.com/ and see for yourself. Better still, have a visit to teched :-)

I will finish the silly little blog with a link to a demo of Visual COBOL. Just because I’m proud of it…

ref: http://www.microsoftteched.in/
and http://www.microsoft.com/visualstudio/en-gb/products/2010/default.mspx