Tag Archives: CLR

Getting started with Windows Azure

As someone has been working with Windows Azure since its early days I understand how much effort it takes to get started with the platform.

Luckily over the last couple of Windows Azure SDK releases the documentation is started to do justice to the platform itself.

Any here are some nice links to get you started:

General Overview Whitepaper http://go.microsoft.com/?linkid=9682907
Official Microsoft Site http://www.microsoft.com/windowsazure/
Updated List of all “How To” Documents for Azure http://msdn.microsoft.com/en-us/library/gg432998.aspx
Video: Lap Around the Windows Azure Platform http://www.msteched.com/2010/Europe/COS322

A good new years gift…

My day job keeps me housed in Visual Studio 2010/Windows, so for my out of hours development I feel I need a change, so for many years I have been a home mac and/or linux user; anyway I have used many languages over the years but I feel more comfortable using C#, so my development environment of choice is monodevelop.

Last week I was lucky to get onto the MonoDroid beta, I thought of it is New Year gift from Mono/Novell guys…

So last week I started to playing with it…. its fun… and I think its going to be fun ride using Mono, C# and Android. With phone such as my nexus one and the recently announced Droid Bionic (2GHz dual-core processor, 512mb) it really means that we can start to put some quite computational intensive applications on these devices, plus having access to a quick database such as sqlite makes life easier… now what should I do with it?

SQLCLR – Drop Assembly + Dependancies

Recently I have had the pleasure of working on SQL Server 2008 R2 using the SQLCLR support.

Which I must admit it is quite nice to use, though a couple of things have anoyed me while using… one of which is the user of “DROP ASSEMBLY” and its dependancies…. normally this is not a huge problem but when you are write tests, it is good practice to remove/drop everything before deploying.

Being the sort of person that hates doing repetitively, I thought I would do a quick proc…. so let me introduce you to ‘MegaDrop’… yeah… its a rubbish name and the code is not elegant but it gets the job done..

Enjoy…

CREATE PROCEDURE MegaDrop @assemblyName varchar(max)
AS
    declare @name varchar(max)
    declare @type_desc varchar(max)

    declare mf_getprocs_cursor cursor local for
     SELECT so.name, so.type_desc from sys.objects  so with (nolock)
        JOIN sys.assembly_modules sam with (nolock) on so.object_id = sam.object_id
        JOIN sys.assemblies sa with (nolock) on sam.assembly_id = sa.assembly_id
        WHERE ( sa.name = @assemblyName)

    open mf_getprocs_cursor
    fetch next from mf_getprocs_cursor into @name, @type_desc
    while (@@fetch_status <> -1)
    begin
        if @type_desc = 'CLR_STORED_PROCEDURE'
        begin
            print  'Executing DROP PROC '+@name + ' ('+@assemblyName+')'
            execute ('drop proc '+@name)
        end
   
        if @type_desc = 'AGGREGATE_FUNCTION'
        begin
            print  'Executing DROP AGGREGATE '+@name + ' ('+@assemblyName+')'
            execute ('drop Aggregate '+@name)
        end
   
        if @type_desc = 'CLR_TRIGGER'
        begin
            print  'Executing DROP TRIGGER '+@name + ' ('+@assemblyName+')'
            execute ('drop trigger '+@name)
        end
   
        if @type_desc = 'CLR_SCALAR_FUNCTION'
        begin
            print  'Executing DROP FUNCTION '+@name + ' ('+@assemblyName+')'
            execute ('drop function '+@name)
        end
     fetch next from mf_getprocs_cursor into @name, @type_desc
    end
    close mf_getprocs_cursor
    DEALLOCATE mf_getprocs_cursor    

    declare mf_gettypes_cursor cursor local for
     SELECT so.name from sys.assembly_types  so with (nolock)
        JOIN sys.assemblies sa with (nolock) on so.assembly_id = sa.assembly_id
        WHERE ( sa.name = @assemblyName)

    open mf_gettypes_cursor
    fetch next from mf_gettypes_cursor into @name
    while (@@fetch_status <> -1)
    begin
        print 'Executing DROP TYPE '+@name+' ('+@assemblyName+')'
        execute ('drop type '+@name)
     fetch next from mf_gettypes_cursor into @name
    end
    close mf_gettypes_cursor
    DEALLOCATE mf_gettypes_cursor  
   
    if exists (select name from sys.assemblies where name=@assemblyName)
    begin
        print 'Executing DROP ASSEMBLY ('+@assemblyName+')'
        exec ('drop assembly '+@assemblyName)
    end

Last week I replied to a post about exceptions, it made me think those programming .Net daily take for granted the etiquette of using Exceptions. So I thought I would share some of my thoughts… well it is a sort of a rules’ish list.

  • Exceptions can be expensive, so avoid using them for normal conditions
  • Only catch the exceptions you can handle
  • Don’t hide/swallow exceptions
  • Don’t catch System.Exception as will also catch unmanaged exceptions such as System.Runtime.InteropServices.SEHException
  • Consider using your own custom exceptions or derive them from similar ones
  • Remember inner exceptions when processing an exception
  • Use the Exception suffix on your custom exception
  • Consider using Microsoft’s StyleCop to point out common issues
  • Avoid using System.ApplicationException if you want to use the code in the Silverlight CLR
  • Remember to serialize your own exception types
  • Use xml comment docs for the exceptions a method raises… it helps intellisense..

I suspect I might have missed something.. so feel free to comment..

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