Saturday, May 20, 2006

Minor change to Blog layout

I've finally managed to get hold of some code which allows the new abbreviated front page of my Blog. This new format sees each 'long' post have a brief summary on the front page with a link to the full article.

The code I've used, is a slightly modified version of some I found I found at Anything Else which is very clever and also allows for shorter posts which do not link to a larger page. (This current post is such an example.)

I have adjusted it slightly from the version indicated to allow for a summary which is not a part of the actual post.

Wednesday, May 17, 2006

Code Generation

A post on code generation in this case via MyGeneration and some of the clever things can be done with it :)
HanselMinutes
I listen to several podcasts. For a complete list see my sidebar. One of these is HanselMinutes
A weekly audio talk show with noted web developer and technologist Scott Hanselman and hosted by Carl Franklin

In reality what this means is that Scott has spend a great deal of time dealing with technology of varying kinds and brings us this weekly show with Carl Franklin (of DotNetRocks fame.) and shares with us his knowledge regarding tools utilities tips and tricks relating to .. well .. almost anything.

Scott recently did a podcast (ok so it was a month or more ago) on Code Generation in which he mentioned several code generation options and rated their capabilities and effectiveness.

My Generation
As Scott himself points out during the course of his show, there are far more Code-Generation products available then he could possibly have covered during his show.

One that was missed by Scott, but found by my good friend Marc Croom, was MyGeneration a freeware application/framework build around the dotnet framework.

MyGeneration claims to be capable of everything that CodeSmith is and has the added bonus that it's free (although not open-source).

Naturally (given a free framework some spare time to play with) we have begun to delve into the realms of Code-Generation and have been trying to decide just how we can take advantage of it.

The idea seems to be that you code up a script/template (in VB.Net/C#/VBA or JavaScript(I think)) and then this script is made to run, generating the code you requested.

MyGeneration comes complete with a framework for reflecting over an existing database structure. So you can create a class per Table/View/StoredProc which can contain constants derived from the Table names, Field names, parameter names and code derived from a combination of these and code pre-build into the template. (Of course these are ideas not limits)

Inside a couple of hours we (Marc and myself) have managed to create a few simple templates which we were then able to use to completely regenerate the existing DAL (Data Access Layer) of one of our company's main applications.

The application in question has, at last count, 122 tables, and 45 views.

Classes for all 167 of these database objects including Select/Insert/Update/Delete methods (obviously where appropriate), relevant fieldname constants and several lazily loaded properties were generated following this in less than 30 seconds

And that was just from the meta data gathered from the database itself.

I can insert this generation of code into my build procedure to ensure that I get compile errors if I change the database to the point where old code cannot work against it.

For example
If I delete a field the code generation will regenerate without that field constant and any code that references it will fail to compile.

In the old days, (lol I'm not that old :)) we would have "hand coded" constants to represent the fields of a database. These constants would have been compiled into the code and would not have manifested problems until runtime.
So here we fail as early as possible moving as many errors as we can from runtime to compile-time.

Hey here's an interesting idea. Hows about we generate assertions for the existence of each and every field and table in our entire data structure which we can run upon application startup in order to determine for certain that we are running against the correct version of the database.

The Next Step
In the current situation, "The Database" is the domain language. In other words the database contains all the information used to perform the generation of code. This is great for simple needs, like mine are currently, but what if later I need to express exceptional circumstances to my templates?

I plan on taking a leaf out of Scott's book and trying the following.

I'm going to try to use MyGeneration to generate, not code, but an XML representation of the schema of my database.

I'm then going to see if I can't knock up an assembly or 2 to help MyGeneration reflect in a strongly typed fashion over the generated XML and see if it's can't be made to generate the SQL Schema from the XML

Initially this might seem like a strange idea, but there is method to my madness.

You see in doing this, I will have shifted my Domain Language from the Database to the XML.

The reason for this, is that XML is capable of having meta-bits added to express things that the database cannot. So I can markup my database XML definition with business information which should enable my code generation to make sensible decisions.

For example
I would like to generate a DAL for my database. But there are several tables for which I would like to generate further logic.

I have a Customer table and a Product table and I would like to indicate to the generation system that it should generate Domain Objects for these tables.

So I would actually like the system to generate Customer and Product objects which would naturally have intellisense access to properties which represent the fields in the table. Further I would like to generate collection classes for these new Domain Objects and finally I would like the DAL for these objects to return collections of these objects rather than Rows, Datasets or Datareaders. So now my results are strongly typed, iterable (not sure that's a word) and I can inherit/amend these to add business-logic.

There is no easy way to markup the database itself so that the templates can understand that these tables are special but I should be able to markup the XML in any way I see fit making this an almost trivial task.

And finally...
There would seem to be many potential uses for Code-Generation. A few more I have either thought of myself or have had suggested by others include

Unit Tests for generated code
XML Documentation
AJAX Style Javascript Functions that map to existing server-side functions

If you can think of any more, then please feel free to let me know in the comments. I'm very interested to know what people are doing in this space.

Tuesday, May 16, 2006

The bane of my life for many weeks (Solved)

A retro post which refers to an incident that was actually solved a couple of weeks back, but I didn't have a Blog then did I? :)

Once upon a time (It could be up to about 6 months back). I had a perfectly working ASP.Net WebApp when suddenly and through no fault of mine..... (never is, is it?)

.... my copy of Visual Studio broke.

Well I say it broke. What I mean is that the QuickWatch broke. But if there was ever a feature I use all the time it's QuickWatch.

What I mean to say is that while simple types (int32, String etc) would work fine, complex classes would fail to display the little plus sign which allows one to expand the item being watched and to then see all of it's properties.

More recently I discovered that this only happens in VB.Net (and before the zealots jump out and kill VB, I must point out that this is not a flaw in the language. It's a flaw in the VB part of the IDE).

Further more I already knew that this is a project based problem. That is, only a select few projects suffer from this.

This plagued me at work for some time.

I later found the answer while programming my 2 latest DXCore offerings (DeclareIt and MoveIt). Both are now available by the way (in a sort of beta testing open source kinda way) at http://www.rorybecker.co.uk

I had been besieged by the same problem. My solution, in this case contained a few different plugins (again written in VB.net) but nothing that special.

I was livid. That B*stard bug had leapt from my work PC to my home PC and now my "fun relaxation" programming was becoming as stressful as my work programming.

That was it.... I had to do something...

I spent all night (on and off) visiting forums and google groups and found lots of people who seemed to be experiencing the same problem. None of whom had any answer..

Finally I found someone who had the answer... the holy grail... the goose that laid the golden egg.

But damn it I can't find it any more. That's right I have no idea who you can actually thank for finding this little nugget of information.


Sorry

..
.
.


Oh yeah sorry that's right I didn't tell you why this happens yet. :) Sorry

Well it turns out that this happens if your solution contains a project with no compilable code in it :)

Yup its that simple

In my case I have a project for creating my plugin repository homepage which , due to it's very simple HTML/CSS approach (due to my being too cheap to plump for real hosting with asp.net facility), had no compilable code in it whatsoever.

So long story short (oops too late) all you have to do is to find that one ( ok I guess there might be more than one) project in your solution which has no code(VB/C#/C++ etc) in it, and then add a single class ( class1.vb/class1.cs/class1.whateverthoseawkwardc++guysareusingthesedays)
Compile once and your done.

Go on try it... Yeah now put a QuickWatch on that class instance over there (yes that one)... See that plus sign (you do remember those don't you?) yeah expand that.


Now doesn't that feel better :)...

Now go get a beer..

You deserve one...

And so do I :)

RefactorPro -> CodeRush -> DXCore

The most useful visual studio toolset that money can buy. And the free bit too.
About 6 months ago now I discovered the most useful Visual Studio tool in existence.

Refactor Pro (by Developer Express) is basically a language-agnostic refactoring tool for visual studio (2002,2003,2005).

This means that it allows you to do lots of very clever things to your code which transform it into a very much more readable state without damaging the code itself. Oh yeah and itworks for both C# and VB.Net

Note: A Refactoring in it's truest sense should not alter the functionality of the code it's modifying. In other words, after the refactoring, the code should manage to pass all unit tests which it passed before the refactoring.

From there is was a very short walk to find CodeRush. CodeRush is a programmer productivity tool like RefactorPro. It's productivity is gained, not from changing old code, but by allowing you to create new code in far less time with far less keystrokes.

Word has it that Mark Miller was giving a talk on the merits of coderush and he challanged one of the audience members to a duel wherein he (Mark) and the audience member would attempt to code the same simple application from scratch. The audience member was given a standard PC with no hidden extras, and Mark was using another machine identical save for the copy of coderush which was installed. Then, to make things a little more challanging, Mark informed the audience that he was so confident in Coderushs ability to aid him in his quest, that he would consent to type the code for this application using, not his hands but instead by using a single pair of chopsticks.

Apparently Mark managed to beat the poor guy with time to spare.
(I think it likely that Mark probably had a litte cafine advantage aswell)

The final thing to be aware of is that both CodeRush and RefactorPro are built on a "Free" framework architected by Developer Express on to which anyone can build additional functionality.

And to prove the point, that's exactly what I've done.

My home page is over at http://www.rorybecker.co.uk/ and exists at the moment as a place to dump some of the DXCore plugins that I work on in my spare time.

So I emplore you if you haven't heard of these great tools, please head on over to www.DevExpress.com and checkout both Coderush and RefactorPro

Please be warned... some of them require Refactor Pro aswell as DXCore.
But at only $99 (last time I checked) It's probably the best money you will ever spend as a VS.Net developer

I will be posting a couple more Plugins (DeclareIt and MoveIt) soon so check back here or in the devexpress DXCore/CodeRush/RefactorPro newsgroups for information as it's available.

Monday, May 15, 2006

Who am I

A little introduction to me
My name (as you really should know already from this blog's title) is Rory.
More precisely it's Rory Becker.

I am a programmer for a relatively small company which I'm not going to name here so that I can say whatever I want without it reflecting badly on them.

I mostly program in VB.Net although I am fairly competent in C# as well. I have been following the Microsoft thread of IDE's since VB 5.0 when the company I was working for dictated that this was the "New Language" that would be used for our next project. I immediately complained (although not very loudly) that Delphi (not sure which version was about at the time but it would have mattered very little) was a far more capable language for almost any possible project.

I was informed that this did not matter as we had a large supply of VB 3.0 code from a previous project which would be the basis of this new project. (Oh dear)

I would like to have quit but that wouldn't have looked good on my report when I'd finished my gap year.

Anyway that's all water under the bridge now and since VB has now caught up and become a real grown up language.

So now I program in VB.Net 2003 but I am, for the moment, very happy doing so.