Development General

Data Access Layers

At my previous company we created a “data access layer” above all data access layers.  One guy mainly built and maintained our templates for generating code.  Another guy build complicated views for retrieving and updating data which applied business logic, etc.  The database became a repository for metadata about the system.  From then on, the data access layer took over and produced marvels. 

It was quite an amazing system, but like any amazing system it had its down-sides.  You really needed to be an expert to work with it, logic was many layers deep and it needed to load a lot of metadata before the system would start to work.  Basically there was a lot of overhead to get stuff done.

When I started at my current job I’d realised the benefits of automating data access, but foolishly tried to build my own data access layer.  For code to stand between the developer and a database, there’s a lot it must do before it starts to become an integral part of the system.  Unfortunately writing one from scratch, and writing it well is just too much trouble for most people (myself included).  The requirements are many, but most notably it needs to do two things:

  1. It needs to make the developers life easier
  2. It needs to impose standards

Unfortunately those often end up at odds, and even worse often rule one is lost completely.  Slightly more forgivable, rule two often is just really hard to maintain because each organisation has different standards so having one system that works everywhere is most likely unachievable.

So what’s a developer to do? Recently we decided to revisit our chosen tool for generating our data access layer.  We did some research, gathered the names of the most “popular” ORM tools and set about testing each one.  We have a fairly large legacy of code that we need to work with, so there’s a lot of customisation we need to make it work well and have the code look reasonably neat/legible.

We looked at Subsonic, NHibernate, Entity Framework, Linq to SQL, LLBLGen Pro, NetTiers, PLinqo and a few others.  It boiled down to cost vs customisability vs time and effort to setup.  Cost wise we were down to Subsonic, NHibernate, Entity Framework, Linq to SQL, Plinqo and NetTiers.  We dropped NetTiers because it hasn’t been updated in about a year, and dropped Plinqo because it was basically just a wrapper around Linq to SQL (plus we’d need to buy more licenses of CodeSmith).  So we were down to Subsonic, NHibernate, EF, and Linq to SQL. 

The first framework we tried was Subsonic, took 5 minutes to setup, a quick tutorial video and we were off (I read some documentation too).  Within 30 minutes I’d generated a data access layer, and within an hour I was customising the templates to do little things I wanted them to do.  It was simple, quick and efficient.

NHibernate was an eye opener.  Many config files, odd documentation, no easy “do x, y and z and you’ll have a DAL”.  I resorted to using a copy of CodeSmith with some NHibernate templates to generate the classes and config files for me and even then it was just too complicated.  I know it can do miracles, but we just need something simple and this much config is just too much trouble.

EF was easier than NHibernate, but still had its downfalls.  It offers the world, which means it has extra complications, models, etc.  There wasn’t an easy way to simply add a table to the database and have it generate the code for us in the custom ways we want it to.  The only real benefit it gave us was complicated ORM functionality that we didn’t need.

Linq to SQL is great, but again not much customisation, and really Subsonic and other frameworks provide better features.

So in the end it our decision was clear … Subsonic was the easiest.  Even before we’d made the final decision we had some of our developers asking “can we use Subsonic in project X?” 

The only down side so far to Subsonic is that submitting changes to their code/templates requires you to use Git, which so far is proving to be a real pain in the ass. I guess you can’t have everything you want in life, but at least I have a nice ORM now.

Development General

Java Dates from a n00b point of view

After spotting that seemingly 90% of the methods on the Date class were deprecated, I started getting worried.  Then when new Date().getYear() started giving me odd numbers like 109 instead of 2009.  I started thinking “Wow! Those Sun developers must have been on some strong drugs when they designed this language!”

So a bit more googling and some “WTF?!?!!?”’s later I found out that the Date class returns the time since January 1, 1970.  So of course .getYear() returns the number of years since then.  Well, not exactly.  Because there are only 39 years since 1 Jan 1970 and today.  So how the heck did getYear() return 109?  I didn’t bother to go any further than that – clearly the Sun developers were on drugs that should never have been invented, and I didn’t want to even think about starting to try and understand what was going on in their twisted little minds at that point in time.

BUT, I found out that they have a GregorianCalendar class which can help you out in most situations, even if it’s a little obtuse in how you use it.

The following code:

Calendar cal = new GregorianCalendar(mYear, mMonth, mDay, mHour, mMinute, mSecond);

Gives you a calendar object based on the input you gave it.  Now of course I get a Date object from a database so I want to do new GregorianCalendar(mDate).  But again, that would be asking too much, so instead of that simple instantiation I have to do:

Calendar cal = GregorianCalendar.getInstance();

Why, on earth did they call it setTime?  I mean, I’m setting a date and time.  I’m not just working with Time here folks! Surely the method should be named something a little better like .setDateTime() or .setDate()?

But wait… it gets better.  You know the Date class had a getYear() method, right? Remember? It’s the one that requires large amounts of hallucinogens to understand or use!  Well, naturally you’d expect that the Calendar class would have a getYear() that makes sense.

That’s where you’d be wrong!  Since they started with the obscure requirement of doing .GetInstance() followed by .setTime(mDate), they clearly hadn’t quite yet gotten off their high when they made the get() method.  Here’s how you use it:

Calendar cal = GregorianCalendar.getInstance();
mYear = cal.get(GregorianCalendar.YEAR);
mMonth = cal.get(GregorianCalendar.MONTH);
mDay = cal.get(GregorianCalendar.DAY_OF_MONTH);

Yup, that’s right… why make it as simple as cal.getYear() when you can make it cal.get(GregorianCalendar.YEAR)?  Perhaps someone over at Sun had some stock in a company that provides treatment for RSI’s, so that by making us type out loads more letters they hoped to make more of us suffer not only the pain of using their language, but also of repetitive stress injuries.


From my brief work with Java previously, I know that the designers were NOT on any drugs and in fact they probably had very good reason for designing the classes the way that they did. It’s just not immediately obvious to the new user, and that’s mainly what I’m ranting about here.  So, to all the Java fanboys out there who are preparing to unleash their full fury on me for my sacrilegious comments about their language – please take a moment and laugh before you post. 🙂

Development General

Android, Eclipse and Java – First Experiences

After bitching about the lack of good tutorials last time, I found that I’d actually been looking at Android 1.1 tutorials and trying to run them on Android 1.5.

Now that 1.5 is out (well, it was released quite a while ago, but I’ve only recently started using it in earnest) I’ve re-read the tutorials and some of the introductory info on the Android Developer’s Guide. In my exploring I’ve hit MANY “Force Close” dialogs.  Unfortunately they never seem to say anything about WHY the app is being forced to close.ForceClose

The first few times I’ve just popped Eclipse into debug mode in an attempt to figure out what’s wrong.  Unfortunately it doesn’t seem to help much.The exceptions that end up being thrown seem really vague and not entirely related to the issue at hand.  So without any further blabbing, here are my top three reasons for getting a “Force Close” in my Android development:

1. Putting the call to setContentView at the end of the onCreate method instead of the start.

Initially I thought I’d be clever and first setup all my event handlers, and data binding and stuff.  But after some really confusing exceptions I realised that unless I call setContentView at the top of my onCreate, I won’t have access to any of the “objects” on my form.

2. Having an abstact class instead of a concrete class for my Activity

I’m not entirely sure how Eclipse and I came to this point, but somehow when adding this class I must have ticked a box that I hadn’t ticked before and suddenly the class was abstract.  No idea how or why this happened, and I certainly didn’t notice it at the time.

Of course I’d expect the exception to say something about “You cannot create an instance of an abstract class.” or something  meaningful like that.  But no, it seemed that this was not to be.

3. Not adding the Activity to the AndroidManifest.xml

This happened at the same time as the issue in point two so I’m not too sure if the two issues together just made the cause of the problem really hard to find.   Maybe I’m just too used to Visual Studio protecting me from myself, but this seemed a little silly.  I created a class that extends Activity.  Now I still need to go and add that info to the AndroidManifest.xml file?  I

People might think that I’ve been overly coddled by VS.Net, and that this is simply how normal development works.  If I used a Wizard to credit an Invoice, I’d expect to have a credit note created at the end, ready to send on to the client.  I wouldn’t expect to have to then go and specify the Client, and add the information for the line items, and then add up the totals manually and then finally to add on the VAT before I could send it.

There are a few nice wizards to get things started, but they often seem to only go half way. If I use a wizard, I expect the wizard to do the whole job.  So when I add an Activity to an Android app, I’d expect it to create the class AND enter the information into the AndroidManifest.xml file for me. Actually, in this case there is really no Wizard in the SDK toolkit to add the Activity – and that’s another gripe – there are wizards to create an Android XML file which is relatively easy to do.  But something like creating a new Activity requires a bunch of manual steps.  So where’s the consistency?

Anyway – enough whining.  Soon I’ll face the anger of a mob of Java developers. 😉

Development General

Exporting BlogML from Subtext 2.1 and importing BlogML into WordPress 2.7

I currently run by blog using Subtext and I’ve been wanting to test out some features in WordPress for a while.  So naturally I don’t want to lose all of my blog posts and I’m not keen to write a whole bunch of code to read from the one database and write to the other.

Fortunately Subtext has the ability to export to BlogML and WordPress apparently has the ability to import from anything (if you believe the rumors)  Unfortunately both the export and the import don’t really work.

Firstly, Subtext’s export doesn’t work. BUT since I had started my day thinking about moving from Subtext to BlogEngine.Net, I’d found a forum post where a friendly person showed how to make SubText export to BlogML successfully (Its the post by Harold on 2 Dec 2008).  He links to the binary of SubText.BlogML.dll – you just replace the one on your site (make sure you have a backup of your version, just in case there’s some issues with newer versions of Subtext)

Now for the import part – Thankfully Aaron Lerch had handled this back in WordPress 2.3 in his post about moving to WordPress from Blogger.   The important thing for me was his BlogML importer.

There are a few things that seem to have changed.  Aaron’s post uses “XPath.class.php” which you can get from Sourceforge.  It suggests that you put the file in the /wp-admin/import folder, but that just seems to cause issues.  So I’m going to lay out the steps that I took over here:

  1. Download “XPath.class.php
  2. Put the file in your /wp-admin/ folder
  3. Edit /wp-admin/admin.php and add a line at line 27 so that the file looks like:
    require_once(ABSPATH . ‘wp-admin/includes/admin.php’);
  4. I’ve modified Aaron’s blogml importer and you can get blogml.php.
  5. Upload it to your /wp-admin/import folder

The original blogml had a few issues:

  1. It couldn’t handle   or > or even < in any of the blog posts
  2. Categories were not imported correctly at all
  3. It didn’t, and still doesn’t, handle draft posts at all.
  4. It didn’t work with WordPress 2.7

I managed to fix the problems with the escaped html, the categories and the WordPress 2.7 issues, but didn’t quite get to fixing the issue with the draft posts.  Let me know if you manage to fix that part – I only had one draft post so it didn’t really matter.  Hopefully this will help someone else though.

Development General

Code Reviews

One of my month-end deliverables this month was to have finished reviewing certain portions of code that my team has written.  I have spent the last two hours going throug 20 printed pages of code. (I print the code out because I find it a lot easier to reference multiple sections of code at once using paper than I do using a monitor)

I feel like I’m back at varsity marking pracs and tuts (which I did for 3 years) or like I’m back doing my 1 year of part time Computer Studies teaching and I’m marking the Grade 12 tests.  The 20 pages I have looked at are almost more covered in ink from my pen than in the pigments from the laser printer.  (ok, so that’s a slight exaggeration)

The code I have to review is probably 30% mine and 65% developer A, and 5% developer B.  All of us are pretty pedantic, but I’m quickly seeing trends.  I think I can now make out Developer A’s code at a distance, wearing a blindfold, with both my hands tied behind my back.  I don’t think I’ve seen any of Developer B’s code yet, but we have recently been through a rather large “refactoring” of our OO heirarchy so I think there are bits of his code which I’ve looked over tonight. And I’m still amazed at how my own code sucks.

A lot of our development has been evolving as we’ve gone along so there are some cases of really old and BAD code, right along side really new and slick code.  Then there are cases of really old and BAD code being copied and pasted into really slick and new code without even changing the comments to match their new location!

There are cases where one developer built one way of getting access to a list, and then a few months later built another way of getting access to the same list and put it right next to the first way in the code and didn’t pick up on it.  I’m not sure if I am that aware of the code that surrounds my current coding task, but I’m sure as heck going to start trying to be in future.

One of my pet peeves has been comments in the “///” comment portions ahead of method/field/property/class/etc declarations which are written assuming a fair amount of knowledge of how our system works, instead of written to explain to a new developer how the system works (I’m guilty of this too).  One of the things we’ve discussed many times is the fact that eventually we want to produce NDoc/DoxyGen documentation from our code so having comments that are not helpful is really a waste of time in the long run.  (Worse, in my mind is repeatedly starting sentences with lower case letters – something that is uber evil when you consider how many members of the the grammar police are in our team)

Fortunately, after our recent OO Architecture “Refactoring” Marathon, most of the code I am reviewing is consolidated into three main base classes, and I’ve completed the review of two of the three.  So after the next class I should have very little code left to review.

I’m still rather suprised about the amount of silly bugs we have in our code… I’ve been watching Scrub’s recently and I keep saying to myself (in the voice of Dr Cox) “Oh, my goodness barbie!  How do you get your bra and panties on in the morning all by yourself!”

I guess that’s why we have code reviews.  We try to have “debriefing” (after my “bra and panties” comment, I must say – “No, not that kind of de-briefing!”) sessions where we all try and learn from the mistakes made in code that is reviewed.  So I have LOTS of content for our next session, and hopefully I won’t see these mistakes ever again.  (I can dream, can’t I?)


So after that general badmouthing about the quality of our code, I must just say that we have a great team.  If(when) we make mistakes we learn, and code better.  The other guys are super bright and really capable (which is probably why some of th bugs shock me so much) and I’m really happy to be working with them.

Development General

Entering the new world

Yesterday I started working on moving our development to .Net 2.0.  We spent a year doing .Net 1.1 only development specifically to keep us open to the option of using Mono to create a frontend for our Mac users.  Now we have decided (and convinced everyone) that our web solution will cover the needs of the Mac users so we can move to .Net 2.0, VS.Net 2005, and even a little bit of WPF/E research.

We’re planning on getting our full product done by around June this year, when Mono is planning on having a .Net 2.0 version of their framework out, so even if we do have to build a Mac application we could use Mono 2.0 (assuming its stable).

Anyway, I’m happy. Because after a year of watching other people play with the cool toys while I’m stuck with the 3 year old model I finally get to join the ranks of the cool-toy-people.  😀 (Even more so since I got my HTC TyTN in December)

.Net Stuff Development General

I finally did it… :(

Today I finally un-installed VS.Net 2005 from my PC.  I keep trying to do things in VS.Net
2005 that just won’t work in Mono, and more recently I had issues with various
application blocks that just didn’t work in .Net 2.0. Apparently there are
work-arounds, but I’m not interested in any of that. I want something that just
works and, despite its many issues, VS.Net 2003 has a much bigger base of tools
that will “just work” for me.

So as I type VS.Net 2003 is installing on my PC and I’m going to have to say
good bye to “built in” support for Master Pages, Refactoring, and a many other
cool features so that I can have “stuff that works”. (Unfortunately for me
“stuff that works” roughly translates to “stuff that works on
.Net and on Mono”) 

Another good reason for the swap is that I need to build a web app at the
moment (needs to be working by January)and, unless they’ve seriously jacked up
their capabilities since .Net 1.0 was released, the likelyhood of any South
African ISP’s supporting .Net 2.0 in the next 6 months is probably not very
high.  So we’ll be far more likely to find a place to host the site if we
use v1.1.

.Net Stuff Development General


Last night’s SA Developer meeting in JHB was on
LINQ.  I unfortunately was about 25 minutes late so I missed some of the
stuff but what I did see was both pretty cool and frustrating.  The new C#
3.0 stuff looks really awesome but the LINQ stuff is “just another teaser that I
won’t be able to use until its released in 2 years time”.  Oddly enough my
previous company had built something similar to the Linq expression trees to
enable you to query our database and return objects.  We had been building
the system for about 3 years (it does a lot of other things besides just
querying a database), so I’m sure they’ll all be kicking themselves about the
fact that in 2 years their cool technology is going to be “replaced” by
microsoft’s LINQ and they’ll be working on “outdated” or “non-standard”

I guess that’s the way things go… I certainly wish I could use LINQ now, it
would definately make my life a lot easier, and would probably make my boss far
happier with using .Net.  I guess that’s the story of MS products, version
3 is the one that finally gets it right and kicks ass…

Not that version 1, 1.1, or 2.0 don’t kick ass… its just that they just
don’t have all the coolness of 3.0. 

Added to that – I’m having to build a new project in v1.1 instead of 2.0
because we have to use Mono. So I’m a bit frustrated with seeing new
coolness that I can’t use  even though part of the geek in me is
super excited at where things are going and how cool the new coolness of 3.0
will be when we finally get it.

Development General

Excursions in Mono and Generics…

Shortly after starting at the new company I found out
that a lot of our clients like Mac‘s so we need to find some way to make the new version of our
product work on Mac‘s.  Of course the initial solution was to make it web
based, but that causes some hassles with interactivity… I know Ajax/atlas is cool but for lots of
data entry the web just didn’t seem to quite do it quite as nicely as was
required… so our next option started becoming either Java or Mono.   And since we
don’t have any real java experience here we decided to take a look at Mono.

I was pretty excited when I found out that it does Generics and Nullable
types, and some tests showed that assemblies built in VS.Net 2005 that used
Generics and Nullable types worked quite nicely in Mono.

Unforutnately I’ve had some issues with the UI side of things.  Mono
kinda (98%) supports System.Windows.Forms v1.1 and if I design a UI in
VS.Net 2005 there seems to be no way to make it use .Net v1.1 components. 
So I get a bunch of designer code that isn’t 1.1 compatable, as well as a whole
bunch of controls that aren’t in v1.1.  Apparently Mono has a few of these
controls already in testing, but they’re not planning on supporting System
Windows Forms (SWF) v2.0 until the end of 2006, so its not a good idea for an
application that I need to build now.

So I’ve tried to use VS.Net 2003 and while that builds the UI’s nicely, it
doesn’t recognise the Generics and Nullable types in my v2.0 assemblies.  I
thought I’d try out SharpDevelop v1 and v2 (which isn’t yet released). 
SharpDevelop v1 bombs out when I try to reference a .Net v2.0 based assembly,
and SharpDevelop v2 has two problems:
1. Its not very stable (which is to be
expected from a pre-alpha product)
2. It seems to suffer from the same
problem as VS.Net 2005.

Next I tried out MonoDevelop. I could only get that working on Linux,
which I managed thanks to Ubuntu and Virtual PC, and it doesn’t seem to like SWF
at all – it only seems to build GTK# based interfaces. 

I even tried X-Develop, and didn’t have much joy.

From what I’ve seen there seems to be no IDE that will build a Mono SWF (aka
MWF) UI while using some of the .Net v2.0 features that mono
supports.  So my next plan of action is to dumb down my business logic
so that it only uses .Net v1.1 features so that I can build a UI using either
VS.Net 2003 or SharpDevelop v1.

At this moment I’m super impressed with how Mono performs, I’m just
struggling to find tools and controls (that’s another story/blog post – coming
soon) that will work easily.

.Net Stuff Development Geek food General

New toolkit released

[IMHO just lost my blog entry – it posted it, but the contents were blank, so here’s a 2nd attempt]
I just read via the Daily Grind that Component Factory have just released the Krypton Toolkit.  Its basically a set of windows forms controls that are written for .Net 2.0 and VS.Net 2005, and best of all they’re free for commercial use.

I’m going to go and download it now and check it out.

[And for the .Net bloggers out there, if you blog about their release you can get a free copy of DotNetMagic – check out the offer here.]