Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, Winforms & LINQ to SQL.. Datacontext lifecycle?

Tags:

We use an enterprise framework that we wrote to facilitate all sorts of company specific stuff that we do.

Within the framework, we provide a LINQ to SQL ORM to use when appropriate. All of this is based on the Microsoft MVC framework. On the MVC side, we new up a datacontext in our base controller. This allows us a full datacontext lifecycle, which is extremely useful for transactions.

One task that we're looking to accomplish is to provide Winforms support.

However, I'm banging my head against the wall trying to figure out an approach that would work in a similar fashion for Winforms.

Given that the MVC approach is pretty straight forward becase one page load represents a logical transaction, it's difficult to come up with a solution on the Winforms side.

Has anyone done anything similar or have any recommendations?

like image 730
Ian P Avatar asked Jan 19 '09 20:01

Ian P


2 Answers

If you are thinking to choose between having a long-lived DataContext (for example as a Singleton in your app) or having short-lived DataContexts, I would choose the second. I would new() a DataContext for each "Unit Of Work" and make sure to keep it alive for as short a period as possible. Creating a new DataContext is not a big issue, since they cache metadata anyway. Having a long lived DataContext gives you a bit of a nightmare when it starts tracking to many objects.

like image 93
Sergiu Damian Avatar answered Sep 28 '22 06:09

Sergiu Damian


I did something like that for some small softwares we built last year.

We created an application shell that loads the forms similarly to the request/response model.

I built a IRenderer interface with a RenderView() method that I've implemented for web and for windows forms. It allows me to use the same controler and model for both. Search for Model-View-ViewModel (MVVM) on goodle and you may find something about this approach.

I think this article may help you to understand what I'm talking about.

like image 33
Rafael Romão Avatar answered Sep 28 '22 07:09

Rafael Romão