locked
n tier design Service accesses data layer? RRS feed

  • Question

  • Hi,

    In an n-tier architecture should the Service Layer access the Data Layer directly? or should it go throw the BusinessLayer ?

    Shouldn't the layer be able to access only the below layer?
    Me http://www.zapacila.com
    Monday, July 20, 2009 11:45 AM

Answers

  • Hi,
    Service layer should not access Data Acess layer unless it's a Data service layer which provide a data service for business logic layer.

    Shouldn't the layer be able to access only the below layer?

    Yes, and this the resoan we called it a layer.

    The best practice is don't make service layer access the business logic directly inserted access the business facade which access the business logic, so now you have a service facade layer and business facade layer and all business logic encapsulate inside the business logic layer.

    For more infomation see the following patterns & practice link:
    http://apparch.codeplex.com/Wiki/View.aspx?title=Application%20Patterns&referringTitle=Home

    Thanks


    We are volunteers, if the reply help you mark it as your answer. thanks!!
    http://mohamed-radwan.spaces.live.com/default.aspx
    Monday, July 20, 2009 12:09 PM
  • Dear Jap,

    We had similar question on Architect Role on 11th July and the title is "service layer management" , for your reference.

    Service layer exposes the Interfaces that handles the business logic, which could be authentication, authorization, etc. So it always sits on top of the Business layer.

    And it is not preferred to access the data layer directly from the service layer.

    Cheers,
    Vishal Mohan

    Monday, July 20, 2009 12:30 PM
  • Still if the direction is from top to bottom and  Service-> Business -> Data Layer how will datalayer return data? if what format?

    Hi,
    There is more than one option, you can return DTO which is a business logic class without operations (Methods) there is a very good article for Rudy Lacovara on DTO here
    http://rlacovara.blogspot.com/2009/02/high-performance-data-access-layer.html

    Another option you can return data set or reader etc, there is a good thread on ASP.NET forum which discuss similar question
    http://forums.asp.net/p/1120895/1753031.aspx

    Thanks


    We are volunteers, if the reply help you mark it as your answer. thanks!!
    http://mohamed-radwan.spaces.live.com/default.aspx
    Tuesday, July 21, 2009 5:28 AM
  • Now lets take a scenario of an employee management, for the employee management we have the Business layer which comprises of CRUD Methods, and others methods required.

    A Service layer can be built on top of this business layer and can expose a set of related Methods for example:-

    //the service layer method

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IList<Employee> SearchEmployee(string EmployeeName)
            {      
                  // add the CAS    

                return employeeBusinessObject.Search(EmployeeName);
            }


    // business layer

    public IList<Employee> Search(string EmployeeName)

     {

      //add businesslogic;
       

                return employeeDataLayerObject.Search(EmployeeName);

     }

    hope this makes some sense.

    Tuesday, July 21, 2009 8:35 AM

All replies

  • Hi,
    Service layer should not access Data Acess layer unless it's a Data service layer which provide a data service for business logic layer.

    Shouldn't the layer be able to access only the below layer?

    Yes, and this the resoan we called it a layer.

    The best practice is don't make service layer access the business logic directly inserted access the business facade which access the business logic, so now you have a service facade layer and business facade layer and all business logic encapsulate inside the business logic layer.

    For more infomation see the following patterns & practice link:
    http://apparch.codeplex.com/Wiki/View.aspx?title=Application%20Patterns&referringTitle=Home

    Thanks


    We are volunteers, if the reply help you mark it as your answer. thanks!!
    http://mohamed-radwan.spaces.live.com/default.aspx
    Monday, July 20, 2009 12:09 PM
  • How will the Data Layer return data? using DTO`s?
    Me http://www.zapacila.com
    Monday, July 20, 2009 12:25 PM
  • Dear Jap,

    We had similar question on Architect Role on 11th July and the title is "service layer management" , for your reference.

    Service layer exposes the Interfaces that handles the business logic, which could be authentication, authorization, etc. So it always sits on top of the Business layer.

    And it is not preferred to access the data layer directly from the service layer.

    Cheers,
    Vishal Mohan

    Monday, July 20, 2009 12:30 PM
  • Still if the direction is from top to bottom and  Service-> Business -> Data Layer how will datalayer return data? if what format?
    Me http://www.zapacila.com
    Monday, July 20, 2009 12:44 PM
  • Still if the direction is from top to bottom and  Service-> Business -> Data Layer how will datalayer return data? if what format?

    Hi,
    There is more than one option, you can return DTO which is a business logic class without operations (Methods) there is a very good article for Rudy Lacovara on DTO here
    http://rlacovara.blogspot.com/2009/02/high-performance-data-access-layer.html

    Another option you can return data set or reader etc, there is a good thread on ASP.NET forum which discuss similar question
    http://forums.asp.net/p/1120895/1753031.aspx

    Thanks


    We are volunteers, if the reply help you mark it as your answer. thanks!!
    http://mohamed-radwan.spaces.live.com/default.aspx
    Tuesday, July 21, 2009 5:28 AM
  • Now lets take a scenario of an employee management, for the employee management we have the Business layer which comprises of CRUD Methods, and others methods required.

    A Service layer can be built on top of this business layer and can expose a set of related Methods for example:-

    //the service layer method

    [DataObjectMethod(DataObjectMethodType.Select)]
    public IList<Employee> SearchEmployee(string EmployeeName)
            {      
                  // add the CAS    

                return employeeBusinessObject.Search(EmployeeName);
            }


    // business layer

    public IList<Employee> Search(string EmployeeName)

     {

      //add businesslogic;
       

                return employeeDataLayerObject.Search(EmployeeName);

     }

    hope this makes some sense.

    Tuesday, July 21, 2009 8:35 AM
  • And if the Business Layer and the Data layer are on the same tier? won't dto be somehow of an overhead?

    Thanks. guys!! Really apreciate it!

    Me http://www.zapacila.com
    Tuesday, July 21, 2009 8:41 AM
  • Do you think it`s ok to make a shared DTO ? to use for all layers? business <-> data  service <-> business  presentation <-> service??

    \\or should i define DTO for data layer and business layer  another type of DTO for service with presentation

    \\or use inheritance ?

    Me http://www.zapacila.com
    Tuesday, July 21, 2009 9:22 AM