Take the 2-minute tour ×

In Business Logic Layer i do have a process that has 8 steps . And in DAL am using EntiyFrameWork Code first approach. I have created an object of Container that inherits DBContext like(using(var context=new MyContainer)) in every method where i have to do DBoperaions. For performance part i saw it took time when i create its object. Can i make Object of that container once in Bll (at the beginning of process) and pass it to all the methods as parameter and dispose that object at the end of the process after completing all the 8 steps. Any help will be highly appericiated.

share|improve this question
Improve your accept rate –  Eranga May 1 '12 at 4:53
How i can do that? Actually i am new to stackoverflow. –  ethicallogics May 1 '12 at 4:54
See How does accept rate work? –  Eranga May 1 '12 at 4:58
Passing the context sounds like a viable solution. –  Gert Arnold May 1 '12 at 6:26
add comment

1 Answer

up vote 2 down vote accepted

Yes, you should create exactly one ObjectContext per unit of work.

You can either pass the OC to the steps, or just the parts needed. E.g., if you only need to query MyEntities.Foos for some step, you can do:

public class Step4 
    public Step4(IQueryable<Foo> foos)
        // ...

...and then do:

var step4Result = new Step4(context.Foos).Execute();

This will be easier to test than passing the whole context.

share|improve this answer
add comment

Your Answer


