RIA Services Samples Project on CodePlex

Announcing a CodePlex project with RIA Services sample apps and extensions...

This is a brief post to share a CodePlex project I have set up, called RIA Services Essentials. It will host various samples and extensions I've shared out in the past on the blog and at various presentations.

The initial sample I've published is my Book Club app, as a reference application of sorts. I wrote the application at MIX10, and since then it has appeared in various other presentations. The bits I published include some bug fixes, updated login UI and other new features. I'll be publishing a second post detailing what is demonstrated in this application.

Other planned samples I'll be adding over time include my Fluent metadata extension, and LocalDomainClient for unit testing as well as some some samples from MIX09 that are still relevant (such as offline storage) as I get a chance to refresh these things so they run against the RIA Services v1 bits.

Check out the project and stay tuned for updates over time. What else would you like to see the samples cover?

Posted on Monday, 6/7/2010 @ 8:38 AM | #Silverlight


Comments

19 comments have been posted.

John Bloom

Posted on 6/7/2010 @ 10:31 AM
It would be nice if you could cover Ria Services Libraries and how to move logic on the server into class libraries while using EF.

Ben Hayat

Posted on 6/7/2010 @ 11:39 AM
Hi Nikhil;

>>What else would you like to see the samples cover?<<
One thing that I'm beginning to see more and more from companies posting jobs for candidates and asking in the requirement section, is that they're looking for candidates with experience in SL3/4, Prism2/4, MEF, MVVM and RIA services. Another words, companies want to start building business apps using RIA with MVVM and Prism/MEF for building decoupled and module apps. I see this becoming a very prominent requirement.

So, my humble suggestion is to provide a real-life LoB app done with RIA and MVVM and MEF to provide a template or architectural blue print how to go about it, so the companies and candidates can both have a better picture what it means to build a SL app using RIA & MVVM & MEF. I had just returned from some meetings that there were many confusing directions and that shows many people don't have a real and clear picture how to go about it and it's like listening to a bunch of blind men describing an elephant. :-)

Thank you in advance!

Ben Hayat

Posted on 6/7/2010 @ 11:49 AM
Nikhil, I have forgotten since I originally looked at this program. In the Web.Config file, when entering the Access and secret Key, do we need to have the "[Access Key]" around the keys or not? e.g.: "[abcde123]" or "abcde123"?
Thanks!

Nikhil Kothari

Posted on 6/7/2010 @ 12:02 PM
@Ben
First the app does use MVVM. Like any other MVVM approach, this uses its own bits of frameworky pieces, since there isn't something out-of-the-box in Silverlight [for now at least]. On MEF, I don't know if this app is complex enough to qualify/need it. I think the key thing is to apply technology where it is needed. A much bigger app would be needed IMO to rightfully show use of MEF.

On the amazon question, just the key, i.e. "abcde123" without the square brackets. Hope that helps, and works this time. :-)

zubo

Posted on 6/7/2010 @ 2:44 PM
Great idea, just what I was looking for. It is always easier to learn from good examples, like book store app. I hope to see some more unit tests and extensions like fluent metadata!
Also, SilverlightFX is great framework, but because lack of good documentation i couldn't start using it in production, never had time to dig into source.
Thank you for everything and keep up good work!

Rick Arthur

Posted on 6/7/2010 @ 7:46 PM
I would like to see an example that does Inserts and Deletes. Despite promoting Silverlight as a platform that does Line-Of-Business apps, I haven't found any good examples of this out there. (If I recall, even this Bookclub app doesn't do Inserts and Deletes, but only adds and removes from 'My' collection).

It would be nice to see some pattern of what Microsoft recommends as far as this goes.

Thanks,
Rick

Nikhil Kothari

Posted on 6/7/2010 @ 9:22 PM
@zubo
Most of the docs for SilverlightFX are distributed across the blog posts introducing concepts/samples. I agree that its not as optimal as it could be, and hopefully things will improve over time.

@Rick
Did you see the sample and play with it? Did you see BookShelfService - ShareBook and UnshareBook aka Insert and Delete. Insert and Delete can use more meaningful names rather than being forced into generic names.

Aaron

Posted on 6/8/2010 @ 6:50 AM
Nikhil, it would be nice to see the following scenarios covered in one or more of your samples. I am very surprised to see that people aren't requesting to see these features in action, as these are pretty common scenarios in LOB applications.

1.) Add functionality - it would be nice to see how you handle adding a new item and keeping the Submit button disabled until the new item becomes valid.

2.) Master/Details functionality - it would be nice to see how you handle the above situation, with children in mind as well, so that the Submit button is only valid when the parent and all of its children become valid. Maybe display another grid to display the children of a selected parent?

3.) Derived (Calculated) properties - it would be nice to see how you handle a calculated field on a parent (eg SalesOrder.Total) based on a change in one of its children (eg LineItem.Price). So in this scenario, SalesOrder would raise NotifyPropertyChanged("Total") or similar when one of its childrens .Price values changed.

4.) Extending on 3.), it would be nice to have the option to lazy load AND eager load the children, and being able to reuse the derived property functionality.

Thanks!

Aaron

Posted on 6/8/2010 @ 6:54 AM
To clarify 3.) on my previous post, I envision a domain model pattern, where the parent listens to its children (or DomainContext), and updates itself appropriately when one or many of its childrens' properties changes. So this logic would not be handled in the VM, but rather, within the parent (SalesOrder) itself.

Thanks again!

Nikhil Kothari

Posted on 6/8/2010 @ 8:34 AM
@Aaron
There is add functionality in the sample. I don't know if it was you I had a previous comment thread with, but I believe the add button should stay enabled, as it is what triggers entity-wide validation beyond fields. If the entity is not valid at that point, it should not commit, but the Add button should stay enabled in my personal opinion, and the sample reflects that. Same goes for master/detail - entity validation would cover that the children are in valid state.

On change notifications, look at the sample. There are computed properties that propagate change notifications. It is done in the entity itself, and not in the view model.

Hope that helps.

Aaron

Posted on 6/8/2010 @ 12:25 PM
Nikhil, I'll take a look at your updated code, thanks! Before looking into it, however, would your updated sample (in a master/detail scenario) invalidate the parent and its children when clicking the add button? Thanks again

Nikhil Kothari

Posted on 6/8/2010 @ 12:36 PM
@Aaron
The sample doesn't have master/detail in the add scenario. It has a single entity add scenario.

However, for either scenario, what I said was clicking the Add button would trigger entity-level validation (attached to the parent entity type). In the case of master/detail or more precisely composition scenario (eg. order + order line items), the validation rule would validate the parent along with its children, and surface an error at the parent level, and optionally cause the child entities to go into error state as well. Hopefully that clarifies...

Aaron

Posted on 6/8/2010 @ 5:13 PM
Nikhil, thanks for your correspondence. I am having difficulty locating the computed properties that you speak of. Which entity contains the logic?

Thanks

Nikhil Kothari

Posted on 6/8/2010 @ 6:33 PM
@Aaron
Look at Book.cs in the client project within the DataModel folder. Example: the IsNew and IsModified properties.

Aaron

Posted on 6/8/2010 @ 7:47 PM
Nikhil, those aren't quite what I'm talking about. Let's take the following example, I'll try to be as brief as possible as not to waste your time:

partial class SalesOrder : Entity
{
protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
base.OnPropertyChanged(e);
// ideally, this would execute when a LineItem changed
RaisePropertyChanged("Total");
}

public List<LineItem> LineItems { get; set; }

public decimal Total
{
get { return LineItems.Sum(i => i.Price); }
}
}

public partial class LineItem : Entity
{
public decimal Price { get; set; }
}

So for example, when the following code is executed:

SalesOrder.LineItems[2].Price = 20.25;

SalesOrder would be aware of one of its children changing, thus firing -> RaisePropertyChanged("Total");

And any bound value to SalesOrder.Price would be properly reflected.

Hopefully this makes sense. Currently, I have found this impossible to achieve due to events not being raised as one would think they would be (via DomainContext OR individual change events to child objects). This is disappointing, because to achieve this functionality, I am forced to bury the logic within a VM. This is what I mean by WCF RIA not supporting the Domain Model pattern.

Does this make sense? Am I missing something blatently obvious? For example, are there events I should be subscribing to in order to facilitate this behavior?

One final thing to note is that if you modify a new book, back out the changes, it will remain in a modified state. This has been one of my struggles getting this type of behavior to work.

Thanks again for your continued correspondence.

Nikhil Kothari

Posted on 6/8/2010 @ 9:08 PM
@Aaron
If SalesOrder is an Entity from a RIA Service and has an associated set of LineItems, that would be typed as an EntityCollection<LineItem> and the LineItems property on a DomainContext would be of type EntitySet<LineItem>. Both of those types support collection change notifications, while List<T> doesn't.

However, the real problem is not RIA Services. It is that change notification is simply hard and error prone. You not only have to listen to collection change notifications but to property change notifications on individual items in the collection. I have a prototype that removes all manual propagation of changes. It works beautifully - no more need to ever implement INPC or propagate changes manually. Like I said, its not about RIA Services, and correspondingly that prototype has no dependency on RIA Services. Until then you have to manually hook up to both collection changes and collection item property changes - no shortcut. The sample has a simple example of property change notifications, but its the same concept.

If you want to undo your changes you have two options...
- Within a single edit session, call BeginEdit when starting editing, and upon cancel, call CancelEdit (or CommitEdit to commit). Again, see the sample, it uses these APIs.
- Across edit sessions, if you want to revert back to what you got from the server, call RejectChanges.
In either case, don't back out changes manually.

Aaron

Posted on 6/9/2010 @ 7:02 AM
Nikhil, Thanks.

I get that, I just watered the classes down for sake of brevity.

So basically I'm hearing that there are no available hooks via WCF RIA for reliable notifications, and I'm left on my own to implement my own notification mechanism to get this behavior to work?

I would very much like to see your prototoype. Btw, what is INPC?

In the case of backing out changes, what I meant was typing something into a textBox that is bound to an object property. As soon as you type something in, the object itself == modified, and Entity.PropertyChanged fires. If I back out the changes with the backspace, wiping out everything I typed in, the object is still modified. In addition, the Entity.PropertyChanged event will not fire after this scenario, which makes it impossible to raise events and keep track of child changes based on that fact.

Nikhil Kothari

Posted on 6/9/2010 @ 7:25 AM
@Aaron
I don't know what I said that implied there are no reliable notifications. Quite on the contrary, we did explicit work to raise all the right notifications at the right time, on Entity, EntitySet, EntityCollection and DomainContext, whether they are property change notifications or collection change notifications. You of course need to do the work to subscribe to them, and use them for your scenarios.

INPC is INotifyPropertyChanged. My prototype only works for WPF. Silverlight doesn't have a necessary piece of extensbility today. So I am still debating whether to publish or not.

TextBox doesn't commit until you tab away from it. So I would expect typing something, reverting it etc. should not cause a change, because Entity property setters check if the value is same before going into modified state. Yes, if the user types something new and then tabs away, it will get marked as modified. That is what a Cancel button in the form is for - for undoing the changes made in a particular edit session on the entity as a whole.

Sumit Pranav

Posted on 7/20/2010 @ 5:30 AM
I will love to see some well done sample (at least having compete architecture) on MVVM and MEF by you guys.
MEF and Prism is buzzword. We need strong understanding of the pattern but very few samples are available.
Post your comment and continue the discussion.