SilverlightShow: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern Comments http://www.silverlightshow.net/ Silverlight articles, Silverlight tutorials, Silverlight videos, Silverlight samples SilverlightShow.net http://www.rssboard.org/rss-specification Argotic Syndication Framework 2008.0.2.0, http://www.codeplex.com/Argotic en-US estoychev@completit.com (Emil Stoychev) RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Tim,</p> <p>The point of using ObservableCollection is that it supports INotifyCollectionChanged to keep the UI up to date when you binid to it and the collection changes behind the scenes. The collections exposed by the DomainContext implement that interface as well, so they are just as suitable for data binding or exposing from your ViewModel as an ObservableCollection. If you want a little more decoupling between your view model and the domain context, you will want to look at the preview of SP2 - they are making it so the underlying collections of the domain context are more detachable and support direct data binding a little better.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment6119 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Thu, 12 May 2011 02:29:21 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>My ViewModels to date have all used ObservableCollections for binding the XAML with.</p> <p>What is the best method of working with RIA Services and ObservableCollections.</p> <p>Should I</p> <p>a) Change the Domain Service to provide ObservableCollections</p> <p>b) Convert the data when loading it in the ViewModel (introduces possible continuity issues)</p> <p> c)Use a different type of collection for data binding</p> <p> </p> <p>Thanks,</p> <p>Tim</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment6116 Tim http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 11 May 2011 17:02:00 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern Thank you code and guildine of  WCF RIA SERVICE. It's good for me and it gives me a lot of insprite. http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment5931 Jason Wang http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Tue, 19 Apr 2011 12:18:37 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Acsed:</p> <p>The Model is rarely an explicit thing in MVVM the way the views and view models are. The model is the combination of the business objects (entities), business rules, data access - I like to say the model is everything else besides the views and view models. But what most people focus on as far as the model is concerned are the business objects or domain model itself, which is represented by the entities in a RIA Services application. The combination of the data they contain along with any validation you embed on them through the validation attribute constitutes most of the client side model.</p> <p>As far as true data access is concerned, that happens on the server side by necessity in any Silverlight application, whether RIA Services or not. View models are all about containing data, providing it to the view, and manipulating that data in response to user interactions. So they have to get that data somehow. The most common pattern is the repository pattern to insulate the view model from how the data gets into the client, which I did not do here because unfortunately the further back in the stack you bury the RIA Services stuff. the less value you get out of it and the purpose of the article is to show how to use RIA Services, not how to put together the most decoupled client architecture possible. In my real world projects, I often will wrap the RIA domain context in a repository to decouple the view model from the mechanism of getting data into the app if there is some potential of swapping it out.</p> <p>But realize that the domain context is a repository itself - it exposes a collection like interface (unfortunately not a true .NET interface that can be mocked, but at least a collection like API). And as I show in a later article, there is at least a means of mocking (albeit complicated) the guts of the domain context, so there is a degree of testability there.</p> <p>Anyway, your question leads to a long design discussion that can't be done here and that really has to be done in the context of a given app to be meaningful in terms of picking the right degree of abstraction.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment5483 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 11 Feb 2011 17:00:41 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern Hi Brian,<br /> I do not see any "Model" (the first 'M' of 'MVVM') in your implementation. All what the Model is supposed to do (for instance the access to the data) is implemented within your View Model, which is not what is supposed to be done when using the MVVM pattern. In your example, there is a very strong coupling between the DAL and the View Model.<br /> Please correct me if I'm wrong.<br /> Thanks for your article http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment5463 acsed http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 09 Feb 2011 19:38:47 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Dear Brian,</p> <p>I already added the IEnumerable<user> userE and assigned it to the op.Entities and it works perfect.</p> <p>Thanks for your reply indeed</p> <p>Waleed</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment5419 Waleed http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Thu, 03 Feb 2011 00:11:44 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Hi Waleed,</p> <p>The EntityQuery<T> that is returned from the DomainContext is always going to be the same. It is an expression tree that is sent to the server for execution that is used to retrieve the entity collection. It is not the entity collection itself.  What you want is a property of type IEnumerable<User> in your view model that you can point to the Users collection on the DomainContext. Then your view will be indirectly binding to the Users collection on the domain context itself, which will be populated when the Load operation completes. You shouldn't have to raise the change event on the property yourself, because the underlying collection does not get overwritten, it just gets populated (.Add calls) and it will raise CollectionChanged events itself so the view should update.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment5417 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Thu, 03 Feb 2011 00:06:00 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Not sure, if you still follow up this post, but anyway, I am a little bit confused as I try to implement the approach introduced here.</p> <p>I have my viewmodel as follows:</p> <div class="reCodeBlock" style="overflow-y: auto;border: #7f9db9 1px solid;"> <div style="background-color: #fff;"><span style="margin-left: 0px !important;"><code style="color: #069; font-weight: bold;">public</code> <code style="color: #000;">EntityQuery<user> Users { </code><code style="color: #069; font-weight: bold;">get</code><code style="color: #000;">; </code><code style="color: #069; font-weight: bold;">set</code><code style="color: #000;">; } </code></span></div> <div style="background-color: #f8f8f8;"><span><code>        </code><span style="margin-left: 24px !important;"><code style="color: #000;">GLDomainContext _context = </code><code style="color: #069; font-weight: bold;">new</code> <code style="color: #000;">GLDomainContext(); </code></span></span></div> <div style="background-color: #fff;"><span><code>         </code><span style="margin-left: 27px !important;"> </span></span></div> <div style="background-color: #f8f8f8;"><span><code>        </code><span style="margin-left: 24px !important;"><code style="color: #069; font-weight: bold;">public</code> <code style="color: #000;">LogonViewModel() </code></span></span></div> <div style="background-color: #fff;"><span><code>        </code><span style="margin-left: 24px !important;"><code style="color: #000;">{ </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>             </code><span style="margin-left: 39px !important;"> </span></span></div> <div style="background-color: #fff;"><span><code>            </code><span style="margin-left: 36px !important;"><code style="color: #000;">Users = _context.GetUsersQuery(); </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>            </code><span style="margin-left: 36px !important;"><code style="color: #000;">LoadOperation<user> loadOp = _context.Load(_context.GetUsersQuery(),  </code></span></span></div> <div style="background-color: #fff;"><span><code>                                                    </code><span style="margin-left: 156px !important;"><code style="color: #069; font-weight: bold;">new</code> <code style="color: #000;">Action<LoadOperation<user>>(OnTasksLoaded), </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>                                                    </code><span style="margin-left: 156px !important;"><code style="color: #069; font-weight: bold;">null</code><code style="color: #000;">); </code></span></span></div> <div style="background-color: #fff;"><span><code>        </code><span style="margin-left: 24px !important;"><code style="color: #000;">} </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>        </code><span style="margin-left: 24px !important;"><code style="color: #069; font-weight: bold;">void</code> <code style="color: #000;">OnTasksLoaded(LoadOperation<user> lo) </code></span></span></div> <div style="background-color: #fff;"><span><code>        </code><span style="margin-left: 24px !important;"><code style="color: #000;">{ </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>            </code><span style="margin-left: 36px !important;"><code style="color: #069; font-weight: bold;">if</code> <code style="color: #000;">(lo.HasError) </code></span></span></div> <div style="background-color: #fff;"><span><code>            </code><span style="margin-left: 36px !important;"><code style="color: #000;">{ </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>                </code><span style="margin-left: 48px !important;"><code style="color: #000;">lo.MarkErrorAsHandled(); </code></span></span></div> <div style="background-color: #fff;"><span><code>                </code><span style="margin-left: 48px !important;"><code style="color: #069; font-weight: bold;">return</code><code style="color: #000;">; </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>            </code><span style="margin-left: 36px !important;"><code style="color: #000;">} </code></span></span></div> <div style="background-color: #fff;"><span><code>            </code><span style="margin-left: 36px !important;"><code style="color: #069; font-weight: bold;">this</code><code style="color: #000;">.RaisePropertyChanged(()=> Users); </code></span></span></div> <div style="background-color: #f8f8f8;"><span><code>        </code><span style="margin-left: 24px !important;"><code style="color: #000;">}</code></span></span></div> </div> <p>I am using PRISM (first time) so my viewmodel is implementing the <span style="font-family: consolas; color: #2b91af; font-size: 13px;">NotificationObject.</span></p> <p><span style="font-family: consolas; color: #2b91af; font-size: 13px;">Why I can't get the users to bind back to the grid itemsSource !!</span></p> <p><span style="font-family: consolas; color: #2b91af; font-size: 13px;">What am I doing wrong here ...</span></p> <p><span style="font-family: consolas; color: #2b91af; font-size: 13px;">Appreciate your feedback</span></p> <p><span style="font-family: consolas; color: #2b91af; font-size: 13px;">Waleed <p> </p> </span></p> <p> </p> <p> </p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment5416 Waleed http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 02 Feb 2011 23:50:34 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Hi Olusola,</p> <p>I have definitely thought of exactly that and would love to - just a matter of finding the time. On each of these topics there is a lot more to say and different scenarios to cover. Just have to find the right life balance of things that pay enough to eat and things that are fun to do but would make me homeless if I spent too much time on them. :)</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4679 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 13 Oct 2010 21:04:23 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Brian,</p> <p>This series is so nice. Have you thought of making the parts of this series into chapters of a book/mini-book on WCF/RIA. Maybe an additional chapter on RIA deployment. You can exploit these digital publishing facilities like Amazon Kindle if you don't have the time to do business with paperback publishers. I'll be your first buyer. There are other WCF/RIA materials but this series is brief and straight to the point.</p> <p>Regards, Olusola</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4677 Olusola Adio http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 13 Oct 2010 16:43:16 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Brian,</p> <p>Thanks for this MVVM-RIA article. This is very concise and well understandable.  Please keep up the good work.</p> <p>Olusola Adio</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4662 Olusola Adio http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 13 Oct 2010 13:03:33 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Brian,</p> <p>Ok, I figured out what it was.  I was using SQL Server 2005 (hopefully we are going to move to 2008 soon) and the ProviderManifestToken was set to 2008 and when I changed it to 2005 all worked.  Gotta love this stuff, never a dull moment!</p> <p>Keith</p> <p> </p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4645 Keith Safford http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Mon, 11 Oct 2010 16:31:44 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Keith,</p> <p>Take a look at the code for Part 7, which has a callback for the load calls on the service. Most likely you don't have the database set up or have a connection string problem. That will result in an exception being thrown to the client, and if unhandled in the client, RIA Services will raise it as an unhandled exception. You should be able to put a breakpoint in the unhandled exception handler in the App class to see the exception. In the later parts I added the callback handler, which is your opportunity to see that there is an error,  do something about it, and then mark it as handled so that it doesn't blow up the app. I'll be talking more about that pattern in Part 8, which I am a bit behind on getting out.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4637 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 08 Oct 2010 22:19:58 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Brian,</p> <p>First off, thanks for the tutorials.  I have been anxiously awaiting the MVVM one and went through the tutorial.  However, when I ran it, I received a VS Just-In-Time Debugger error.  Then, I ran the completed, downloaded code and received the same error.  The error is: <br /> An Unhandled Exception ('Unhandled Error in Silverlight Application Code: 4004<br /> Category: ManagedRuntimeError<br /> Message:<br /> System.ServiceModel.DomainServices.Client.DomainOperationException:</p> <p>but that was it.  I did a search and some on the web who received a 4004 error and more information after the Client.DomainOperationException.  But that was all I had.  Anyone have any ideas on what is going on?  I will do more searching on the web.</p> <p> </p> <p> </p> <p> </p> <p> </p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4636 Keith Safford http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 08 Oct 2010 20:45:34 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Eric,</p> <p>Take a look at the behavior in the code for Part 5 - I have a behavior in there that hooks the TextBox.TextChanged event to simulate the UpdateSourceTrigger.PropertyChanged option that WPF has. You could instead route the call to your view model through a command or method call.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4635 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 08 Oct 2010 19:32:05 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Brian,</p> <p>First off, thanks for the tutorials.  I have been anxiously awaiting the MVVM one and went through the tutorial.  However, when I ran it, I received a VS Just-In-Time Debugger error.  Then, I ran the completed, downloaded code and received the same error.  The error is: <br /> An Unhandled Exception ('Unhandled Error in Silverlight Application Code: 4004<br /> Category: ManagedRuntimeError<br /> Message:<br /> System.ServiceModel.DomainServices.Client.DomainOperationException:</p> <p>but that was it.  I did a search and some on the web who received a 4004 error and more information after the Client.DomainOperationException.  But that was all I had.  Anyone have any ideas on what is going on?  I will do more searching on the web.</p> <p> </p> <p> </p> <p> </p> <p> </p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4634 Keith Safford http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 08 Oct 2010 19:09:54 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern very nice, that's works.but I think the TextChangedEvent is more difficult to deal with.is there a easy way...? http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4630 eric yan http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 08 Oct 2010 16:09:39 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Eric, </p> <p>To handle events from the view in the view model that do not have correponding events, you either need to bind a corresponding property on the control that changes when the event fires to a property in the view model (i.e. SelectedItem property instead of handling the SelectionChanged event) or you need to use behaviors such as Blend InvokeCommandAction or CallMethodAction behaviors to hook the event in the view and call functionality on the view model. </p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4626 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Thu, 07 Oct 2010 18:34:37 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern Brian, thanks, Now it works, but I have another question, how can I capture the SelectionChanged event from the DataGrid? I try to define a  SelectionChangedEventHandler in the ViewModel file, but I can't bind it to  SelectionChanged property of the DataGrid, thank you. http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4621 eric yan http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Thu, 07 Oct 2010 09:31:30 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Hi Eric,</p> <p>Not really a RIA Services question, but in a ViewModel world I would typically bind the SelectedItem property of the DataGrid (or any Selector - i.e. ListBox, ComboBox, etc.) to a property exposed by the view model (i.e. CurrentTask) so that the ViewModel always knows what the selected item is and can act on it.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4615 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 06 Oct 2010 16:56:10 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern Hello, I have a question, when I want to delete a task, how can I know what item in the DataGrid hasing been selected? Let ViewModel to know the View or fire a event to the ViewModel from the View? thanks. http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4614 eric yan http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 06 Oct 2010 16:53:48 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Brian, thanks for looking into it. I decided to take a look myself, in hopes of getting an answer to it, I found that if I make a model of a table with a datetime2 data type, I'd get something like the following in my model.edmx-file:</p> <p> <EntityType Name="test"><br /> <Key><br /> <PropertyRef Name="id" /><br /> </Key><br /> <Property Name="id" Type="int" Nullable="false" /><br /> <Property Name="name" Type="nchar" MaxLength="10" /><br /> <Property Name="dato" Type="datetime" /><br /> <Property Name="dato2" Type="datetime2" /><br /> </EntityType></p> <p>Pretty clear, and also visible through TestModel.Store -> TestDatabase -> Test table -> type property in the model browser. </p> <p>In YOUR project, though, I couldn't find any trace of datetime2 in the model. Made me believe that the schema was interpreted differently by some version attribute or alike.</p> <p>ProviderManifestToken="2005" or "2008". Attribute of the "TaskManagerModel.Store" schema. A google search seemed to confirm it. Funny still, that a definition of datetime in the schema makes the framework pop up with an idea of datetime2.</p> <p>So. In order to have backward compatibility, I have two options: Stick with sql server 2005 or modify the edmx-xml manually after creation. Am I right? I guess an attribute to the properties sheet in VS shows up eventually.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4443 Anders Øyvind http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Sat, 11 Sep 2010 00:07:50 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>PCDiver</p> <p>A general answer to your specific questions about why not to put controls in VMs is that in some cases it might work, but in others not so it is generally a bad practice. And by work, I am specifically referring to working in a unit test environment. It is also just a weakening of the clean separation of concerns. If you let that control in there, are you also going to start letting others? Where do you draw the line? The point is to keep it simple and make it so there are clean definitions of what goes where. </p> <p>An example of where it won't work well. The other day on the Prism team, we were writing some unit test for a behavior. In this specific case, the behavior is something that is intended to go in the view definition, so it is OK to reference controls in there. The specific behavior was to simulate the UpdateSourceTrigger.PropertyChanged option that a binding in WPF has but Silverlight does not. We were trying to write a unit test to verify that the behavior updates the bound property when the Text property changes. It wasn't working. So we tried to write an even simpler test. As far as the object model of the TextBox class is concerned, Text is just a .NET property and there is a corresponding TextChanged property that is supposed to fire when you set the Text. Based on that API, I should be able to write a test that sets the Text property and confirms that the TextChanged event fires. We did that. FAIL. Why? It turns out that in the implementation of the TextBox, which is a control, they don't fire that event unless the control has been RENDERED. It is a control, it assumes it is going to be used in a UI context, and it modifies its behavior based on that.</p> <p>That is why putting controls into a view model is a bad idea in general. Controls have a reasonable presumption that by design, they are for use in a rendered UI context. They may not behave the same if not in that UI context, and so won't unit test consistent with their design.</p> <p>Hope that helps clarify. Just cause it might work for DDS doesn't mean it is a good idea. And because DDS derives from Control, I am not willing to presume that it will work consistently for all of its behavior if it is not in a UI context.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4441 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 10 Sep 2010 18:10:30 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern Hi Anders, I've run into this in classes as well. Let me do a little research and get back to you. It is indeed tied to having created the solution on a SQL 2008 R2 database, but likewise I have had a hard time tracking down exactly where that dependency is defined and how to quickly change it. I'll post another comment once I've solved it. http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4440 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 10 Sep 2010 17:59:55 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Hi.</p> <p>I just downloaded the project that one can start with on this part of the series, added a reference in web.config to an existing sql server 2005, and created the taskmanager database using the script given.</p> <p>Adding a new row and save the changes gave me an error, and checking the SubmitOperation I found that my dbms-version didn't support <strong>datetime2</strong>.</p> <p>So, yes, I did use sql server 2005, and switching to sql server 2008 express r2, it worked as it should. However, from what I see, the startdate is defined as a datetime data type in your entity model. Where is this defined? Can an attribute be set in the domain servrice metadata, that the field is a regular datetime?</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4438 Anders Øyvind http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 10 Sep 2010 17:15:36 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Brian,</p> <p>Thanks for taking the time to answer my question.</p> <p>about 1.  If a control can be used without UI, why is it wrong to use it in a VM?</p> <p>about 2. The domain context can still be accessed via the DDS and so all the collections.  I'm not sure what you mean with the problems of accessing a control on a background thread.  Can this not be done? (limited experience here)</p> <p>about 3.  Can you give an example why a control would not work in a unit test?</p> <p>about 4. I don't see the difference between wrapping a domain context in a repository and doing so for a DDS.</p> <p>I expose my DDS via a property in my VM.  I bind directly to the VM not the DDS. If later on I decide to change my DDS to something else, like a paged collection, I just replace my DDS.  That means no changes to the view, only in the VM, and isn't that what MVVM is about?</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4399 PCDiver http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Sun, 05 Sep 2010 13:03:58 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>PCDiver - </p> <p>A couple reasons for not putting the DomainDataSource in the VM...</p> <p>1) Its a control, and putting a control in a VM is just wrong :)</p> <p>2) As a control, it would have issues with manipulating it on a background thread, whereas you can work with the collections of the domain context in the background.</p> <p>3) Even though it does not do rendering itself, the fact that it is a control means it may not play well in a unit test environment, and testability is again one of the main motivators for doing MVVM</p> <p>4) It makes your VM even more coupled to the fact that you are using WCF RIA Services as a data retrieval and update mechanism, and ideally the VM should be decoupled from how it gets its data, it should just be about what it does with that data to support the view (note - putting the domain context into the VM suffers this same problem, just to a slightly lesser degree because it would be a little easier to wrap the domain context in a repository like interface to decoupled the VM than I think it would be to wrap the DomainDataSource.</p> <p>So while you could do it structurally, I think you are giving up too much by doing so.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4397 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 03 Sep 2010 17:58:10 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern Why don't you make the domaindatasource part of the VM?  This way you keep the paging functionality without having to go the PagedCollectionView road. http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4394 PCDiver http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Fri, 03 Sep 2010 14:34:40 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>@Mohsen</p> <p>Check out PagedCollectionView class</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4365 seonsoo http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Tue, 31 Aug 2010 03:52:14 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Hi, thank you for your brief and useful article. i have a question here..by removing DomainDataSource we actually lose some facilities alike paging... do you have any idea how can i add paging in this approach?</p> <p><br /> </p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4347 Mohsen http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Sat, 28 Aug 2010 13:51:11 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>nice article</p> <p>please keep them posting!</p> <p>especially looking forward to topics on structuring & architecting SL 4 Applications using RIA Services + MVVM that's blendable and testable.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4333 seonsoo http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Thu, 26 Aug 2010 11:21:13 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Hi Venkat, The sample already supports editing. You can edit the tasks in the grid and press the Save Changes button and the changes will be persisted to the back end. Basically the domain context tracks changes to the objects it has retrieved based on property changed events that they fire when you edit them. Then when you call SubmitChanges on the domain context, it sends all the changed entities back to your domain service so it can persist those changes. </p> <p>I'll also be showing adding and displaying child entities in the next article (Part 5), which should be out within a week.</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4214 Brian Noyes http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 11 Aug 2010 14:57:23 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>Please let us know how to edit records. For adding you will add something like<strong>  _Context.Tasks.Add(newTask);.</strong>  </p> <p>Thanks in advance</p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4213 VenkatRaman http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Wed, 11 Aug 2010 13:55:10 GMT RE: WCF RIA Services Part 4 - Integrating with the Model-View-ViewModel Pattern <p>The downloaded code for this article has a couple problems.</p> <span style="font-family: consolas; font-size: 13px;"></span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">In AddTaskView.xaml.cs the OkButton_Click should be named SaveButton_Click.</span> <p>In MainPage.xaml</p> <p>Add Mode=TwoWay to the bindings for the DatePickers:<br /> <span style="font-family: consolas; color: #0000ff; font-size: 13px;">"{</span><span style="font-family: consolas; color: #a31515; font-size: 13px;">Binding</span><span style="font-family: consolas; color: #ff0000; font-size: 13px;"> LowerSearchDate</span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">,</span><span style="font-family: consolas; color: #ff0000; font-size: 13px;"> Mode</span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">=TwoWay}"<br /> <span style="font-family: consolas; color: #0000ff; font-size: 13px;">"{</span><span style="font-family: consolas; color: #a31515; font-size: 13px;">Binding</span><span style="font-family: consolas; color: #ff0000; font-size: 13px;"> UpperSearchDate</span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">,</span><span style="font-family: consolas; color: #ff0000; font-size: 13px;"> Mode</span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">=TwoWay}"</span></span></p> http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx#comment4199 Thomas H. Lanier http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx Mon, 09 Aug 2010 23:46:56 GMT