How to properly access Database Context within Class when using HttpModule

2 posts, 0 answers
  1. AJ
    AJ avatar
    33 posts
    Member since:
    Jun 2011

    Posted 02 Jun 2012 Link to this post

    As my previous post a while ago mentioned, I am converting from SubSonic and have thus far been happy with the decision. However, I'm running into an issue and really need some assistance. I'm using the Custom HttpModule as demonstrated in your docs.

    I have a class called AMSession which is basically like a session helper. One method is called CurrentOrder which basically checks the session for an OrderID and, if one exists, it returns the Order object from the database. This way, I don't have to create a new Order object every time I need to retrieve/set properties. 

    Here's that bit of code:
    public static Order CurrentOrder
    {
        get
        {
            if (Session[Keys.CurrentOrder] == null)
            {
                try
                {
                    var ctx = Modules.OpenAccessContextModule.GetContext();
                    Session[Keys.CurrentOrder] = ctx.Orders.First(o => o.OrderID == CurrentOrderID);
                }
                catch (Exception ex)
                {
                    throw new CustomExceptions.InvalidOrder(ex);
                }
            }
            return (Order)Session[Keys.CurrentOrder];
        }
    }

    Now, I have a page called ViewCart.aspx. When the cart ListView is binded, I want to recalculate the SubTotal, Shipping, and GrandTotal. For that, I have this bit of code:

    AMSession.CurrentOrder.Subtotal = AMSession.CurrentOrder.OrderProducts.Sum(op => (op.UnitPrice * op.Quantity));
    AMSession.CurrentOrder.Shipping = OrderHelper.CalculateOrderShippingTotal(AMSession.CurrentOrder.OrderProducts);
    AMSession.CurrentOrder.GrandTotal = AMSession.CurrentOrder.Subtotal + AMSession.CurrentOrder.Shipping;
    _ctx.SaveChanges();

    However, this code doesn't actually save the order.

    When I change the code to replace all the AMSession.CurrentOrder with an Order object created on that page, it saves just fine.

    var CartOrder = _ctx.Orders.First(o => o.OrderID == AMSession.CurrentOrderID);
    CartOrder.Subtotal = CartOrder.OrderProducts.Sum(op => (op.UnitPrice * op.Quantity));
    CartOrder.Shipping = OrderHelper.CalculateOrderShippingTotal(CartOrder.OrderProducts);
    CartOrder.GrandTotal = CartOrder.Subtotal + CartOrder.Shipping;
    _ctx.SaveChanges();

    I'm open to suggestions on how I can solve this issue. If it means that I simply can't use AMSession.CurrentOrder to save information, then that's just something I'll need to do. However, I just didn't want to change my entire application if there's a better way to handle this scenario as I do something very similar in another project.

    I sincerely appreciate your time and energy on this. 

    Thanks,
    Andrew
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 06 Jun 2012 Link to this post

    Hi Andrew,

    Storing the Order instance in the session state could lead to wrong behavior if there are multiple requests to a page or custom handlers which are using the same session. This could lead to a situation when one request tries to access this instance. Problems could arise when the application is being used in a web farm. You could store the Order instance in the HttpContext or use the approach which you tried on your side by replacing the AMSession.CurrentOrder with an Order object.

    Hope that helps. If any other questions arise, do not hesitate to contact us back.

    All the best,

    Damyan Bogoev
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
Back to Top