Scope in Seesion?

Thread is closed for posting
3 posts, 0 answers
  1. Thomas Kaske
    Thomas Kaske avatar
    7 posts
    Member since:
    Nov 2009

    Posted 07 Jan 2010 Link to this post

    Hello everybody,

    i have a question about the scope in web applications. I read the best practices and it says that it is ok to put the scope in the session and clear the session after that. So that means, taht i create the scope on every request.

    So we have about 60 classes which are mapped in our company and it takes around 2,5 seconds to create a scope every time, but when we create the scope on the first time and never ever:-) again and then put it into the session and keep it there it saves time to. We just look on a request in the session and take it out of there.

    What is the best way. Your examples are alwys on some classes with some members but how I mentioned we have around 60 classes with lots of members. I understand that it takes time for the OR Mapper to check the mapping but that must go faster.

    What would you suggest me to put it into the session or create it everytime or maybe i did understand something wrong???
  2. IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 08 Jan 2010 Link to this post

    Hi Thomas,

    If it takes 2,5 seconds to create something is really wrong.

    In webapps you generally follow the pattern of "one thread (request) - one scope"... However, on creation of the very first scope it takes longer because mapping configuration has to be read and so on.
    If you take a look at the provided (with OA) ObjectScopeProvider (I don't know if you create scopes from that one)..but anyway... Take a look at it.

    It has a Database.Get method call which return a Database instance according to your connection info in the config. That's where the mapping configuration and so on is read, but no scope is created yet..

    The scope is created when you do a GetObjectScope() on the retrieved database instance (the one you got from the Database.Get method call)... and this should perform very well...

    Take a look at your code that handles the scope, maybe you're creating the Database instance over and over...  You only need to create the Database instance once...and then create scopes from this instance.

    The number of classes is not relevant on scope creation (it might be on Database.Get method call, but you only do this once)


  3. Zoran
    Zoran avatar
    534 posts

    Posted 08 Jan 2010 Link to this post

    Hello Thomas,

    It is possible to store the scope in session, but I would recommend you to create it, as Henrik suggested on a per-request basis. Scope creation is a very cheap operation and I also assume that the call that takes 2,5 seconds is actually the creation of the database object. Storing the scope in session is not even possible in large web applications where the Session is stored in SQL Server database as it is not a Serializable object.
    You may be already aware of it but I would still point you to the approach that has proven to be most successful and popular with our customers, namely, to store the scope in the HttpContext.Items collection and keep it there for the lifecycle of a web request.

    Best wishes,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top