Three-Tiers or Lots and Lots of Tears!

by Adam 9. December 2010 20:56

An application is really like your house or at least part of your house… maybe the garage. It’s exciting when you get it first and you can see so much potential. You slowly start adding in a few bits here and a few bits there. Next thing you know, it’s filling up with. It’s all useful little things like the lawn mower, workbench, stepladder but together it all seems a lot like junk. There’s just no structure to it all. What you need is a plan to keep it clean and tidy. You might know where everything is but what if you’re away and one of your friends is searching for something? It could take days, if the even do find it! Software is no different… mostly.

Previously, I blogged about the importance of design patterns and a few short rules which will help keep you on the right track. Now I’d like to mention the importance of layers of separation. First, lets get a few things straight. There can often be confusion between the definition of layer or tier. The standard is that a layer is a logical separation where as a tier is physical separation. In the case of three-tier applications (or n-tier applications), the separation may be physical but it should definitely be logical. As we are more interested in coding rather than the physical installations, what we are really talking about here is a 3-layer architecture. It’s also worth noting that the “3” is just the standard design, n is more suitable. Your software may only have two layers or it might have even more the three layers.

Presentation Layer – This is the User Interface (UI). This layer should only be interested in what the user wants and displaying data it receives on the screen. It doesn’t need to care about where the data comes from, how it’s formatted or how anything is calculated.

Logic Layer – The Logic or Business Logic layer contains all the rules for your application. This should include things like how data is formatted, if a daily report has already been run for the day or if a client does not have sufficient funds to extract more money. The Logic layer does not need to know how to connect to the data or how many databases exist or if a database even exists at all. It should merely contain functions like RunReport or GetClientFunds. The Logic layer tells the Data Access Layer (DAL) what it wants and that’s all it needs to know. The DAL looks after everything else. The information returned from the functions can then be checked to make sure it does not break any rules such as insufficient funding.

Data Access Layer – Depending on who you talk to, the application requirements or the architecture used, this layer might be integrated with the Logic layer. Ideally, it shouldn’t be but we all know we don’t live in an ideal world. For large systems I would recommend that you segregate the layers as much as possible, having said that, there are times when it just isn’t needed and can be seen as over kill. Of course, all that is off-record! The last thing I want to do is get involve in a debate about the merits of rigid architecture models and purist views. So what is the DAL? This layer knows all about the Data Stores, what is being used (SQL database, XML files) and how to connect to it. It doesn’t know that users exist or that they want shiny looking ASP.NET presentations and it certainly doesn’t care. Separation of responsibility; This layer has it’s job to do and that’s all it cares about.

Data Store – This is where all the data is kept. It could be a simple XML file or a small SQL database or spread across multiple servers. Whatever it is, all it should know is that data is stored here. When a request comes in from the DAL then it checks the data and spits it back to the DAL. That data it gives to the DAL should be just that, data. It should not be information. The difference being that data is the raw format which could be very difficult for a user to decipher if they saw it where as information is data formatted into something which is clear and easy to read for the end user.

ntier

An n-Tier Architecture. (source: MS Patterns and Practises).

Now you may be looking at that and thinking ‘I really don’t need so much detail’ but please remember that you actually do! If you don’t have a strong architecture then your simple application will quickly become more complex when further changes are required and that’s not even thinking about the amount of time lost when someone else will try figure out your code or time you have to spend after not looking at it for several months!

When designing it’s important to know the in’s and out’s of each layer. How they connect to each other and which layer is responsible for what. That is the minimum knowledge you should have before starting the actual programming. Otherwise things will get messy! Very very messy.

I really can’t stress the importance of this enough. You might not have systems that require many changes after design or which are never scaled up or maintained but you WILL come across a situation where these layers are mingled to a point where they are impossible to separate. The presentation layer will contain SQL statements, the DAL will create webpage's or similar ridiculous situations. One experience of that and then you’ll understand the hard way. So start to remedy that now. Follow the structure and at least you’ll know that your code is of a high standard. If everyone done this then we’d be one step closed to a better world.

Mogwai Law and Design Patterns

by Adam 10. November 2010 17:10

Any developer worth his stock knows that all software solutions need to follow (no matter how roughly!) a design pattern. Not only will this make the design phase easier to model and explain to others but it will also help to create scalable, reusable and elegant solutions. The type of design pattern that should be adopted is entirely dependant on the application. A Singleton pattern might suit a classic desktop application where as MVC would prove more useful when developing a web application. You may of heard of the term "Gang of Four". This refers to the four authors of a book called "Design Patterns: Elements of Reusable Object-Oriented Software", in which they detailed 23 classic design patterns.

One way to understand this design process is, what I like to call, the Mogwai law. If you can stay within the limits of the three rules then you'll be happy, your clients will be happy, any problems that arise can easily be fixed and everyone will be smiling with delight while playing with the new, cute and furry, software.

But if you neglect the rules, if you try to bend them a little too much or think 'I'm sure I can get away with it this one time' then you may be in for a shock and lots of pain. Your software can quickly turn against you, seemingly small changes may take hours of coding. Rather than increasing client productivity your software will have them running in terror and, in all too many cases, they will never return. Once a client's trust is damaged it is difficult to get them back on your side.

Luckily, there is a solution - Good Coding. This begins with choosing a design pattern and following a few little rules. The three main rules I like to adhere to are listed below. Be warned, they are only the main rules and others do exist but if you can keep within these three then you should be doing better than a lot of code I have had the displeasure in sorting out.

1. Minimise Complexity

What this means is that you should only create what you need to create and each component you create should be as simple as possible. Never try to create one super control to do everything unless not doing so would increase the overall complexity. Dividing a small windows application into multiple components, that you know will never be used, might be over kill and although it would simplify the individual components it would increase the overall complexity of application, way beyond what is required. The goal is keep things simple overall. The simplest solution when it comes to development, testing, using and expansion.

2. Limit the Knowledge

Each component within your application should be good at what it does and only that. There is no need for the UI to know how to access the database or calculate the latest figures. The UI should only be interested in how to display information while a separate business or data layer can worry about how to get/calculate data.

3. Never Repeat Code

If your application is going to be reusable, scalable and manageable then you should never repeat code. Having to update an SQL query in a single location is fair more enjoyable than having to edit twenty identical SQL queries.

The most common patterns that I use are Factory, Singleton, MVC, MVP and MVVM.

For more information on Gang of Four patterns then check out dofactory and if you are looking for a detailed Architecture Guide then Microsoft Application Architecture Guide/ is a great source for information, learning and as a reference.

About Qwerty.ie

Qwerty.ie is all about technology; news, coding and life. Catching the overspill from one man's mind and blending it with everything from news, information, coding guides, tips and general ramblings before splattering it across the pages of a Blog. Which Blog, you ask? This Blog! Now with pictures and technicolor.

Keep up-to-date with all the latest Qwerty thoughts by subscribing to Qwerty.ie and find out what feeds the Blog in Qwerty's Articles of Interest.

Email Qwerty.ie  Subscribe to Qwerty.ie

All Rights Reserved - Qwerty.ie