C# – Business Objects and Data Layer

cdata-access-layern-tier-architecturevisual-studio-2008

This site has provided me with many useful answers, however after a hours search I haven't found anything that specifically answers my needs. So here goes…

The company I'm working for is in the process of designing a new Business Objects Layer and a Data Access Layer – these will reside in separate assemblies.

The problem is I'm having a hard time getting my head around the interaction between these two layers – specifically, should the DAL know about the BOL, I've read numerous articles that have said the dependency order should go something like this:

GUI / Presentation –> BOL —> DAL

But as far as I can see, the DAL needs a reference to the BOL in order to be able to 'return' objects to the BOL layer.

I'm going for a intermediate assembly between the BOL and DAL which will be basically a thin layer filled with interfaces to decouple those two DLL's, so the framework can use different DALs if the need arises.

This lead me to the idea of introducing another thin layer with a bunch of interfaces that the BOs implement, then when the BOL calls the DAL interface, it passes it an object which implements one of these BO interfaces and then the DAL proceeds to populate the object. This removes all dependencies between the BOL and the DAL – however, I'm finding it hard to justify it to be honest.

Ideally we would like to use an ORM as it just removes the need to write CRUD stuff but our clients have a habit of fiddling with column lengths on their database and this is the cause of most of our errors to-date using the strongly typed DataTables. I've heard Linq2SQL also stores column lengths at compile time, not sure if NHibernate does or not (but, I'm not sure if our Database Schema is designed cleanly enough for NHibernate, pitfalls of dealing with legacy systems).

So yea, any insight on the relationship between a BOL and a DAL would be very much welcome – apologies if the above is poorly written, if anyone needs clarification I'll be happy to provide more detail.

Marlon

Best Answer

The way the I do this is the BO expects a DataReader or a DataContext or whatever back from the DAL, not the actual formed object. It is then the job of the BO layer to take and fill itself from the object that came back. The DAL isn't returning back a completed BO. The main thing to remember is that changing something in the BO layer shouldn't cause issues for the DAL layer, but changing something in the DAL layer could cause issues for the BO layer.

A short example of what I typically do

In the BO layer

FillData(){
   DataReader dr = DataLayer.GetData("SomePropertyForAStoreProcedure");
   If dr.Read(){
    Property1 = dr.GetValue("Property1");
    //So on and so forth
   }
}

In the DAL

DataReader GetData(String SPProperty){

}