TechEd South Africa Slides and Code - .NET RIA Services and ViewModel

Slides and code from my ViewModel and .NET RIA Services talks at TechEd South Africa...

This is a quick blog post to share slides and demos from my presentations at TechEd in South Africa. Feel free to post questions here...

From the talk on ViewModel and Application Patterns for Silverlight...
TwitterBugThe app I built was a mini-Twitter client named TwitterBug. The demo covered the following:

  • The ViewModel pattern using data-binding and commanding
  • Behaviors as a mechanism to encapsulate repetitive view functionality
  • Implementing an IoC container and dependency injection using MEF
  • EventAggregator pattern to facilitate view model to view model communication
  • Designer/developer workflow
  • Unit testing of view models
  • Using .NET RIA Services and building a DomainService that uses LINQ to Twitter as its DAL

.NET RIA Services…
For this session I built a basic library management front-end. The code demonstrates a simple CRUD app for managing a list of books:

  • Unified client and server as part of a single application
  • LINQ as a cross-tier query language and mechanism
  • A metadata pipeline from the database to the middle-tier to the client
  • Validation and authorization
  • Shared code
  • Using POCO/non-DAL types in conjunction with Linq to SQL DAL types
  • Imperative use of client framework, as well as declarative use of DomainDataSource
  • Use of the DataForm, and customizing it for read-only and editable views

Additionally I also demo’d using the DomainService/DomainContext pattern in a broader variety of application contexts:

  • Using Azure storage as the DAL
  • Creating jQuery/Ajax and ASP.NET MVC-based front-ends/presentation layers
  • Creating an ADO.NET Data Service (aka Astoria service) based on a domain service
  • Implementing a proof-of-concept offline data model for use in Silverlight Out-of-Browser applications

Most of these scenarios were described at greater length in my MIX blog post. I’ll go over more details on various topics with individual blog posts when I am back in Redmond.

Posted on Tuesday, 8/4/2009 @ 3:02 PM | #Silverlight


Comments

21 comments have been posted.

Raghuraman

Posted on 8/4/2009 @ 6:30 PM
Nikhil, Thanks a Lot for Sharing these.
Both powerpoint presentations are quite insightful.

Steve Strong

Posted on 8/5/2009 @ 2:34 PM
I would love to see the source code for this application. Is it posted somewhere?

Donald Massyn

Posted on 8/6/2009 @ 1:30 AM
Hi Nikhil, I was lucky enough to attend this session. Just like to say Thanks! , what I learned was invaluable, you managed to demystify the MVVM pattern for me.

I hope we see you again next year in Durban

Anil

Posted on 8/7/2009 @ 10:56 PM
Hi Nikhil:

Are there any plans to evolve domaindatasource( or create an adapter that encapsulates domaincontext) into something that can be implemented as "SearchViewModel" easily (with common functionalities such as datapaging, CRUD/Query methods on a given entity and commands that respond to bindable xaml elements???

This MVVM pattern seems cumbersome to implement. Essentially recreating all the functionality that is already provided in the code behind.

Any thoughts in your spare time would be much appreciated.

Thanks,
Anil

Abinash Bishoyi

Posted on 8/9/2009 @ 6:43 PM
Thanks Sir, I am new to .NET and it help a lot in understanding the MVVM pattern.

shailesh

Posted on 8/9/2009 @ 11:15 PM
Thanks foe the sharing power point file. It' will help full to me to learn MVVM

Nikhil Kothari

Posted on 8/12/2009 @ 8:24 AM
@Anil
MVVM is about refactoring functionality you'd traditionally have put in code-behind, but written in a way that the logic is decoupled from the UI. So its not fundamentally duplicate work. It does require a bit of practice and experience, and once you're familiar with the approach, it will likely feel very natural, and fairly clean, and at the very least far from cumbersome.

You can certainly think of a standard view model against a DomainContext. However that IMO defeats the purpose of a view model - a view model is where you put _your_ custom logic. If a generic view model could be created, you might as well package it as a component, and drag/drop it or instantiate it with one tag in markup - oh, wait, that is called a DataSource! :-)

Zeeshan Umar

Posted on 8/12/2009 @ 9:21 AM
Thanks for sharing such a nice article. Good job.

Zeeshan Umar

Anil

Posted on 8/12/2009 @ 12:04 PM
Nikhil,

"If a generic view model could be created, you might as well package it as a component, and drag/drop it or instantiate it with one tag in markup - oh, wait, that is called a DataSource! :-) "

--LoL!! That was a good one.

I am still trying to get a grip around MVVM. So please bear with me. The way I understand it, it does seem like a bit of work to hook custom commands, events, triggers, actions and behaviors to be wired up to the view model code. I wasn't clear with my question. I wasn't talking about the duplicate work in terms of "functionality" that I would write in the code-behind to respond to some interesting UI events. I am talking about duplicating the mechanism that triggers my code-behind. Take any interesting events on my page (or user control) that can be wired up to code-behind methods. It's a piece of cake. In order to implement MVVM, I have to resort to some framework to provide the bridge between my view and viewmodel. [viz., SilverlightFX, SLExtensions, Prism [I am leaning towards SilverlightFX by the way -;) goes beyond command implementation].


Decoupling code-behind logic into a separate view model is truly amazing. I can see all the benefits and understand the true intention behind it. But as it appears for now, I am still thinking...

How much code in the code-behind is complex enough to justify a separate viewmodel? A simple child window that displays some custom message with some colors/glitz has to be decoupled to a viewmodel?

Is it a rule that there has to be 1:1 relationship between viewmodel and the view? What if my view is loaded with half-a-dozen controls that are delegated to perform some real subtasks. Could they all share the same viewmodel? (I see "$model" and "$datacontext" bits come as rescue though!)

Can I still have the code-behind respond to UI events (default implementation, simple buttonclick) but call upon viewmodel for the functionality that it should respond with?

Thanks for your time and reponse.

--Anil

Nikhil Kothari

Posted on 8/12/2009 @ 10:25 PM
@Anil - All good questions.

Yes, there is some cost specifying the glue between view and its model once you decouple things. At the same time keep in mind that a view model is about encapsulating view state and interaction logic, and not view logic. Code-behind is perfectly fine to have to implement view logic (eg. starting/stopping storyboards). It is also perfectly fine to handle events (from UI elements and the view model itself) and have event handlers in code-behind call into the view model... essentially in that case your glue is imperative rather than declarative. It is not the amount of code-behind that signals whether a view model is warranted or not, but how much code exists that could be decoupled, which is of course harder to think of up front. Even then, you have to apply some level of judgment depending on what you might want to test, develop independent of design, or where the code will be as it evolves...

Also, there isn't a rule to get to zero code-behind, but initially the desire to get to zero helps push one to internalize the viewmodel pattern. Once that happens, typically you tend to be able to separate what is truly view logic that is ok to be expressed as code-behind, and what is interaction logic and associated state that can be naturally decoupled from the view. The question I do ask of remaining view logic is how much of it is truly view specific, vs. how much of it can be encapsulated into a reusable component that is declaratively instantiated (eg. a custom control, or attached behavior).

You can definitely have multiple views bound to the same view model. That is one of the potential benefits of more advanced usage of the viewmodel pattern.

Hope that helps.

Anil

Posted on 8/13/2009 @ 9:26 AM
Nikhil,

Thanks a lot for a clear and concise explanation. I really appreciate your thoughts and time. I am going to keep this conversation as a reference while implementing MVVM.

--Anil

Quinton

Posted on 8/17/2009 @ 1:10 AM
Hi

In the demo I attended you used a polling method to refresh the view with new updates from the DB and mentioned that RIA could be modified to implement duplex mode binding. Could you possible describe how that would be implemented with RIA services as I cannot track any information on how to accomplish it using RIA services.

Thanks
Q

Vitor

Posted on 8/17/2009 @ 12:42 PM
Hi Nikhil,
Thanks a lot for sharing this code! I enjoy your articles and frameworks! I'm evaluating MVVM pattern, learning with yours and Mr. Wildermuth articles and thought about the following: If we create every views in our applications as a Control (not UserControl), with ViewStates and TemplateParts wouldn't be a perfect applicaton of MVVM? where the DefaultStyle would be a View and control logic the ViewModel?

Thanks,

Vítor

jacky

Posted on 8/17/2009 @ 6:51 PM
hi, after i published my silverlight project ,which used ria service ,and run it on IIS6.0 ,visit it with ie8.0 ,i found that the client did not display any data . i track database ,sure that it have queried data ,then debug it with vs2008+IIS6.0 ,it really have data at logic layer ,that use DomainService. But when it return to the callback method ,the result is empty .so ,why ,does here something importent i need to config with my ii6.0? --------by the way ,when debug with vs development server,it all done well .

Nikhil Kothari

Posted on 8/22/2009 @ 10:57 AM
@Quinton
The demo does in fact do polling. I was responding to a question from the audience about other options and alluded to duplex notifications as a possibility when we build offline as a real feature beyond the proof of concept stage. That said, you can use WCF directly and the support for duplex communication in Silverlight to send update notifications, and RIA Services to fetch updated data in response. Its just not integrated end-to-end for this release.

@Vitor
You have absolutely the right observation. When you look at a control, the code in it is like a view model, and its template is the view. It definitely works, and the pattern is quite applicable.

Amit

Posted on 9/2/2009 @ 6:56 PM
Hi Nikhil,
I have read in one of the articles that RIA Services uses text/JASON as message type.
Silverlight 3 supports binary encoding, can use binary encoding with RIA Services ?

Nikhil Kothari

Posted on 9/3/2009 @ 9:58 PM
@Amit
The answer will be yes, you will be able to use binary encoding. Not with today's public bits, but definitely in the not too distant future. :-)

Stefan Buys

Posted on 10/12/2009 @ 5:55 AM
Hi Nikhil,

We attend both your talks (Parts 1 and 2) of .NET RIA Services for Silverlight. I am currently in the process of building a framework to be used for future Silverlight LOB applications within our business. I find that working with SQL Server (LinqToSQL) works very well in RIA Services, but a large portion of our business is built on of Oracle. I manage to scavenge an EDM Provider from codeplex.com/LinqToOracle for Oracle that enables me to utlise LinqToEntities against an Oracle based EDM.

However, most of our Oracle data driven apps utilize Oracle stored procedures for all CRUD events modelled on top of ODP.NET. Is it possible to leverage an existing ODP.NET based DAL in RIA Services?

Nikhil Kothari

Posted on 10/21/2009 @ 12:04 AM
@Stefan
You should be able to call your stored procedures from within the CRUD methods on a DomainService using something like ODP.NET or maybe even ADO.NET.

You don't have to use an ORM. An ORM however makes some scenarios simpler esp. in terms of ordering updates, if you have multiple updates packaged within a single Submit call.

Prabha

Posted on 11/9/2009 @ 12:55 AM
Hi,How can I poll for db updates using RIA Domain services?I need my views to automatically refresh whenever there are db updates through any client.How can I use server polling/pushing data to client with RIA services?

NormP

Posted on 1/16/2010 @ 7:13 AM
very effective and beautiful bi article. Thanks for sharing. good work
du address and e-mail address http://www.normpompa.com.tr to examine what it is missing my chance Do you write?
Post your comment and continue the discussion.