The DataContext is quite lightweight and designed to be used in a production application when you use it. However, I don’t think I saved the DataContext in my object. You might want to take a look at the repository templates if you are not going to use the code created by the designer to manage your business objects. The repository template will allow you to work with your objects removed from the data context, and then reconnect them before performing updates, etc.
Personally, I can mostly live with the created DBML code developer, with partial class implementations for my business logic and validation. I also create an abstract abstract data construct and inherit it to allow me to intercept things like stored procedure methods and table functions, which are added directly to the data context and apply business logic there.
The sample I used in ASP.NET MVC is to introduce a factory class that creates the appropriate data contexts needed for units of work. Using a factory allows me to easily outline a data context using (1) a wrapper around an existing data context class to mock (mock the wrapper because the DataContext is not easy to mock) and (2) create a Fake / Layout of contexts and factories to create them. Being able to create them at will with the factory makes it so that I don’t need to maintain it for long periods of time.
tvanfosson Dec 23 '08 at 19:31 2008-12-23 19:31
source share