我一直在查看Nerd Dinner代码,他们在模型中做的一件事是创建 DataContext 的实例,如下所示:
public class DinnerRepository {
private NerdDinnerDataContext db = new NerdDinnerDataContext();
public IQueryable<Dinner> FindUpcomingDinners() {
return from dinner in db.Dinners
where dinner.EventDate > DateTime.Now
orderby dinner.EventDate
select dinner;
}
// more methods below
}
这些在控制器中使用,如下所示:
public class DinnersController : Controller {
DinnerRepository dinnerRepository = new DinnerRepository();
public ActionResult Index() {
var dinners = dinnerRepository.FindUpcomingDinners().ToList();
return View("Index", dinners);
}
}
但似乎 NerdDinnerDataContext 从未被处置。这是我应该担心的问题吗?或者这个模式可以吗?
注意:不是最新的书呆子晚餐代码,我知道