Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:52:48+00:00 2026-05-16T00:52:48+00:00

Ive just started using EF4 with the repository pattern. Im having to call the

  • 0

Ive just started using EF4 with the repository pattern. Im having to call the dispose method after every use of context or wrap code arround in the using block. Can I use the ObjectContext without doing this in every method I write or is there a better way of handling this in the repository.
Also I dont want to pass the ObjectContext to the repository from the UI as well.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:52:49+00:00Added an answer on May 16, 2026 at 12:52 am

    The general pattern for using an object context is:

    public BusinessObject GetSomething(){
       using (MyObjectContext context = new MyObjectContext()){
          //..do fun stuff
       }
    
    }
    

    Which hopefully is the pattern you are using. Calling dispose seems a little overkill when you can just use a “using” statement.

    Another option is if you are going to be doing multiple DB queries in a flow. I have seen a pattern where you can reuse the same context within the thread. People basically implement a Thread based singleton pattern and pass around the context. Advantages to this is not having to rebuild the context, as well as some in memory caching. Downside is you could run into concurrency issues. Someone updating something that you have cached internally in EF.

    I am guessing the second case doesn’t really apply because it sounds like you are writting a small app. (that statement was based on your comments about passing a context from UI…a statement which will scare any good code architect).

    If you are interested in a thread based singleton. First learn about the Singleton pattern, and then check out this blog about “DataContext” threads. You will have to change the “DataContext” type to the ObjectContext class but it would work.

    EDIT

    I will say I overlooked an obvious solution and that would be the below ;). Just using a property based Object Context and playing your repository in a using statement. It would be the same as the using example above, but you would implement IDisoposable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive just started using WCF with silverlight and its working fine. But one thing
Ive just started using mutagen and have succefully used it with m4a , mp3
I've just started using google charts and want to use it in a small
Could someone help me understand whats going on here, Ive just started using Jquery
I've just started using Dapper for a project, having mostly used ORMs like NHibernate
I've just started using hibernate 3.6.0 and JPA 2.0 and I'm having some troubles.
I've just started using the MVP pattern in the large ASP.NET application that I'm
I've just started using Java's enums in my own projects (I have to use
I just recently started using MeioUpload 2.0 with cakephp 1.3. Ive configured the behaviour
I've just started using cakephp elements and they're awesome (I used to use include).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.