Monthly Archives: December 2007

Thumbritis or WTS (Weird Typing Syndrome)

I’ve been having extremely bad pain in the joint that joins my thumb with the rest of my hand. I didn’t realize what it was until a couple of days ago. Turns out that I can blame it on computers. I’ve been working a lot on my laptop lately. Actually I work on my laptop 100% of the time, but most of the times I plug in an external keyboard and screen. However, in the past month or so I’ve been using the device’s keyboard itself. The other day it hit me why I was having this excruciating pain. It’s because of the way I use my thumb to get to the right shift and cursor keys. Turns out, as shown in the wonderful diagram below, I bend my thumb under my hand to get to the keys instead of using my right pinky for the shift and moving my whole hand down to get to the other keys. Using my thumb has made me extremely fast with the keyboard.

 

My pain problem is that right now, as stupid it as it may seem, it’s extremely hard to get rid of the habit. So I really feel like Homer every time I repeat the same movement and cringe in pain. Doh!

So, if you’re like me, don’t do this movement. Stop it before it starts to hurt, literally!

Layered architecture

When developing applications using a layered architecture, one of the problems that arise is how to pass information through tiers, in particular between the data and business layer. Let’s assume that we have an employee entity that accesses an employee data class to retrieve the information from the data store. Employee is in our business assembly and our repository and data layer is in our data assembly. The repository can and should ideally be in a separate assembly, however for simplicity we will keep it in the same for this example since it doesn’t affect the situation.

{%URL.DOC

Since BusinessLayer calls EmployeeRepository to get an instance of a particular employee, BusinessLayer needs to reference DataLayer. In turn, since DataLayer creates an instance of type Employee, DataLayer needs to reference BusinessLayer. However, this causes a circular reference problem.

One solution to the problem would consist of using a DTO (Data Transfer Object) in a third assembly to transfer information. BusinessLayer will now reference DataTransfer, as will DataLayer, avoiding a circular reference.

{%URL.DOC

This however adds a new class to our domain which isn’t strictly necessary. It also means that we now have to copy values from EmployeeDTO to an Employee class, since EmployeeRespository returns type EmployeeDTO.

Using interfaces and generics

In order to avoid the above situation, we can make use of interfaces and generics to solve the problem. Instead of having EmployeeDTO, we create an interface IEmployee that Employee implements. This interface only declares the properties employees will have. Methods are particular to the business needs and are only present in the actual Employee class.

{%URL.DOC

Let’s look at the resulting code.

{%URL.DOC

The EmployeeRepository code should look something like the following

{%URL.DOC

However, we now face a new problem. FindEmployee is returning an EmployeeDL. To do so, it’s calling EmployeeDL. EmployeeDL cannot reference BusinessLayer, since if it does, we’d be in the same boat (circular reference). EmployeeDL should return an IEmployeeDL. This seems simple enough, except it’s not possible to create instances of interfaces for obvious reasons. This leaves us with the problem of returning an instance of EmployeeDL, and to do this we make use of generics. For this, we implement EmployeeDL in the following way

{%URL.DOC

We are forcing the generic type to implement IEmployee and also have a parameterless constructor. This allows us to create new instances of the type and also assign the properties of an employee object. The only thing left to do is change the EmployeeRepository to use this code.

{%URL.DOC

We now have a clear separation of layers without the need of introducing additional classes to our domain, complicating the scenario when it’s not completely required.

Note: The point of this post is to show you how to pass data between tiers without using auxiliary objects, whether this may be a DTO class, an XML file, a DataTable or DataSet or whatever else you might like. I’m not debating which method is better, although I have a preference for the one shown in this post.