Code Generated DAL (Data Access Layer) using ORM - Article 2

by Guido Tapia

in software-engineering,

July 9, 2009

PicNet and PicNet.Data Projects

Hi All, In this article we will look at some of the libraries/classes required by the generated DAL. These libraries simply have base classes and utility classes that make life a little easier.

PicNet.Data.IEntity

IEntity is the core interface of the data layer. This interface defines all common behaviour for all entities generated by the code gen. This helps with polymorphism and generally helps keep things nice and neat.

PicNet.Data.AbstractEntity

AbstractEntity is just a default implementation of many of the methods/properties defined in the IEntity interface. All generated entities will extend this class to get some default behaviour. Abstract entity also contains some utiltity methods that help in getting some meta information about the entities.

PicNet.IGetSetPropertyValue

You will notice that IEntity extends IGetSetPropertyValue. This interface defines a way of setting and getting property values. One of the beauties of code generated code is that you do not have to rely on reflection for dynamic property access. Simply have your code gen generate a SetPropertyValue method and you have lightning fast dynamic property access. i.e (This is taken from the generated DVD.cs class which we will see in the next Article).

protected override void SetPropertyValueImpl(string propertyName, object value) {
  switch (propertyName) {
    case "DVDID": ID = (int) value;break;
    case "ID": ID = (int) value; break;
    case "StorageUnit": StorageUnit = (StorageUnit) value; break;
    case "StorageUnitID": StorageUnitID = (int) value; break;
    case "DVDName": DVDName = (string) value; break;
    case "DVDDescription": DVDDescription = (string) value; break;
    case "DateCreated": DateCreated = (DateTime) value; break;
    case "DateLastUpdated": DateLastUpdated = (DateTime) value; break;
    case "Name": Name = value; break;
  default: throw new ApplicationException("Property: " + propertyName + " was not found in type DVD");
  }
}

PicNet.Data.DataFacade

The DataFacade is the brains of the whole operation. I’m not sure if DataFacade is an actual Facade, I mean, is it an implementation of the Facade pattern? Who knows but I like the name because its the entry point to the data layer. You can think of the DataFacade as a generic Repository which I believe is much neater then the repository pattern which would in this example lead to 3 classes (+ all the interfaces and test implementations) to access the data layer, these are:

  • DVDRepository
  • StorageUnitRepository
  • StorageUnitTypeRepository.
  • Image a complex data layer with hundreds of entities. Horrible.

Utility Classes

The PicNet project has a lot of utility classes that support the PicNet.Data classes and the generated objects. Some of these classes include CollecitonUtils, LogUtil, UReflection, Utils, XMLUtils, etc. Please read through these classes if you like they are very self explanitory.

Note

It is important to note that these projects have been around for years and for the purpose of this article I have just gone through and canabilised them, I removed:

  • All code I did not want to share (encryption, etc)
  • All code that was perhaps not directly required buy the basic DAL functionality I want to show you

However you may still find archeological evidence of these other classes which you should just ignore, again I am doing this to show you how you can build your own code gen DAL not really to provide you lots of great quality code.

Downloading the Solution

You can download the entire code for this article here.

Next Article Generating the hibernate POCO objects.

Guido Tapia Software Development Manager PicNet Pty Ltd Email: guido.tapia@picnet.com.au