开发者

Converting WPF / ADO.NET Data Services application to the MVVM pattern - Examples

开发者 https://www.devze.com 2022-12-19 12:36 出处:网络
I 开发者_高级运维have a WPF application which was written in C#. This application hasn\'t been written with any particular design pattern in mind, but as I have learnt .NET I\'ve realised that the MVV

I 开发者_高级运维have a WPF application which was written in C#. This application hasn't been written with any particular design pattern in mind, but as I have learnt .NET I've realised that the MVVM model would be suitable. Thus, I'd like to start converting the code.

This will be the first time I've used MVVM, and whilst I'm willing to get stuck in, I'm finding it difficult to find solid MVVM examples online where an ADO.NET Data Service is the Model and XAML is the View. I'd like to look over some examples before setting off on the process of converting my own app to make sure I have correctly understood what I am doing!

Can anyone recommend a small (but non-trivial) example application with code which uses WPF, ADO.NET Data Services and the MVVM model?


I recommend starting with any example that uses MVVM with WPF, and there are many. The fact is that a clean implementation of MVVM will not have any true data access code in it -- data access should be handled by another, abstracted layer (see MVVM where to put Data Access Layer?).

Work on designing a viewmodel that encapsulates all of the data and interaction that your (already existing) views require. Clean out your codebehind and get your view binding to your viewmodel.

Once you have that going, you can worry about how to get your objects to and from a persistence store, but the actual work of doing so does not belong in the M, V, or VM.

I know that there are tons of examples with data access right in the viewmodel or even the model, but those are meant to be quick illustrations that don't require tangents to address dependency injection, facades, etc.

Find any nontrivial example of MVVM in WPF, and when you get to the part where they deal directly with data access, remind yourself that at that point you'll be using an abstraction of persistence.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号