If I have two distinct methods – one deletes a user and another deletes the user’s address (for argument’s sake, let’s assume they have to be deleted and cannot be set to inactive). If each method creates a repository of that type in order to delete the record, is it considered good practice to create an ObjectContext in the first method and then pass it to the other? If so, should it be passed by value, or reference? What issues or problems can arise from doing this?
Share
Yes it is a valid approach. The pattern of sharing one context to act on several repositories to accomplish a specific use case is referred to as the Unit of Work pattern.
http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx
http://joel.net/repository-and-unit-of-work-for-entity-framework-ef4-for-unit-testing
Note that you must share a context if you require transactional integrity. If you used separate contexts, and your second delete failed, you would have no way to roll back the first delete.