Lukas Narus
Lukas Narus

Reputation: 103

Wrapping methods using db connection in MVC

I was following the MVC music store tutorial and I came to the part where they are using database connections (DbConnection is a child of DbContext). I was taught to create methods like this (wrapping with using):

public class StoreManagerController : Controller
{
    //
    // GET: /StoreManager/

    public ActionResult Index()
    {
        using(var db = new DbConnection())
        {
            var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
            return View(albums.ToList());
        }
    }

    ...
}

but Visual Studio generated me a controller which looked like this:

public class StoreManagerController : Controller
{
    private DbConnection db = new DbConnection();

    //
    // GET: /StoreManager/

    public ActionResult Index()
    {
        var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
        return View(albums.ToList());
    }

    ...
}

I assume, Visual Studio isn't wrong, but why was I told to wrap each method with using to make the connections as short as possible and also the users to use separate connections?

Upvotes: 2

Views: 725

Answers (2)

Aritra B
Aritra B

Reputation: 1746

I assume, Visual Studio isn't wrong, but why was I told to wrap each method with using

    using(var db = new DbConnection())
    {
        var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
        return View(albums.ToList());
    }

The scope of db remains only within the curly braces. This is perhaps another purpose that using keyword serves in C#. It defines the scope of a variable, here in the above case it is your db object.

Now, if you debug the code that visual studio generated for you, then you notice that there is a Dispose method being invoked each and every time an object of a controller class is made or in other words, an action method is called within the corresponding Controller.

The DBContext instance is always disposed because of the following -

  • As you load more objects and their references into memory, the memory consumption of the context may increase rapidly. This may cause performance issues.

  • If an exception causes the context to be in an unrecoverable state, the whole application may terminate.

  • The chances of running into concurrency-related issues increase as the gap between the time when the data is queried and updated grows.

For more info - Reference

Upvotes: 2

rae1
rae1

Reputation: 6144

This might depend on the usability of your app; whether or not you need a persistent connection, and the cost of creating one (and a myriad of other factors).

But for starters, you should always dispose of the connection (as in the first pattern, not the one suggested by Visual Studio) and then move to other patterns based on new requirements or performance-related issues.

The biggest issue I see with the Visual Studio-suggested options is that you have no way of controlling the lifetime of the DbConncetion object, and are leaving it up to the garbage collector to eventually dispose of it. This could leave the connection resources in use for an rather undetermined period of time.

Upvotes: 1

Related Questions