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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:54:11+00:00 2026-05-13T21:54:11+00:00

Suppose my repository class looks like this: class myRepository : IDisposable{ private DataContext _context;

  • 0

Suppose my repository class looks like this:

class myRepository : IDisposable{
    private DataContext _context;
    public myRepository(DataContext context){
        _context = context;
    }
    public void Dispose(){ 
        // to do: implement dispose of DataContext
    }
}

now, I am using Unity to control the lifetime of my repository & the data context & configured the lifetimes as:
DataContext – singleton
myRepository – create a new instance each time

Does this mean that I should not be implementing the IDisposable on the repository to clean up the DataContext?

Any guidance on such items?

EDIT:
DataContext – singleton – read this as singleton per-web request

  • 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-13T21:54:11+00:00Added an answer on May 13, 2026 at 9:54 pm

    As a general rule, abstract dependencies should not derive from IDisposable, because it would be a Leaky Abstraction. A dependency may or may not hold unmanaged resources dependending on concrete implementation. In any case, the container should manage lifetime, so it’s not up to the consumer to do so – it has no knowledge of the lifetime of the dependency: it could be shared with other consumers, in which case it would be destructive to prematurely dispose of it.

    That said, a (LINQ to SQL?) DataContext represents a different problem because it already implements IDisposable, and you can’t very well change this because it’s defined in the BCL.

    You can either properly implement IDisposable for your repository, but that means that you will have to match lifetime for all repositories and the datacontext.

    The other alternative is to simply ignore that you are holding on to a disposable resource, but if you do that, you will have to make absolutely sure that Unity properly disposes of the DataContext at the appropriate time – but since you plan on using the Singleton lifetime, this shouldn’t be a problem.

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

Sidebar

Related Questions

Suppose a mapping like this one @Entity public class User { private Integer id
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
So, I have a little problem here. Suppose I have: public class Repository<TEntity> where
Suppose i have the following class. public class Location { public Id { get;
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
My application I have an application design that looks like this: web application layer
Suppose I have a set of commits in a repository folder... 123 (250 new
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I git clone a repository. Then edit a file A and delete a

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.