Last post Aug 26, 2014 09:36 AM by Xsrk
Aug 26, 2014 08:45 AM|Xsrk|LINK
I'm using EF with my ASP.net Web Application, I have seen a lot of examples related to MVC/API only (covering my scenario) but there is not a single example with ASP.net web forms. I dont know either this one is related
to Architecture related question or DATA with EF related.
Suppose i have only one entity named 'customer' i have written CustomerRepository mention below.
public class CustomerRespository
private readonly EFTestDBEntities CurrentDBContext;
public CustomerRespository(EFTestDBEntities context)
CurrentDBContext = context;
public IEnumerable<Customer> getAllCustomers()
public Customer geCustomer(int customerID)
i can call customer repo from code-behind of any page using code below. How i can dispose DB Context ? i dont want to use Using Statement everywhere in my code-behind.
CustomerRespository custRepo = new CustomerRespository(new EFTestDBEntities());
public void TestMethod()
I want to achieve following objectives
1-I dont want to use (Using statement with DBContext) in my repositories i ll prefer to use DI with constructor injection in each repo. (e.g. CustomerRepository,OrderRepository ect)
2-If my CustomerRepo implements IDisposable then it will enforce me to use (Using Statement) when i ll use repo in code-behind of any web page.
3-HTTP Request should get a new Context each time for each request.
4-Want to allow multiple Repositories to work with one Object Context.
5-Perhaps i have to use Unitofwork but i dont know how i can do that?
Example with code only in ASP.net Web forms is required.
Aug 26, 2014 09:01 AM|AidyF|LINK
Your code doesn't include anything related to either webforms or mvc, both webforms and mvc handle the presentation where EF handles the data. EF code isn't specific to either webforms or mvc so it shouldn't matter what presentation the same code is using.
Anyway. Your main issue is that you have called your class CustomerRepository, but calling something Respository and thinking that makes it one is is no different to thinking that standing in your garage makes you a car :) Your "CustomerRepository" is
actually a query builder. As the content is passed in, you will have to dispose of the context in your calling code, or just don't bother and let the garbage collection do it (bad idea IMO). If you want to use objects that require disposal then I'm afraid
you have to dispose them, it might be annoying, but that's life.
Implementing IDispoable won't force you to use "using" at all, you can still create the object outside of a using context.
Have a google for injecting DBContext using DI and you'll find a few solutions on how the cleanup can be done on a per per request basis so you don't have to handle it in your code. Or you could make your repositories actual repositories that create the
context, do a ToList to return the data, then close the context themselves.
Aug 26, 2014 09:36 AM|Xsrk|LINK
Have a google for injecting DBContext using DI