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 7458923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:49:52+00:00 2026-05-29T13:49:52+00:00

I have a n-tier application based on pretty classic different layers: User Interface, Services

  • 0

I have a n-tier application based on pretty classic different layers: User Interface, Services (WCF), Business Logic and Data Access.

Database (Sql Server) is obviously quered throught Entity Framework, the problem is basically that every call starts from user interface and go throught all the layers, but doing that I need to create a new ObjectContext each time for every operation and that makes performance very bad because every time I need to reload metadata and recompile the query.

The most suggested pattern it would be the one below and it is what I’m actually doing: creating and passing the new context throught business layer methods each time the service receives a call

 public BusinessObject GetQuery(){     
   using (MyObjectContext context = new MyObjectContext()){ 
      //..do something  }    }

For easy query I don’t see any particular dealy and it works fine but for complex and heavy query it makes a 2 seconds query to keep going for like 15 seconds each call.

I could set the ObjectContext static and it would solve the performance issue but it appears to be not suggested by anyone, also because I won’t be able to access the context at the same time from different thread and multiple calls raise an exception. I could make it thread-safe but mantain the same ObjectContext for long time makes it bigger and bigger (and slower) because the reference it imports each query it execute a query.

The architecture I have I think it is the most common so what is the best and known way to implement and use ObjectContext?

Thank you,
Marco

  • 1 1 Answer
  • 0 Views
  • 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-29T13:49:55+00:00Added an answer on May 29, 2026 at 1:49 pm

    In a Web context, it’s best to use a stateless approach and create an ObjectContext for each request.

    The cost of ObjectContext construction are minimal. The metadata is loaded from a global cache so only the first call will have to load it.

    Static is definitely not a good idea. The ObjectContext is not thread save and this will lead to problems when using it in a WCF service with multiple calls. Making it thread save will result in less performance and it can cause subtle errors when reusing it in multiple requests.

    Check this info: How to decide on a lifetime for your ObjectContext

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

Sidebar

Related Questions

We have a 3 tier application - winform based thick client, several WCF services
How do you pass data to layers in an n-tier application? I have mapped
I have a 3-tier application with Data access, service and presentation tiers. Where should
I have an application based on 3 tier architecture with postgres database(npgsql) having more
I have a superclass Entity in a logic-tier project within existing 3-tier core application
I have an n-tier application with the different tiers running on different machines. I
I have a multi-tier application. DAL -> BAL -> Business Gateway ->UI. If a
I have a three-tier Windows-based web application bundled into 3 AMIs on Amazon EC2
I have a 3-tier application hosted in Tomcat; web, service and DAO layers. How
I have a three-tier application which is installed in corporate environments. With every server

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.