views:

1484

answers:

5

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?

+2  A: 

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.

Rafael Romão
Thank you very much for the info!
Ian P
A: 

Anyone have any additional insight on this issue?

Ian P
Perhaps you can give us more details about exactly what you are looking for.
Rafael Romão
+3  A: 

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.

Sergiu Damian
+1 unit of work is the key phrase here. a desktop application is still performing distinct business transactions that can and should be divided in to separate units of work. these should control the lifecycle of the datacontext
Hamish Smith
A: 

The problem with only having one datacontext for everything is that you can not have multiple open edits and only commit one. For a lot of application models, this is a non-starter. Therefore, I have one singleton datacontext for reads and create one for each commit action. The update function detaches the objects to be saved from the read datacontext and attaches them to a new commit datacontext, then runs DC.Submit changes.

The only tricky thing is having a way to keep track of if an object should be updated on submit or inserted on submit, and if you have a standard auto num primary key or manage in a collection somewhere what column should serve as the check for which class for insert versus update, this is quite trivial to overcome.

tom.dietrich
Downvoted for keeping a long-term datacontext open for reads. It's a good way to collect stale data. Yes, you can refresh, but then you lose any performance gain from holding the data. Using contexts only when you need to get or put data also simplifies the record keeping, and there's not so much need to worry about whether an object is to be inserted or updated. (If you have an identity column, you can check its content, anyway.... there's nothing there if it hasn't been written.)
Cylon Cat
A: 

I know this thread is a bit old, but in unhaddins we have an implementation of Conversation-per-BusinessTransaction. Currently we have only an implementation for nhibernate, but implementing it for Linq to sql or entity framework should be straightforward. Check my answer in this list.

José F. Romaniello