Search This Blog

Thursday, April 26, 2012

How to decouple the business layer and the DAL suing the provider model pattern

A way to make the DAL layer indipendent of the BLL is as follow
The Layer between the Business and Data Layer will be made of Abstact Classes (instead of interfaces)
The Idea is that each abstract class has a public static get property "Instance" that use reflection to create a concrete class. The concrete class to be created is defined in a configuration file
The code call will look like this

IRSBodyProvider irsBodyProvider = IRSBodyProvider.Instance

Or if we create a helper class DataProvider we will have

IRSBodyProvider irsBodyProvider = DataProvider.IRSBody

---------------------------
Code
---------------------------

public Abstract IRSBodyProvider
{

   static private ArticlesProvider _instance = null;
   ///

   /// Returns an instance of the provider type specified in the config file
   ///


    static public ArticlesProvider Instance
   {
        get
        {
             if (_instance == null)
            _instance = (ArticlesProvider)Activator.CreateInstance(
                     Type.GetType(Globals.Settings.Articles.ProviderType));
            return _instance;
         }
   }
}

static public class DataProvider
{
    public static IRSBodyProvider IRSBody
   {
       get { return IRSBodyProvider.Instance; }
   }
}


Another way to make the DAL Layer indipendent of the BLL is
1) Create a Layer between the DAL and BLL with Provider Interfaces
     IIRSBodyProvider (with the CRUD Method signature)
     ITRSBodyProvider (with the CRUD Method signature)

2) Then we add in the same Layer an abstract factory "ProviderFactory" that creates/gets the provider inferfaces

IIRSBodyProvider IProviderFactory.GetIRSBodyProvider()
ITRSBodyProvider IProviderFactory.GetTRSBodyProvider()


3) Then we create in the DAL some concrete implementation of the IProviderFactory.

class SQLProvideFactory : IProviderFactory {

   IIRSBodyProvider IProviderFactory.GetIRSBodyProvider()
   ITRSBodyProvider IProviderFactory.GetTRSBodyProvider()
}

calss XMLProviderFactory : IProviderFactory {

   IIRSBodyProvider IProviderFactory.GetIRSBodyProvider()
   ITRSBodyProvider IProviderFactory.GetTRSBodyProvider()
}
Note how we are implementing the Provider Model Patter + Abstract Factory Patter to create decoupling from the Layers.
In Addition note how the two methods are the equivalent of the Instance method created in the first solution of the abstact class. They are the ones creating the concrete class. In this solution is the factory that is creating the class non the static method of the abstract class)

4) We then create a static class client of the factory called DataProvider that instanziate a concrete IProviderFactory using Reflection.

The call to the code will look like this

IIRSBodyProvider irsBodyProvider = DataProvider.IRSBody

We will use this kind of call in the retrive method of the IRSBody object.
The decoupling of the BOL and the DAL is complete

This is a Static Class. It works more or less like this

DataProvider.IRSBody.Retrieve(irsId, trsId)
DataProvider.TRSBody.Retrieve(trid, fundId)



-------------------------------------------------------------------------------------------------

PSEUDO CODE FOR THE CLASS

--------------------------------------------------------------------------------------------------

static Class DataProvider {
    //We provide a private member to hold and instance of the class
    static private IProviderFactory _factory = null;
    static DataProvider()
   {
     //NomeAssembly and SomeProviderFactory can be read from some configuration file
     string nome = "NomeAssembly";
     Assembly assembly = Assembly.Load(nome);
     _factory = (IDataProviderFactory)assembly.CreateInstance(nome+".SomeProviderFactory");
    }

  public static IIRSBody IRSBody()
  {
    // Here we assume that the factory does not create a new instance of the provide each time we call the
    // method get.
      return factory.GetIRSBodyProvider();
   }
}
 
 

No comments:

Post a Comment