4

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.

Mikalee
  • 289
  • 3
  • 14
  • Perhaps this is of any help? http://stackoverflow.com/questions/2953624/ioc-and-datacontext-disposing-in-asp-net-mvc-2-application – Oskar Kjellin May 18 '11 at 20:21

1 Answers1

3

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?

Community
  • 1
  • 1
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • thank you. for (2) how would I go about doing that? In a given page load, there are 3 or 4 different controller actions being called (e.g. authentication, menu builder, main content) in different controllers, how could I minimized the DB Connections? – Mikalee May 18 '11 at 21:03
  • I would Open the connection right before the Query and then close the connection right after the Query. It is fine to open and close connections multiple as needed. It is not about minimizing DB calls as much as it is about minimizing the time you have an open connection. – Peter Kelly May 18 '11 at 21:31