This is a migrated thread and some comments may be shown as answers.
Web Development Best Practice
1 Answer 46 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rui
Top achievements
Rank 1
Rui asked on 03 Apr 2012, 11:13 PM
Hello I was looking at your samples in the SDK.
In bestpracticewebdevelopmenttwo you have:
public class ContextFactory
    {
        public static NorthwindContext GetContextPerRequest()
        {
            HttpContext httpContext = HttpContext.Current;
            if (httpContext == null)
            {
                return new NorthwindContext();
            }
            else
            {
                int contextId = Thread.CurrentContext.ContextID;
                int hashCode = httpContext.GetHashCode();
                string key = string.Concat(hashCode, contextId);
 
                NorthwindContext context = httpContext.Items[key] as NorthwindContext;
                if (context == null)
                {
                    context = new NorthwindContext();
                    httpContext.Items[key] = context;
                }
                 
                return context;
            }
        }
    }

Using this method how will the NorthwindContext be disposed?



1 Answer, 1 is accepted

Sort by
0
Ivailo
Telerik team
answered on 09 Apr 2012, 12:03 PM
Hello Rui,

You have probably seen the old version of this example, available with Q3 2011 of OpenAccess SDK.

In the latest release of the SDK (available on our web site for free) you can check the refactored and renamed sample now called "Managing OpenAccess Context" in the ASP.NET category. There, you will find the following way of disposing the OpenAccessContext in case you have chosen to store it in the HttpContext:

public class Global : HttpApplication
{
    protected void Application_EndRequest(object sender, EventArgs e)
    {
        NorthwindContext context = ContextFactory.GetContextPerRequest();
        if (context != null)
        {
            context.Dispose();
        }
    }
}

I hope that helps. Let me know if you have any further questions. 

All the best,
Ivailo
the Telerik team
Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
Tags
General Discussions
Asked by
Rui
Top achievements
Rank 1
Answers by
Ivailo
Telerik team
Share this question
or