将db连接卸载到helper类,我应该在每个方法中使用db.dispose吗?

时间:2015-10-22 21:26:12

标签: asp.net-mvc entity-framework-6

我正在开发一个变得非常复杂的仪表板。我正在使用带有EF 6的Asp.Net MVC 5。

我的一般架构是;

控制器实例化一个ViewModel,我使用Helper类中的对象填充该ViewModel以传递给视图。帮助程序类包含数据库连接和我公开的方法,其中包含我的linq查询。我这样设置它以保持控制器的轻量级,因为当它完成所有的时候,我将有大约20个对象,我将通过ViewModel传递给视图。我不确定这些日子最适合的模式,但我喜欢这种方法,因为它有助于让我保持井井有条。 (随意提供建设性的反馈)

我的问题是;因为我不是从控制器访问数据库,我应该在Helper类中执行每个方法后调用db.dispose()吗?

提前致谢!

这是一个样本;

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);            
    }
}

助手类

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
}

1 个答案:

答案 0 :(得分:1)

如果你没有明确处理自己,你应该没事,但建议使用using(){}以至少隐含处置。

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;
}

有关详情,请点击此处:http://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext.html#.U6WdzrGEeTw