Define a global ObjectScope

4 posts, 0 answers
  1. Hessner
    Hessner avatar
    189 posts
    Member since:
    Mar 2003

    Posted 27 Jan 2009 Link to this post

    I define an objectscope in the BeginRequest event, like this:

      app.Context.Items["ObjectScopeProvider"] = Database.Get("MyDatabaseConnection").GetObjectScope();

     

    And use it anywhere I want during the current request, like this:

     
      IObjectScope osp = (IObjectScope) Context.Items["ObjectScopeProvider"];

    I do not use any cleanup code - at all.

    1. Maybe I should do some "cleaning" up?
    2. Do you see any "danger" in this super simple setup?

  2. Dimitar Kapitanov
    Admin
    Dimitar Kapitanov avatar
    632 posts

    Posted 28 Jan 2009 Link to this post

    Hello Hessner,
    Yes, handling the IObjectScope should be done properly as it must be disposed in some way in the end of the request/page life-cycle. You can take a look at Simplifying OpenAccess Scope in ASP.NET with HttpModule - one of the popular techniques for handling IObjectScope in web applications. Hope this helps.

    Regards,
    Dimitar Kapitanov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Hessner
    Hessner avatar
    189 posts
    Member since:
    Mar 2003

    Posted 28 Jan 2009 Link to this post

    I could easy dispose it in the EndReqest event(app.Context.Items.Remove("ObjectScopeProvider")), but if the following are true:

    "Context.Items is available for all components (including the page) through the entire current request response lifecycle"

    then why should I have to,- the Context.Items are automaticly "flushed" when the request end, as I understand it?

    Are you suggesting that I execute this code in EndRequest event:
     IObjectScope osp = (IObjectScope) Context.Items["ObjectScopeProvider"];
     osp.Dispose();
     app.Context.Items.Remove("ObjectScopeProvider");

  5. Dimitar Kapitanov
    Admin
    Dimitar Kapitanov avatar
    632 posts

    Posted 29 Jan 2009 Link to this post

    Hello Hessner,
    It is better to do so, as I was unable to verify whether the Items contents are to be disposed in the proper manner. Regarding the code my suggestion is to first remove, then dispose the scope instance, just in case.

    Kind regards,
    Dimitar Kapitanov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top