Jan 31

This post is my 2 cents contribution to the debate on persistance stack architecture. I try to write short post (the shorter it is…the understanding greater is), but this time —with such a topic— I can only fail on this point. So this post will be followed by other posts :-).

I will not start from scratch explanation on the different approachs, the wording and so on…you will find some links in this post that I find usefull and I do not want to be redundant with them. I would like to enlarge the scope of the persistance stack to higly customizable environement and also the various features required by an Entreprise Management Application (taht I will name EMA) relying on a persistance layer.

What are the different layers (required or not)

You will find here an excellent article on the different layers of a typical persistence stack and further…to the UI. The UI is key but I will not address this question in this post. You will notice the author talk about a Persistence Layer which is different from an Persistence Stack. I use the word “Persistance Stack” to express all the layers that are requiered to build an application that use a database without the UI. These layers are usually (from the upper level to the RDBMS middleware):

  • Service Layer
  • Domain Layer
  • Data modeling
  • Persistance Layer
  • RDBMS middleware 

About the interest to respect layering

It’s just about commion sense : by layering correctly you will decouple your code and allow easier replacement of components as there are inricated. Most of the time, developers are doing naturally as it give an impression of over complicated the code. The important point they have to though about that an Entreprise Software relying on database will grow and evolve (faster at the begining) and you will write at the end touthands lines of code. If the rules and delimitations are clear from the begining, you will avoid a real mess of large classes.

Now let see in details the aim of each layers. I will go from down to top to express clearly the boundaries of each layer (starting from top tempt to mix the layer and put the code/requirement at a higher level than it should).

Persistance layer

The persistance layer is juste about persistance…obvious but not always respected. Persisance is only about storing and retreiveing data, nothing else. You can use an ORM (Object Relational Mapping) framework as Hibernate or TopLink for example. It’s only bout storing, that means no validation, no business logic at all, and the objects should be limited to structures, no methods. There is no real interest to build it’s own persistance layer as most of the time your requirement are fulfilled by the most advanced frameworks. That does not means you should not take care in the choice of this framework as this layer is the key for the performances: having a poor framework (even a rich ones depending on how you will use them) will dramaticcaly impact performance by generating useless or not optimized RDBMS queries.

Most of the time these objects are named DAO objects or DAL objects (for Data Access Layer objects).

Tow extensions to EMA that may be include in this layer are rights and partitioning. Rights management is the ability to give read/write access to attributes of the DAL objects. The Partitioning is the ability to segregate the read/write access to DAL objects. These fucntionality have obviously a business aspect,e specially for the partitioning. But for performance reasons (espacially for partitioning which is usually implemented using a where statement), this might be implemented in this layer.

ORM..a myth or not? 

The different post, disucssions and articles I’ve read about persistance stack are highlighting the interest of doing OOP (Object Oriented Programing) especially in the domain layer. Most of the argument yield to the mandatoiness of using an ORM which is the key pivotal framework to step from a relational database to a obejct oriented coding space. This a myth to me and is only relevant for folks that love OOP and particularely love debate about the beauty of it.

ORM use have to be limited to what it is design for: just create DAO objects for upper layer. Some folks abuse of this because they are mixing it with the higher level layers which is strong mistake. I’ve looked at Hibernate and NHibernate (to simplify: NHibernate have the same capabilities than Hibernate v2, but in a .Net environment). As a framework it provides lots of different ways to operate with the RDBMS layer, but you usually use on 10% of them as you are doing some assements. IMHO, this is not a mistake to do so, if you take care that this assement will not have a strong impact on the other layers. You will never be able to take some decisions that have absolutely no impact, but you can mitigate this.

An example of assement is to chose the same type for keys: there is a real advantage to do it, but it will have an impact on the Domain Layer and the evolution of your application (if the type is long…evolve to GUID for example may be tricky if they are exposed and used in the other layers). On this specific point, a good practice is to never expose internal keys outside of the layer but to expose business keys in the Service and Domain layers. For each object, exposing 2 kind of business kay is useful: an international one, and a business one. The international one is a key that won’t varie from a language to another, that is usually a sequence of numbers, which is used by external programs to synchronize data for example. The business one is a key that is usually used by humans over business process as a social key for a employee for example, or a barcode for an asset.

Data Modeling

This layer is most of time mixed with the Domain Layer and Persistence layer, but it can be useful to detail its contract.

In my opinion, part of the contractrthis layer is the dual of the relationship aspect in RDBMS. I deed, you have to define the relationship between various objects in the persistance layer (as a purchase order have order lines for example), but you have to define some low level behaviour. Some of you could say that I start to introduce some business logic here…no, i don’t but you are right: we should not introduce business logic here yet. The behaviour I’m talking about are related to the impact on relationships and complex custom types during CRUD (Create Retrieve Update Delete) operations made by the persistance layer.

A complex custom type is a type that rely on more than one attribute of a DAO object. Here are 2 examples.

Currencies: Handling of currencies is not simple, you have to be able to do arithmetics operations on them, and convert them. Obviously a double type field is not enough, neither a string (when I say string, is a string having only one information, not a string containing an xml description of a currency object with multiple attributes). So in the data modeling you could declare that the currency attribute of the value object will be rely on a set of attributes (amount, currency symbol, reference value in $, date of reference convertion) of a DAL object.

Enumeration: Some enumerations fields may have these requirement. First: the user can add new values. Secondly: some values used by the business layers may not be removed. A typical example is the status of a purchase order. The aim is to present some choices to the end-users (all values already entered by other users for a status plus some predefined fixed values). For technical reasons you may store a predefined fixed value in an attribute and other values into another field, but you want your applciation not to be aware of this.

As you see these are low level behaviour. That the same for relationship: if an object of type A is linked to an (or a collection of) object B, B should be deleted if its parent (from a relationship perspective) A is deleted or B should be attached to another object of type A? Does it makes sense to have an object of type B without an association to an object of type A? All this behaviour have a business rational but this have to be implemented in this layer.

Regarding this last sentence, it really make sense to gather together as much as possible the configuration of almost all layers inside one location. In a metadata driven environement, this is absolutely not an issue to mix configuration of different layer inside a unique location (a file or a a folder)…more over, I would recommend it.

Where do I put business logic?

To continue here

Other interesting links
Segregation/Distinction between Service Layer and Domain Layer

Jan 29

hype curveIn a previous post on dynamic languages, I talked about technology adoption curve. I found an article on the hype cycle of the Gartner which is exactly what I talked about. This article is on the vision of the gartner on the different hype technologies, especially Web 2.0.

Jan 20

I’ve come across a bug or strage behaviour of the asp .Net control ImageButton the other day.
If you don’t specify a value for the attribute “ImageUrl”, the server will do an access to your default page. To highlight this I’ve created a small project sample that you can find here.
This project contains a default.aspx page, and 2 pages: one with an ImageButton control with attribute “ImageUrl” set (page1.aspx), the other one without the attribute (page2.aspx).

If you lanch the web application: you will see in the output:

Goes through _Default.Page_Load with IsPostBack:False

Go to the page1.aspx page by clicking on button “Page 1″, you will see in the output:

Goes through _Default.Page_Load with IsPostBack:True
Goes through Page1.Page_Load with IsPostBack:False

Now click on the back button of your explorer, and go to page2.aspx by clicking on button “Page 2″, you will see in the output:

Goes through _Default.Page_Load with IsPostBack:True
Goes through Page2.Page_Load with IsPostBack:False
Goes through _Default.Page_Load with IsPostBack:False

You can notice the access to default page. To avoid useless access to the default page, even if you want to setup the ImageUrl property of the control programatically, set a value in the page.

Jan 17

 I’m exploring creation of Package in Visual Studio 2005 and I was not able to build a dummy project. After different tries, the problem was due to the empty key.snk file. I then tried to create on using the command line:

C:\>sn -k key.sn
Unfortunately I get the following error:

Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.

Failed to generate a strong name key pair — Access is denied.

Finaly ‘google’ helped me : solution is there. Thanks to Aaron for this useful tip. More over, I confirm that now if I create a new dummy project …visual no more create an empty key.snk file.

Jan 14

bushVoici quelques citations de G W Bush. Je n’ai pas vérifié leur véracité…donc a prendre avec précaution et surtout humour. La cerise sur le gateau: regardez le sens du livre que tient le président américain.

  • Nous sommes prêts pour tout événement imprévu qui peut ou ne peut pas se produire.
  • La vaste majorité de nos importations vient de l’extérieur du pays.
  • Si nous ne réussissons pas, nous courons le risque d’échouer.
  • Une faible participation est une indication que moins de gens sont allés voter.
  • Ce n’est pas la pollution qui attaque l’environnement, ce sont les impuretés dans notre air et notre eau qui en sont responsables.
  • Le futur sera meilleur demain.
  • Pour la NASA, l’espace est toujours une haute priorité.
  • Je crois que Dieu a créé le monde. Et je pense que nous sommes en train de découvrir plus et plus et plus comment c’est actuellement arrivé.
  • Je pense que nous sommes d’accord: le passé est terminé.
Jan 12

Univers élégant Voici un excellent livre de vulgarisation scientifique. Les 2-3 premiers chapitres sont très basics…et donc très abordables. ensuite cela se corse un peu quand il aborde la théorie des particules, mais il faut avouer que je connais pas très bien non plus. Ensuite, la théorie des cordes est enfin abordée et cela devient très captivant…pour les habitués du genre. C’est bien écrit, beaucoup de metaphores simple et claire, et on ne croule pas sous les formules ou explications trop techniques ou inutiles. Bref, je recommande cet excellent ouvrage à tous les fans du genre. Si vous voulez l’acheter, clickez ici.

Jan 08

Here is really great post on principals to write maintenable code. More over this can be extended to improve supportability of the products.

Most of the developers are doing development only! It does not sound like a problem for you…I thing we are at the exact barrier between real development and hobby work. I’m not that fan of Agil development as it does not work well if not apply correctly and with a minimum of knowledge, experience and willing to do it. Nevertheless, Agil methodology as the real advantage to melt the concerns and responsability of QA, dev, CM over the entire team. From what I’ve seen, usually, development task are only 20 to 40% of to whole process to implement new features (and I don’t take into account the maintenance, support and bug fixe after release). Do not use the fake argument that it depends on the environment (Language, Framework, QA automation rate…etc), it’s just wrong. I prefer by far having a developper that knows less about a language or a framework but concerned by CM, QA, comprehensiveness and maintenability of features/code than a rely strong developer that will force to use the last framework release that he knows and break build process…even if his coding skills are higher. To make it shorter: don’t care about improving efficiency about 10% on 20-40% of the process, but I care if it’s on 60%-80% of the process plus maintenance, evolution…etc.

A sentence from the article that are fiond terribly true: “there is an inflection point where a coder mindlessly spewing out code transforms into a thoughtful software craftsman capable of creating maintainable code. That inflection point happens the day a coder first stops, lifts his/her nose out of the coding window, and says to him/herself “where should this code go?”.
I’ve seen this many times and is named “Seperation of concerns”. That does not mean using Spring is mandatory do code correctly :-), but as soon as a developer start to think this way, he will improve maintenability, componentization, and avoid spagetty development with useless interdepedencies. We see new languages as Ruby, Python that are marketed as “less code to implement the same feature” than with other languges as Java and C#. I agree that simplicity is fondamental and less code also, but this should not lead to weird code that can’t be maintained: if seperation of concerns leads to 3 additional lines of code to do a back-and-forth between 2 layers…that’s good.

Jan 06

This is the first tip of the year :-) To debug newly written code in the global.asax file, the easiest way is to force a save of web.config file. Each time the config.file is saved, the application is stopped and started.

Jan 02

Bonne année et meilleurs voeux a tous.