JReam
JReam

Reputation: 898

Offloading db connection to helper class, should I be using db.dispose in each method?

I'm developing a dashboard right now that is becoming quite complex. I'm using Asp.Net MVC 5 with EF 6.

My general architecture is;

The controller instantiates a ViewModel which I populate with objects from my Helper class to pass to the view. The helper class houses the db connection and methods that I expose which house my linq queries. I set it up this way to keep the controllers light weight because when its all said and done, I'll have around 20 objects I'll be handing off to the view via the ViewModel. I'm not sure what pattern this most aligns to these days but I like this approach because it helps keep me organized. (feel free to provide constructive feedback)

My question is; because I'm not accessing the database from the controller, should I be calling db.dispose() after each method executes in the Helper Class?

Thanks in advance!

Here's a sample;

CONTROLLER

 public class DashboardController : Controller
{
    Metrics metrics = new Metrics();
    MainDashboardViewModel vm = new MainDashboardViewModel();

    // GET: Dashboard
    public ActionResult Index(string region, string businessUnit, DateTime? startDate, DateTime? endDate)
    {
        ViewBag.Dash = "active";

        vm.Region = region;
        vm.BusinessUnit = businessUnit;
        vm.StartDate = startDate;
        vm.EndDate = endDate;

        //get average seat price
        vm.AvgSeatPrice = metrics.GetAvgSeatPrice(vm.BusinessUnit, vm.Region, vm.StartDate, vm.EndDate);

        //...lots more removed for brevity

        return View(vm);            
    }
}

Helper Class

public class Metrics
{
    private ApplicationDbContext db = new ApplicationDbContext();

       //AVERAGE SEAT PRICE
    public string GetAvgSeatPrice(string bu, string region, DateTime? startDate, DateTime? endDate)
    {

    var averageSeatPrice = (from r in db.Registrations
                            where
                                 (bu == "All" || r.BusinessUnit.Equals(bu)) &&
                                 (region == "All" || r.Region.Equals(region)) &&
                                 (startDate == null || r.StartDate >= startDate) &&
                                 (endDate == null || r.EndDate <= endDate) &&
                                 r.ActualPrice > 0 &&
                                 !r.Status.Equals("cancelled")

                            select r.ActualPrice).Average();

    var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

    return AvgSeatPrice;
   }

 //...lots more metrics methods removed for brevity
}

Upvotes: 0

Views: 118

Answers (1)

lbrahim
lbrahim

Reputation: 3810

You should be fine if you are not disposing yourself explicitly but it is recommended to use using(){} in order to have at least implicit disposition.

public string GetAvgSeatPrice(string bu, string region, DateTime? startDate, DateTime? endDate)
{
    double averageSeatPrice = 0;

    using(var db = new ApplicationDbContext())
    {
        averageSeatPrice = (from r in db.Registrations
                                where
                                     (bu == "All" || r.BusinessUnit.Equals(bu)) &&
                                     (region == "All" || r.Region.Equals(region)) &&
                                     (startDate == null || r.StartDate >= startDate) &&
                                     (endDate == null || r.EndDate <= endDate) &&
                                     r.ActualPrice > 0 &&
                                     !r.Status.Equals("cancelled")

                                select r.ActualPrice).Average();
    }

    var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

    return AvgSeatPrice;
}

For more you can go through this: http://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext.html#.U6WdzrGEeTw

Upvotes: 1

Related Questions