locked
What is the best practice to pass Session variables to Data Access Layer? RRS feed

  • Question

  • User-2033899369 posted

    We have a three tiered architecture as follows.

    + BusinessLogic
    + DataAccess
    + MainWebSite

    The flow is like this: MainWebSite -> BusinessLogic -> DataAccess

    I want to be able to log user actions such as login, logout etc in the database. One way is to update all methods across the BusinessLogic and DataAccess to send Session parameters from the MainWebSite.

    Can anyone explain with an example how to achieve this with the help of an interface and inject the parameters?

    Thanks.

    Wednesday, October 28, 2015 12:49 PM

Answers

  • User614698185 posted

    Hi Vijay87,

    I suggest do not send a session object to your data layers or other business logic. One of the primary reasons for even separating those things out is so that you can reuse the code in other programs.

    As soon as you add a dependency, like on HttpContext, then you may as well not have those layers and just throw it all together.

    Regarding the list itself, when you need to pass the data to the business / data layers send it as a List.

    Please see: http://stackoverflow.com/questions/6656884/net-how-should-i-share-session-objects-between-different-layers-of-asp-net-proj

    http://stackoverflow.com/questions/2702413/how-to-access-session-values-from-layers-beneath-the-web-application-layer

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 10:39 PM
  • User-821857111 posted

    I agree that it is not good practice to pass session variables to other layers in your application. If you have a group of related values that you want to pass, you can create a class (custom object) that contains properties for the values and pass that in to your method instead:

    public class MyValues
    {  
        public int UserId {get;set;}
        public string Name {get;set;}
    }
    var myClass = new MyClass();
    myClass.UserId = (int)Session["UserId"];
    myClass.Name = Session["Name"].ToString();
    MyBusinessLayer.DoSomething(myClass);

    You business layer will perform validation, logging etc and then pass myClass to the DAL to be saved.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 2, 2015 7:47 AM

All replies

  • User614698185 posted

    Hi Vijay87,

    I suggest do not send a session object to your data layers or other business logic. One of the primary reasons for even separating those things out is so that you can reuse the code in other programs.

    As soon as you add a dependency, like on HttpContext, then you may as well not have those layers and just throw it all together.

    Regarding the list itself, when you need to pass the data to the business / data layers send it as a List.

    Please see: http://stackoverflow.com/questions/6656884/net-how-should-i-share-session-objects-between-different-layers-of-asp-net-proj

    http://stackoverflow.com/questions/2702413/how-to-access-session-values-from-layers-beneath-the-web-application-layer

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 10:39 PM
  • User-821857111 posted

    I agree that it is not good practice to pass session variables to other layers in your application. If you have a group of related values that you want to pass, you can create a class (custom object) that contains properties for the values and pass that in to your method instead:

    public class MyValues
    {  
        public int UserId {get;set;}
        public string Name {get;set;}
    }
    var myClass = new MyClass();
    myClass.UserId = (int)Session["UserId"];
    myClass.Name = Session["Name"].ToString();
    MyBusinessLayer.DoSomething(myClass);

    You business layer will perform validation, logging etc and then pass myClass to the DAL to be saved.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 2, 2015 7:47 AM