.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
MarieAdela
Imran Ghani
Post New Web Links

ObjectContext Life Management Best practice

Posted By:      Posted Date: September 20, 2010    Points: 0   Category :ASP.Net
 

I am trying to create a generic data repository using Entity Framework 4. I have a class DataRepository<T> in which constructor I want to pass in the ObjectContext to run the operations against.

public class DataRepository<T> : IDataRepository<T> where T : class
    {

        protected ObjectContext _context;
        protected IObjectSet<T> _objectSet;

        public DataRepository(ObjectContext context)
        {
            this._context = context;
            this._objectSet = context.CreateObjectSet<T>();
        }
...
}

The problem is that I can't do like this because the context is disposed after a call to Add or Delete, meaning the second call will fail:

public virtual void Add(T instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");

            using (var context = _context)
            {
                IObjectSet<T> objectSet = context.CreateObjectSet<T>();
                objectSet.AddObject(instance);
                context.SaveChanges();
            } //here the context is disposed and cant be use again if the caller calls for example: 


View Complete Post


More Related Resource Links

Software Development Life Cycle (SDLC)

  
This is also known as Classic Life Cycle Model (or) Linear Sequential Model (or) Waterfall Method. This model has the following activities.

SDLC Approach or Software Development Life Cycle

  
SDLC process stats from conception to completion of any software project. According to this approach the software development team is responsible for the whole project development life cycle. After doing SRS and requirements analysis; developers follow different models of Software development Life Cycle (SDLC) available, while the most common model is SDLC waterfall model. They divide project in different stages & phases and the output of each stage or phase becomes the input for next stage.

System Development Life Cycle

  
The Systems Development Life Cycle (SDLC) is a conceptual model used in project management that describes the stages involved in an information system development project from an initial feasibility study through maintenance of the completed application. Various SDLC methodologies have been developed to guide the processes involved including the waterfall model (the original SDLC method), rapid application development (RAD), joint application development (JAD), the fountain model and the spiral model. Mostly, several models are combined into some sort of hybrid methodology

The Agile System Development Life Cycle (SDLC)

  
The goal of this article is to describe the agile system development life cycle (SDLC), putting it in context from what you may have heard about within the agile community and more importantly within the context of your overall IT efforts.

Systems Development Life Cycle (SDLC)

  
The systems development life cycle (SDLC) is a conceptual model used in project management that describes the stages involved in an information system development project, from an initial feasibility study through maintenance of the completed application. Various SDLC methodologies have been developed to guide the processes involved, including the waterfall model (which was the original SDLC method); rapid application development (RAD); joint application development (JAD); the fountain model; the spiral model; build and fix; and synchronize-and-stabilize.

Usability in Practice: The Decade of Convergence

  

As devices converge, user experience design needs to change, too.

Dr. Charles B. Kreitzberg

MSDN Magazine April 2010


Patterns in Practice: Internal Domain Specific Languages

  

Jeremy Miller explains how internal Domain Specific Languages can help you craft code that is easier to read and write. His bag of tricks to improve your programming includes extension methods, fluent interfaces, object extensions and use of the semantic model.

Jeremy Miller

MSDN Magazine January 2010


Usability in Practice: More Than Skin Deep

  

While style and slick visuals are important in Web site design, they shouldn't detract from a site's usability and functionality. Here are some hands-on tips for look and feel, readability, discovery of affordances, and more, with plenty of examples of good and bad design.

Ambrose Little, Charles B. Kreitzberg

MSDN Magazine December 2009


Usability in Practice: Search Is Key to Findability

  

In this column, Ambrose Little and Charlie Kreitzberg discuss best practices, design patterns, and other considerations related to implementing a search feature.

Ambrose Little, Charles B. Kreitzberg

MSDN Magazine November 2009


Patterns in Practice: Functional Programming for Everyday .NET Developers

  

This article examines how the new support for functional programming techniques in .NET 3.5 can developers make code more declarative, reduce errors in code, and write fewer lines of code for many common tasks.

Jeremy Miller

MSDN Magazine October 2009


Usability in Practice: Getting Inside Your Users' Heads

  

This article explores techniques developers can use to gather information about and incorporate their users' mental models in their software designs.

Ambrose Little, Dr. Charles B. Kreitzberg

MSDN Magazine October 2009


Usability in Practice: The Tao of Screen Design

  

This article describes methods for designing screens in a user interface and the technology frameworks that support screen design.

Ambrose Little, Charles B. Kreitzberg

MSDN Magazine September 2009


Patterns in Practice: Incremental Delivery Through Continuous Design

  

The end goal of software projects is to deliver value to the customer. Software design is a major factor in how successfully a team can deliver that value. The best designs are a product of continuous design rather than the result of an effort that tries to get the entire design right up front. This approach lets you strive to apply lessons learned from the project to continuously improve the design, instead of becoming locked into an erroneous design developed too early in the project.

Jeremy Miller

MSDN Magazine August 2009


Usability in Practice: Usability Testing

  

This month's column describes the benefits and methodologies of usability testing.

Dr. Charles B. Kreitzberg and Ambrose Little

MSDN Magazine July 2009


Usability in Practice: Agile Ux Development

  

This month the authors show you how to treat the user experience as an essential dimension of the development process while retaining the advantages of Agile.

Dr. Charles B. Kreitzberg and Ambrose Little

MSDN Magazine June 2009


Patterns in Practice: The Unit Of Work Pattern And Persistence Ignorance

  

Jeremy Miller continues his discussion of persistence patterns by reviewing the Unit of Work design pattern and examining the issues around persistence ignorance.

Jeremy Miller

MSDN Magazine June 2009


Extreme ASP.NET: The Life And Times of an ASP.NET MVC Controller

  

Here the author dissects the ASP.NET MVC framework and looks at how controllers work. He then explains how the framework interacts with your controllers and how you can influence those interactions.

Scott Allen

MSDN Magazine May 2009


Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend