Friday 18 September 2009

Extensions, extensions

Once again the .Net framework has brought out another fantastic feature which makes development smarter and easier.
 
Extensions are a very simple but very useful feature in .Net. so what are they? They are methods which extend the functionality of an existing object, what this means is that you the developer can add a method to any object (as far as I know!). For example, if we look at the boring String class we see methods such as Contains(), EndsWith(), Join(), LastIndexOf(), ToLower(), Trim() and many more! However what about the ConvertToMackolicious() method!!? It doesn’t exists! So everytime I want to convert a string to “Mackolicious“ I either have to create a private method or create a class.
 
The disadvantages, if I had 50 classes that required this functionality I would need at least 50 private methods! That’s dumb! So naturally I would create a class right, but every time my application gets compiled so would the class thus creating an unnecessary object in memory, boooo.
 
 Welcome extensions! Now I can write a static method within a static class and provide a certain method signature and  can achieve a functionality which is identical to the functionality of a regular instance method, such as those examples above.
 
This is an example of the syntax required for an extension method: public static string ConvertToMackolicious(this String stringObject){}. Using the keyword ‘this’ tells the runtime what object you are extending, here I’m extending a string, hence all strings within this scope (namespace) will be able to benefit from the ConvertToMackolicious() method yippee!
 
Disadvantages of extension methods (from my personal experience). Acting on an object within the .Net framework is easy and produces good results, however acting on a type built by yourself or someone else may not always be a good idea. The reason being that they might one day change the implementation of that type making your extended method produce wrong results or make it stop working altogether! Be careful!

Sunday 30 August 2009

Windows Workflow Foundation

During my research into Windows Workflow Foundation I discovered many things, the most important discoverey of all was the actual reason for using it. This was probably the most difficult aspect of my research, but after hours of heading banging and implementation I found several good reasons for using Windows Workflow Foundation (WF).

Image a scenario where you have been given the developement task of building a specific aspect of an application. Here you are responsible for only one part and other members of the team are responsible for the other aspects, for example, you have been told to build a function that takes text and determines whether or not the word "Microsoft" appeared within the text. So most likely you build your simple application which uses a regular expression (of some kind) and returns some kind of boolean.

Now what happens to that boolean that is return you have no idea, nor do you know where the text comes from. All you know is that your function works! Now the project manager has thanked you for your working function and has reveiled to you how integral your function is to the company's applications. In fact he has told you that several existing applications and several new ones will be using your function in order to determine some kind of process, for example, one application may use your function to send emails to every address where "Microsoft" is in the domain.

Achieving this portability can be done in many ways, however using WF it can be achieved in a strongly-typed diagrammtic way. All this means is that you can view the various execution processes whislt it being tied into the code. Before this had to be done seperately with the business processes being dis-connected from the code, with WF however, this business logic achieved through a UML designer is built into to the .NET framework allowing true business logic to be programmed directly into the application logic.

So to go back to the inital problem, a team-lead or manager can easily integrate other applications or remove exisiting applications by using the WF. He can also ensure that your function is doing what its supposed to be doing by simpliy integratin unit tests and making sure that the application exits correctly!

Perfect, problem solved you work on your existing code whislt it gets integrated seemlessly with everything else!!