In a Rob Conery-style ASP.NET MVC application, you typically have a repository:
public class CustomerRepository
{
DataContext dc = new DataContext();
public IQueryable<Customer> AllCustomers()
{
return db.Customers;
}
public Customer GetCustomer(int customerID)
{
return db.Customers.FirstOrDefault(c => c.CustomerID = customerID);
}
}
And a Controller:
public class CustomerController: Controller
{
CustomerRepository _repository;
public ActionResult Index()
{
var data = _repository.AllCustomers();
return view("Index", data);
}
public ActionResult Details(int id)
{
var data = _repository.GetCustomer(id);
if (data !=null)
return view("Details", data);
else
return view("NotFound");
}
}
The controller is instantiated throught a Controller factory in the ASP.NET MVC core engine, when a request is routed to it through the routing engine. It then executes the appropriate method on the controller.
Assuming that I want to implement IDisposable
in the DataContext, how does one properly Dispose
the DataContext, without having to re-instantiate the DataContext for every method in the repository?