locked
Context Singleton and Context per Form Approach in Client Applications RRS feed

  • Question

  • Hi,

    I'm an ASP.NET developer in general but have to work with client applications as well (WPF, FinWorms);

    Most of my applications are based on Entity Framework (4.1 now) and I'm used to use a context per request approach in my web applications, i.e something like this:

     

     

     MyContext.ContextProvider += () =>

                {

                    var objectContextKey = HttpContext.Current.GetHashCode().ToString("x");

                    if (!HttpContext.Current.Items.Contains(objectContextKey))

                        HttpContext.Current.Items.Add(objectContextKeynew  MyContext());

                    return HttpContext.Current.Items[objectContextKeyas  MyContext;

                };

     

    Afte what I just write MyContext.Instance anywhere in code and get a shared context singleton per request;

    Now, I'd like to use a context per form approach in client applications and would like to have somewhat similar way of achieving this, i.e. using the same MyContext.Instanse manner;

     

    What would be the best practice of doing it in client applications?

     

    Thank a lot


    Clarity VS Precision
    Friday, October 7, 2011 9:47 AM

Answers

  • Hi Giorgi,

    i use also a custom lifetimemanager for request based contexts. Good idea :D If you want to use one context per form I think the easiest (probably a best practice way) is to bind the dbContext lifetime to the form lifetime. That means, create the context in constructor and call DbContext.Dispose when the dialog is being destroyed.

    Greetings, Holger

    Friday, October 7, 2011 11:39 AM
  • Hi,

    If your UI does not have any references to EntityFramework, You can think of in addition to Holger's implementation, managing Form, probably by means of Guid,  and corresponding ObjectContext in dictionary so that every request you madethrough will take same ObjectContext.

    I hope you also thought about following when you consider ObjectContext per Form

    • Loading too many entities into context might increase the size causes performance issue
    • If you keep too long between retrieving and commiting the data concurrency issues
    • Any error caused that is unrevoverable the application ends in unstable state

    And as you know there is no second-level-cache build in with EntityFramework, you have to load all lookups into same context...

    You can consider how to ? mentioned in http://msdn.microsoft.com/en-us/magazine/hh394143.aspx

    Hope this helps you....


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Friday, October 7, 2011 1:24 PM

All replies

  • Hi Giorgi,

    i use also a custom lifetimemanager for request based contexts. Good idea :D If you want to use one context per form I think the easiest (probably a best practice way) is to bind the dbContext lifetime to the form lifetime. That means, create the context in constructor and call DbContext.Dispose when the dialog is being destroyed.

    Greetings, Holger

    Friday, October 7, 2011 11:39 AM
  • Hi Holger,

    thanks for suggestion, I already do it that way and that Dispose() call annoyance was the reason to start thinking on something "cooler" :)

    let's wait for some others' ideas.

     

    Cheers


    Clarity VS Precision
    Friday, October 7, 2011 12:12 PM
  • Hi Giorgi,

    if you like it cool than build your own LifetimeManager. This manager would need to hold references to all your forms. You could hook on events like destroy or create and inject or dispose a dbcontext this way. This approach would be more loosely coupled than the way described above.

    Have fun ;)

    Holger

    Friday, October 7, 2011 12:54 PM
  • Hi,

    If your UI does not have any references to EntityFramework, You can think of in addition to Holger's implementation, managing Form, probably by means of Guid,  and corresponding ObjectContext in dictionary so that every request you madethrough will take same ObjectContext.

    I hope you also thought about following when you consider ObjectContext per Form

    • Loading too many entities into context might increase the size causes performance issue
    • If you keep too long between retrieving and commiting the data concurrency issues
    • Any error caused that is unrevoverable the application ends in unstable state

    And as you know there is no second-level-cache build in with EntityFramework, you have to load all lookups into same context...

    You can consider how to ? mentioned in http://msdn.microsoft.com/en-us/magazine/hh394143.aspx

    Hope this helps you....


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Friday, October 7, 2011 1:24 PM
  • Or maybe even simpler, I'd just create a base form class with managed context instance bound to its life cycle,

    Thanks


    Clarity VS Precision
    Friday, October 7, 2011 2:26 PM
  • well thats what i meant with my first suggestion.. I didnt mean that you need to do this in all your forms...

    Cheers

    Friday, October 7, 2011 2:50 PM