April 2007 - Posts

Active Record vs. Data Mapper?
30 April 07 08:40 AM | adrian | with no comments

Suffice to say, Active Record is an approach towards natural grouping of behaviours between classes. Active Record is most commonly used to upgrade Data Trasfer Object towards Domain Model. On the other hand, Data Mapper downgrades Domain Model to DTO by taking away the data persistence responsibility.

When I first learned about layered programming, the most natural way to do separation is to use Data Source - Data Mapper - Business Layer - Presentation Layer path, with DTO all over the place. That is, Presentation uses DTO, Business Layer uses DTO, and so on... This creates a very unintuitive way to access data.

Say you have a Person object (heck, thousand books uses the Person object, I would, too), this Person is a DTO. That means it has private fields accompanied with getter/setter methods for each field. Say the Person has FirstName and LastName (again, just like thousand other books).

How does the Presentation (a.k.a. the End User) sees and uses this object?

Dim pers As Person = PersonBusiness.Get(personIdentity)
Write(pers.FirstName)
Write(pers.LastName)

While inside the PersonBusiness.Get method, the following code happens.

Return PersonMapper.Get(personIdentity)

And inside the PersonMapper.Get method, the following code happens.

Dim queryString As String = "SELECT * FROM Persons WHERE Id = " + personIdentity
Dim reader As IDataReader = db.ExecuteQuery(queryString)
If (reader.Read()) Then
  Dim pers As New Person()
  pers.FirstName = reader.ReadString(0)
  pers.LastName = reader.ReadString(1)
  Return pers
Else Return Nothing

To compare it with Active Record, here's how the Presentation sees and uses Active Record enabled DTO (or maybe, Domain Object).

Person pers = Person.Get(personIdentity);
Write(pers.FirstName);
Write(pers.LastName);

While inside the Person.Get method, the following code happens.

string queryString = "SELECT * FROM Persons WHERE Id = " + personIdentity;
IDataReader reader = db.ExecuteQuery(queryString);
if (reader.Read()) {
  Person pers = new Person();
  pers.FirstName = reader.ReadString(0);
  pers.LastName = reader.ReadString(1);
  return pers;
}
else { return null; }

Yep, Active Record is simpler because you eliminate the mapper layer, and the business layer. Fowler on Patterns of Enterprise Application Architecture said that you will need to use Data Mapper when things got more enterprise-y, or in other words, got more complex. But as you can see, it's completely unintuitive to use an external object to deal with another object's internals (that is, by requiring objects to expose unneeded information by public properties.

To illustrate this, say we take the Person object before. The only use of this object is to print his FullName using his FirstName and LastName. When using Data Mapper you'll need the current structure plus another public method that returns the concatenation of FirstName and LastName. You'll expose three methods for one function. If we used Active Record, you'll only need to expose the FullName method, the other two are not required (because you can address private fields using the internal mapper).

But then, can we combine these two patterns to create a better way of accessing things? Maybe like using Active Record that support independent evolution of Domain Model and data structure? Or maybe using Data Mapper without exposing unneeded properties?

Here comes Active Mapper(tm). :D

Active Mapper combines Active Record and Data Mapper capabilities using Dependency Injection to link between the two. Objects involved in this pattern are as follows:

  • Domain Model. This objects has data and behaviour that the Presentation layer can consume. It does not need to expose unneeded methods just to initialize the object.
  • Data Access Object. This object knows how to connect to the data source, queries it, and returns the resulting values.

So how does Active Mapper look like?

ConcreteDomain will retrieve data from data source using an implementation of IDataOperation<ConcreteDomain>. To achieve this, any implementation has to be "injected" into ConcreteDomain using Dependency Injection (be it at method-level for static methods or constructor-level for instance methods). ConcreteDataAccess implements IDataOperation<T>, returns values from data source. It is then up to the ConcreteDomain on how to map these values into its internal structure.

For example, client code will use ConcreteDomain (in this case the Person object) using the following code.

Person pers = Person.Get(personIdentity);
Write(pers.FullName);

While inside the Person.Get method, the following code happens.

object[] values = dataAccess.Read(personIdentity);
if (values != null) {
  Person pers = new Person();
  pers.firstName = (string)values[0]; // Private access
  pers.lastName = (string)values[1];  // is valid!
  return pers;
}
else { return null; }

As you can see, things evolve these ways:

  • Data source structure evolution happens on ConcreteDataAccess.
  • Domain structure (mapping) evolution happens on ConcreteDomain.
  • Data source operation evolution happens on IDataOperation<T>.

Didn't I mention that Generic Strategy is used?

While this looks even more complex than standard Data Mapper pattern, it does offer both Active Record and Data Mapper goodness.

Share this post: | | | |
How Bad Can You Code?
27 April 07 06:02 AM | adrian | with no comments

We have seen bad codes (those with noticable bugs), ugly codes (copy-pasted codes), and worst codes (which doesn't even compile). But have you ever tried to create a really, really bad code on purpose?

If you haven't, or interested doing it, Alex Papadimoulis of Worse Than Failure (was The Daily WTF) has a big contest to write the worst code ever.

The rules are simple, build a four function (most likely add, sub, multiply, divide) calculator program in C/C++, but instead of writing a perfect program, create a code that passes all the test cases BUT very buggy and/or cleverly done. If you can make more bugs than the lines of code, you might as well on your way for the grand prize of a MacBook Pro or Sony Vaio (personally, I prefer the Mac, I've seen the Vaio). Interested? Details are here.

P.S.: I think I have an inspiration for this contest from the Code book I'm reading, now if only I can get some time coding (and learning) C/C++.

Share this post: | | | |
New Student Ambassadors!
26 April 07 08:15 PM | adrian | 2 comment(s)

Since Narenda and me have graduated, ITB has no SA for some time, until now. Last weekend, we interviewed several candidates and have chosen two of them to become the new Student Ambassadors.

Ronald Penalosa, a very creative guy, currently working on a Java-based company in Bandung (we're going to change that, ha!). The other one is Fajar Fathurrahman, a networking guy, very bright, and definitely IPv6-ready (just like Windows Vista!). I believe that both of them will become the key technology drivers in their own community because they are the best of the best.

Please drop some welcome message for them here or on their blogs; welcome to the club dudes!

P.S.: For the other candidates; you guys are cool, bright, and definitely unique, but there is a characteristic that we're looking for in a SA, and that's called passion. Whatever you do, be it programming, networking, even gaming, do it passionately, and show it to the rest of the world.

Share this post: | | | |
Currently Reading: Code
18 April 07 12:43 PM | adrian | 2 comment(s)

Code is a book by Charles Petzold. It explains to a regular joe about how computer hardware and software works. Petzold written this book very simple, it uses simple concepts (like morse code, flash light, etc.) to explain the massive computer system. A must for everyone.

Just in case you're wondering, I "bought" this book using the MVP voucher. The order receipt says "Softcopy", but in fact, it was "Softcover". :D

Share this post: | | | |
Filed under:
Back to Basic
07 April 07 10:44 PM | adrian | with no comments

As most of you knew already, I was awarded MVP for Visual Basic for the cycle of April 2007. It's been quite a while since I used VB, but I would never forgotten my origins. I've started my programming adventure using Basic family, Quick Basic. It's so easy yet so powerful I've made my own PIM with it (of course, it's console, but it has colors - thanks to QB).

Earlier today, my MVP Award Kit arrives. For a fresh grad like me, the contents are always welcome. I'm sure I can use the (pen + laser pointer + stylus) over my day-to-day activities, business card holder for connections, sticky notes for, well, notes, and a flash drive for files. Believe it or not, this is my first flash drive (I've been spoiled with Internet based file storage for years).

But the big thing is not the gifts but the chance to work with the company that shapes the world. Imagine proposing a feature that finally made it to the RTM and being used all over the world. That's something you can't have with companies with maybe better products, like Apple or Google for example.

I'm also retiring the Life of A Student Ambassador tag and replacing it with Life of An MVP. :D

Share this post: | | | |
Live Report: Trend Desktop 2007
07 April 07 08:32 AM | adrian | with no comments

Whoa... this is a crazy morning indeed. Will use a combination of Indonesia and English for quick posting.

08:00 Kampus diblokir, tidak ada yang boleh masuk. Alih-alih ternyata MJK mau datang. Kampus dipenuhi TNI dan Gegana. Ditutup sampai jam 3. Wah, tahu gini gw ga ke kampus.

08:30 Ketemu dengan panitia, katanya sudah diescalate ke Menkominfo untuk izin masuk. Ini karena acaranya span banyak kampus.

08:37 Gegana datang! :D

09:30 Acara dipindahkan ke Ruang Rapat Pimpinan. Dude, this room is crazy cozy one. Wifi, network projector, mic on every seat, wish we have this back on campus. Now we know who's been taking the money! Acara blm dimulai, masih menunggu panitia.

10:45 Done presenting, sekarang lagi presentasi Beryl oleh Andi. Nice!

11:00 Adinoto now presenting Mac OS X Tiger. They're definitely friendly.

11:30 Denni from Klorofil.org presented about WebOS. Presented on Windows Vista + Office 2007. :D

12:50 Done!

For closing, I brought four Windows Vista. 2 Home Premiums goes to the audience. 2 Businesses goes to the speakers. :D

Update: Adinoto has the post here.

Share this post: | | | |
Invitation to FSL
04 April 07 09:09 AM | adrian | 1 comment(s)

FSL (Free Saturday Lesson) is a (usually) weekly event held by Comlabs ITB (a unit maintaining internet connectivity for many students), and this Saturday topic is "Trend Desktop 2007". Details are here: http://www.itb.ac.id/agenda/520, and I've also put up the promo poster below.


(click image for full size)

Oh, there are (maybe) some Windows Vista for giveaway to attendants! :D

Share this post: | | | |
What's New in April
01 April 07 10:37 AM | adrian | 6 comment(s)

For starters, I'll definitely will need to get a new job or a new job title this month. That's definite.

Two, I just upgraded my laptop processor and memory. Now it has 19 cores and 10Gb of RAM (don't ask me how, they seem to have 5Gb pieces instead of 4Gb - is this the end of power 2 numbers in computers?). It's running wonderfully fast (not to mention all those Virtual Machines running in the background), but unfortunately I'll need to reinstall Windows Vista 64-bit first.

Here's the picture of my laptop running pretty much 4 OS at the same time (Ubuntu, Windows XP, Mac OS X 10.4, and Windows Vista as the host). There's so much core that only the first 12 are often used. Memory usage is high because VPC is running in the background.

 

Three, Telkom Speedy have reduced their prices. It is now competitive with Melsa (differs only Rp10.000), but still have lower bandwith (although cheaper investment cost). The cheapest package, which is Rp200.000 for 1Gb of traffic is very reasonable when you need always-on connectivity (especially useful when you're using Windows Vista).

Four, oil and gas prices are up, again. I've seen yesterday on MetroTV News that Premium is up 17% to Rp5.459 per litre for non-subsidised consumers. There's also a new GSM operator in town called 3. Unique name, I might hunt for some good numbers.

And finally, I am one step away from my PhD title. I have finished my paper titled "The Effect of Distributed Methodologies on Cyberinformatics" for peer review this month. If you'd like to see the full version, click here [PDF, 63Kb].

It'll be a great month!

Share this post: | | | |