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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:27:03+00:00 2026-06-18T22:27:03+00:00

I am implementing the Repository Pattern with Entity Framework 5.0 – at least i

  • 0

I am implementing the Repository Pattern with Entity Framework 5.0 – at least i think i am :). This is what i am using

public abstract class GenericRepository<C, T> :
 IGenericRepository<T>
    where T : class
    where C : DbContext, new()
{
    private bool disposed = false;
    private C _entities = new C();
    protected C Context
    {
        get { return _entities; }
        set { _entities = value; }
    }

    public virtual IQueryable<T> GetAll()
    {
        IQueryable<T> query = _entities.Set<T>();
        return query;
    }

    public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        IQueryable<T> query = _entities.Set<T>().Where(predicate);
        return query;
    }

    public virtual void Add(T entity)
    {
        _entities.Set<T>().Add(entity);
    }

    public virtual void Delete(T entity)
    {
        _entities.Set<T>().Remove(entity);
    }

    public virtual void Edit(T entity)
    {
        _entities.Entry(entity).State = System.Data.EntityState.Modified;
    }

    public virtual bool Save()
    {
        return (_entities.SaveChanges() > 0);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
            if (disposing)
                _entities.Dispose();

        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

Afterwards this class is inherited by specific repository class – let’s say PlaceRepository

public class PlaceRepository : GenericRepository<DbEntities, Place>, IPlaceRepository
{ }

In another layer (business layer) i am creating instances of the PlaceRepository class – in class called PlaceController. This class has specific methods for the PlaceEntity (CRUD). In this PlaceController i have one method that is used for inserting Place entity in the database, but at the same time i am inserting something in another table (let’s say Country table). For CRUD operations over the Country table i have another Repository called CountryRepository.

To sum up, my method in the Place Controller creates instances of two different repositories, to use their methods, thereby creating two different contexts of DbContext. See code below

public class PlaceController 
{ 
   public bool InsertPlace(Place toInsert)
   {
      PlaceRepository _placeRepo = new PlaceRepository();
      _placeRepo.Add(toInsert);

      Country _country = new Country();

      _country.Name = "default";

      CountryRepository _countryRepo = new CountryRepository();

      _countryRepo.Add(_country);

     //now i must call save on bothRepositories
     _countryRepo.Save(); _placeRepo.Save();

}
}

I need an opinion for this scenario. Is it good to create 2 instances of the context class to make two insertions? If it isn’t should i consider using/implementing another pattern?

  • 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-06-18T22:27:05+00:00Added an answer on June 18, 2026 at 10:27 pm

    You should use IoC(DI) containers for that. That will help you to use 1 instance of the context whole a long the project. You can look also to the way of serviceLocator pattern but I would recoment to use IoC(DI) container. I like Castle.Windsor(the realization of DI pattern) also you can look to Ninject.

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

Sidebar

Related Questions

Morshed Anwar's article Implementing Repository Pattern with Entity Framework starts off with this at
I am implementing Repository Pattern with ADO.NET entity framework. I see that updating records
Whether using the Repository pattern with an Interface or just implementing DataAccess methods in
I'm implementing a web application using ASP.NET MVC and the Entity Framework using the
I am using Entity Framework Model-First with Repository and Unit of Work patterns, the
So I'm implementing the repository pattern in an application and came across two issues
I've been looking into implementing caching at Repository level using the .NET 4.0 Caching
implementing publishActivity in PHP using the REST API using this code: $activity = array(
I'm implementing the repository pattern in my ASP.NET MVC application. So i was doing
I am told I need to dispose of instances of my Entity Framework repository

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.