Jan Limpens

01. 09. 2009

Refactoring a web forms application to modern paradigms

Filed under: development — admin @ 10:42

For the last two years or so, I was working on a partial rewrite of the web shop I am developing.

The original web site was done in quite a hurry using whatever tool would fit the current situation. So I ended up with a behemoth consisting of asp.net webforms, 4 different data layers (the now defunct gentle.net, subsonic, nhibernate and plain dataviews/adapters/boilerplate code), no real business logic (there were these classes I used to store my data in, but there was no actual logic embedded there. I guess these objects usually are referred to as DTOs (Data Transfer Objects)).

So whatever happened to these DTOs, happened due to logic embedded in thousands of clicked, loaded, databound, changed, etc… events in web forms. It did not take a lot of time, maybe a year or so, and this entangled spaghetti ball became impossible to further develop. Everything had so many side effects, even worse relied on so many side effects, that touching any part of the code would lead to endless debugging sessions.

Well, I was aware of that danger and also, that the initial lack of design eventually would need to such a situation. This situation was caused both by the hurry in which the site had to be up and running and by the lack of options when I started – either they did not exist yet or very very early in their conception. In parts of the application I gradually introduced different open source solutions, to see, what the benefit would be. In parts the original application consisted of quite a few spikes in the most different directions.a

I generated mails, sent to customers with nvelocity. NVelocity s a side project of the great castle stack, so it was only a matter of time until I started to discover Castle Monorails, which I used for some functionalities in the administrative interface. When Gentle.net was officially lied to rest, I had to look for an active supplement and everything pointed into the nhibernate direction, so I went with that.

Soon enough it was clear in which direction I needed to head. I based the upcoming architecture on:

More on each of those in future posts.

02. 09. 2008

Google’s Software Developer Comic

Filed under: comic, development, illustration — Tags: , — admin @ 23:30

Google announced it’s own browser project (Google Chrome) with a comic by Scott McCloud.

It happens often, that in a conversation I try to evangelize people about the comic medium`s vast possibilities. Superheroes – bah! There is much more to it. Also, I was often thinking, how a comic about software development could look like (without mentioning Battlestar Galactica and/or Guitar Hero Darth Vader Edition).

Well, it seems the good folks over at google, had a similar idea and the result was this.

Very cool. I like Scott’s work, in this case it almost reminds me at Daniel Clowes, probably due to the reduced coloring. Scott seems to work almost entirely in illustrator (so I read), his last episode of understanding comics looked a bit dead because of this. This comic here looks much more vivid again. Kudos.

31. 08. 2008

Handling Collections with NHibernate

Filed under: development — Tags: , , — admin @ 11:17

When business entities hold some collection and you read the NHibernate manual on how to handle this, most parts of the book (not all) will either tell you to expose this either as an IList (more pragmatic) or as an ISet (more beautiful, performant, failsafe, etc.).

I am under the impression that it generally is a bad idea to expose any type of collection that way. Why?

(I go with IList here, but same applies to any other collection)

case 1)

class Bar {
    public Foo Parent {get;set;}
}

class Foo {
    public IList<Bar> Bars {get; set;}
}

Now you are completely exposed. Anyone who has to handle this code, must be conscious of any side effects this may have. For instance, if you have a bidirectional relation to handle you must code like that, or you’ll get an error:

var bar = new Bar();
var foo = new Foo();
bar.Foo = foo;
foo.Bars = new List<Bar>();
foo.Bars.Add(bar);

Quite a bit of code that will repeat over and over in your app. My verdict: avoid this! Many people go with something slightly better:

2) Shielded and somewhat correct

class Foo {
    IList bars<Bar> = new List<Bar>();

    public IList<Bar> Bars {
        get { return new List<Bar>(bars).AsReadonly; }
        private set { bars = value; }
    }

    public void AddBar(Bar bar){
        // set whatever is necessary to keep consistence and let the user forget
        // about persistence details like bidirectionality
    }
}

If you unit test this, your tests will be green and you will be shielded against mistakes. You can forget the inner workings and everything will be fine. That is, unless you want to load this collection lazily (and receive lots of performance benefits). I actually already forgot what the concrete problem was, but it seems, that NHibernate inspects, what Bars delivered and tries to give the same back using the private setter. It got a ReadOnlyCollection, so it tries to set one. And this fails somehow. (I’ll try to come up with the original error). So you either return bars directly and leave two possible ways to access it, one that works, and one that sometimes works. Horror. Or. you go, by what I do right now.

3) Shielded

class Foo {
    private ISet<Bar> barSet;

    public ReadOnlyCollection<Bar> Bars {
        get {return new List<Bar>(barSet); }
    }

    public void AddBar()Bar bar {
        // add a bar
    }
}

This

  1. uses the NHibernatewise better performing Set, that most possibly also represents much better your idea of what should be returned.
  2. returns a shielded IList, that has the foreach semantics you were looking for (ISet has not)
  3. Shields you from future mistakes

Mind: You map against bars using a <set/> tag. This is a very basic pattern, but I thought, if you are new to it, it might give you a better start. I did not find this documented anywhere.

Getting GCompris to run in Mac Os X

Filed under: development, misc — Tags: , , , — admin @ 8:56

My kiddo liked to play with GCompris, a quite nice (although none of them are as nice as I would wish they were) application suite for children. I had it running on my windows pc, but when I switched it to a mac mini (I just loved the silence), this was gone.

After a quick google for “GCompris Mac” quickly revealed a fink info page, I knew I had to go the fink route. I nstalled, find and fink commander and… nothing, no gcompris. I rechecked the page –> unstable. So I reconfigured fink to accept unstable branches, but nothing. These unstable branches never seemed to arrive.

So I left it at there. Yesterday I gave it a new try. On the fink faq I found an entry explaining how to install something on an unstable branch, without actually configuring fink to go completely unstable. Sounded quite like the thing I needed.

So I ran fink selfupdate-cvs and it seemed to pull down halve of the world (via cvs). After a while unstable brances seemed to appear and even the looked for GCompris entry. Victory!

So after I did fink selfupdate; fink index; fink scanpackages. I ran sudo fink install gcompris, it again started to download half of sourceforge to fulfill it’s dependencies. First build failed, I had to pull and build a library (don’t remember which) because fink “got confused”. Now I am during the second try and it seems to build even kde and enlightenment and whatever. Seems the dependencies are huge.

Other kids software I think is nice:

12. 05. 2008

About switching and running and being free @ limpens.com

Filed under: development — Tags: , , , , , — admin @ 11:44

So over the past weekend, I changed the dns and limpens.com is running at the new host, webfaction.com, which, so far I can recommend. I’ll talk about them another time, maybe. I switched webhost, because I was unhappy with webhost4liefe’s connection’s speed. I live in Brazil and waiting for a page to appear seemed like I had a 14.400 baud modem, and not a 4mbit connection.

So, just for you to know I am a .NET developer and apart from having seen some rails screen casts in the past I am completely new to this area. At first I thought, rails would be the way to go for me, but coming from a dissident background, I soon figured, if everybody is using it, there must be something wrong with it – and in fact I sincerely dislike the active record pattern that rails kind of forces on to you spamming all your generated (and I am a newbie so I use generate) with base classes. I disliked all the ‘magic’ going on, where your classes seem to have invisible attributes and scaffolding is filling all the gaps. It may be only me and I might be completely wrong in my view, but I like it if there is something missing and I have to put it there. I also think this kind of approach does not help TDD.

In the end I build the site in rails and did not like it because I did not understand it’s inner workings.

So I looked into another direction and with webfaction being a python specialized host, I checked out what python could do for me. And with cherrypy, another mvc like framework, I found something quite beautiful – it’s lean and fast and what’s quite interesting is, that you actually build a object graph of your site`s controllers (they are not called controllers, but the behave in the same way)

so you have

root = Root()
root.about = About()

and so on, this also takes care of controller parameters and routing quite beautifully:

the method

root.somecontroller.action(param1, param2, param3) is mapped to

http://site.com/some/action/param1/param2/param3

Now _that_ is easy. And I had the site running (ruby and python are quite similar, so once you got your architecture in your mind, translation is not an issue).

But I did not like python too much. I disliked the lack of multiline lambda scopes out of language restrictions (no {}), and being the old OO guy, I did not dig all those global methods, like len(string) instead of string.len. I found it awkward why you have to order your methods within the class, so that the called method comes before the caller. I did not like the fact that I was writing methods outside of classes. For me this was a step backward (and I am heading in the lisp direction and am not saying that this is not possible, there), so I ditched it. Most probably a bad decision, many pythonistas might argue, and as they are far more experienced than me, they probably are right, but one has to make decisions, and after reading this article, I found that probably I’ll go for ruby. (And am already missing MI, for that matter)

So I looked around what other frameworks are lingering around in ruby space and encountered Merb. Merb seems to be a framework, that lets you plug in your choice of technologies, which I liked. It offers different physical structure options and still some good defaults.

It took me a part of the weekend to slap together limpens.com and I am quite happy with the results. Merb seems to be quite configureable, I found no magic going on (apart from than that my model was accessible to my controllers without ‘require’- and me adding these requires made the app fail and I had no clue why). So far I am not using any database and merb is 100% ok with it, and once I want to I have a gamut of options. I use haml as a view language (I wish there was something like pythons genshi for ruby, which is a template language which I liked a _lot_) and I’m free
To do what I want
Any old time
I said I’m free
To do what I want
Any old time

06. 05. 2008

Hello world!

Filed under: development — admin @ 15:04

Just stared the account. Let`s see what the lisp/python/ruby world is going to be like.

Just tried to install plone, but failed as it seems to consume more memory than my current profile (80mb) offers.

Powered by WordPress