Showing posts with label Fundamentals. Show all posts
Showing posts with label Fundamentals. Show all posts

Friday, July 17, 2009

N-Tier Design Revisit part 1 – Over View

Back in March I did the blog entry The value of N-Tier Design, I didn’t provide any code, it was just a simple here is why N-Tier is good. After some reflection I have decided to revisit the subject and go a little more into detail.

The primary focuses of N-Tier is to provide a separation of concerns that will make your code more testable and more importantly maintainable, remember the code you write today, is the code you maintain tomorrow. Most of what I’m going to cover are some simple suggestions to make your life easier by putting in a little extra work up front, they may not be applicable in all situations, like a small one time use app for parsing a log file, some good rules of thumb are, if you can rewrite the application in less then 4 hours or it’s a proof of concept for a specific technology, it’s a little over kill for that.

The basic layers of N-Tier are

  1. UI Layer – Contains the logic for interacting with your user, this could be a web form, web service, command-line application, Winform, etc.
  2. Biz Layer – Contains business logic like data validation, manipulation, etc.  Basically any logic that deals with business rules belongs in the Biz
  3. Data Access Layer – Getting the Data and mapping it to Entities.  this could be from a Data Base, a 3rd party web service, an xml File, etc.
  4. Data Entities – Are not a layer, but how data is passed from one layer to another

Layer can consist of a single class or a set of classes that perform different tasks as long as they are doing what that layer is tasked with.

The basic rules for N-Tier are simple

  1. Layers can only talk to the layer right next to it, the UI layer can only talk to the Biz layer, the Biz can talk to the UI layer and the Data Access but the Data Access layer can only talk to the Biz Layer
  2. A Layer should have no internal knowledge of any of the other layers, A Biz class should have no understanding of how the DAL gets the data that it gives the it, The data could come from a Database, an XML File, or be randomly generated. Getting the data isn’t the Biz layers job so it shouldn’t care where it comes from, this becomes more important when you start using dependency injection and the underlying code may change completely.
  3. The layers should be decoupled and use interfaces to communicate with each other, this makes it easier to swap out code, use dependency injection , etc. you should be able to completely change a layer and as long as it keeps the same interface.

Monday, June 22, 2009

The Simple Fundamentals we Forget

A little while ago a frustrated co-worker asked me for some help, he was working on consuming a web service and needed to add an array of strings to the request based on user input and he couldn't figure out how to add a new item to the array. He could find lots of examples of how to create the array, and how to populate it at initialization, etc. but he couldn't find out how to just add a new item. When i explained to him that you couldn't he was a little shocked, all of the other collection type let you add new items, why wouldn't an array? In his defense he came from a dynamic language background, where an array is more like an ArrayList in c#.

This got me thinking about some of the simple fundamentals and basic language features that I don't remember exactly how to use, mostly because I never use them. A good example would be the Conditional Operator
   1: string fileName = tempFileName != null ? tempFileName : "Untitled";
it's purely a personal preference, but I don't like the way they look in code and I think they make it harder to debug, having said that they are a fundamental part of the language and as a good developer I should know how to use them. This is got me thinking about how I should use parts of language/framework that I don't normally use just to keep in practice.

I think this could be beneficial in a couple of ways, primarily it would require me to look for things I'm not using and force me to think about how to use them. Second it would make me think why I don't use them. A prime example is an abstract classes, I know how to use them I just don't like to. Maybe if I forced myself to use them on occasion I would find a place where they are better suited then an interface or I'm just going to prove why I like interfaces better, but the important part is that I reaffirm why I like interfaces more then abstract classes.

At times I think the hardest part of learning new things is not forgetting what I already know, maybe I need to take some time to write some practice code or proof of concepts just for the practice.