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

  • Home
  • SEARCH
  • 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 5846753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:37:17+00:00 2026-05-22T12:37:17+00:00

First some context: I have an MVC3 .net project which, for the sake of

  • 0

First some context: I have an MVC3 .net project which, for the sake of brevity, is set up something like the following:

Controller
– Instantiates a service object (described below)
– Uses service to retrieve db record from mongo (e.g. _service.GetPerson(id))
– Passes domain to view

Service
– Instantiates MongoRepository (described below)
– Calls method to retrieve db record (e.g. _mongoRepository.Single(c => c.Id == id))

MongoRepository : IDisposable
– Constructor (_server = Mongo.Create(ConfigurationManager.ConnectionStrings[“mongodb”].ConnectionString))
– Single method (below)
– Dispose method

public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
        {
            return _server.GetCollection<T>().AsQueryable()
                        .Where(expression).SingleOrDefault();
        }

Now, this is my question, as you can see MongoRepository implements the IDisposable interface, and I believe the best way to ensure Dispose is called is with the ‘using’ block, but

1) when/where should that be? Should it be in the Service layer, inside the method calling the mongoRepository.Single method?

2) when should the MongoRepository be instantiated?

If more code is necessary to answer please let me know, I was trying to keep it short. Thanks in advance.

  • 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-22T12:37:18+00:00Added an answer on May 22, 2026 at 12:37 pm

    1) It sounds like you are instantiating and using the Repository in the Service class. So instead of

    var _mongoRepository = new MongoRepository(..);
    _mongoRepository.Single(...);
    

    you would have

    using (var _mongoRepository = new MongoRepository(..))
    {
        _mongoRepository.Single(..);
    }
    

    If you are wrapping Mongo calls in the MongoRepository class then make sure that the Dispose method properly cleans up the _server connection (close/disconnect/whatever). It looks like you are creating the connection in the constructor, using it in the method but never closing it.

    2) From my experience it is preferable to open a DB connection as late as possible and close it as early as possible – rather than opening it and using it several times for different queries.

    There’s a good discussion on MongoDB (C#) connections at .NET best practices for MongoDB connections?

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

Sidebar

Related Questions

No related questions found

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.