CSLA and ViewModel<T>, and why I gave up

CSLA and ViewModel<T>, and why I gave up

Old forum URL: forums.lhotka.net/forums/t/9000.aspx


dagware posted on Thursday, May 27, 2010

Before I go on, let me make it clear that I love CSLA, and think Rocky is the bomb! SO although this isn't exactly a positive post, I still love everyone. ;p

I've been trying to understand how to get CSLA 3.8.3 and ViewModel<T> to work with in Parent/Child/Grandchild situation, with the children and grandchildren being sorted. I have also been trying to figure out how to have a ViewModel<T> for not only the child and grandchild collections, but the individual business objects also - because Rocky suggested I use a ViewModel<T> anywhere where I need to use Verbs.

I have had nothing but frustration, and no real success at all. The main problem is that I don't have enough documentation, or else enough of an example to understand how it's supposed to work. I know Rocky feels that the MVVMexperiment should be enough, but for me it just isn't.

It would be nice if someone could write a simple demo showing how to do a parent/child/grandchild example with all of them databound at one time (instead of clicking a button to show the children), and perhaps even how to implement ViewModel<T> for each child object, that would be really helpful.

Until then, I have to give up on it.

Dan

hayrob replied on Thursday, May 27, 2010

Can I support this post, including the first sentence!

The videos may well be a great resource but they are priced way out of my pocket. I have copies of all Rocky's books and ebooks since 2001, but the videos are too expensive. I have set aside a few days next week to work through the source code of Csla4.0 and I'm going to try to get the samples working - I've failed so far!

So please can we have some documentation coming along soon - I'm happy to pay up front for it, and read it in draft.

(This is not really a moan - OK, it is - but a plea from someone who values Csla and wants to incorporate Csla4.0 in my next project.)

Jav replied on Thursday, May 27, 2010

Dan,

I can relate to that.  I have had exactly the same issues as you have narrated.  But I didn't want to give up because the promise was too compelling. 

I also have a huge object graph, and no I also never figured out how to create functioning VMs for all those children or grandchildren. Now the promise of MVVM is supposed to be do away with any and all code from the code-behind file.  In order to do that I needed to be a lot more proficient with xaml than I currently am.  So I decided to give up on that goal, and let it all be handled in the code-behind.  So what was left was essentially the communication of my View with my Model.  So here's what I did. It's probably not pretty but it works. 

I created a single ViewModel for my Root object only.  When any UI element needs to talk to any object in the object graph, it does that through the same one ViewModel, using the Model property:  Model.MyFirstChild.MyGrandChild etc.  For DataBinding, I do the same.  I set the DataContext to the proper object or List in the hierarchy using MyViewModel.Model.MyChild and go from there.  I am not finsished with my project, and probably I'll hit a brick wall one of these days - but for now it works for my limited goals.  Can I join the MVVM club now!  I wouldn't even want to apply yet.

HTH

Jav

RockfordLhotka replied on Thursday, May 27, 2010

Here's a start:

http://www.lhotka.net/cslacvs/viewvc.cgi/core/trunk/Samples/CslaLight/cs/CslaMvvmSl/

I'm using this sample (and it will continue to evolve and grow) in my public presentations. It is a little less complete than the one I've been building for the MVVM video content, but shows the most important parts.

cds replied on Friday, May 28, 2010

Hi Rocky

Just wanting to start experimenting with CSLA and MVVM - the sample, is that for 3.8.3 or 4.0 beta 2?

I've downloaded 4.0 beta 2 and got the above sample source from SVN. I've updated the references in the sample to point to  the appropriate assemblies. When I run it it seems to go OK but when I click the Save button (adding a new Person) I get a missing method exception (and get thrown into the runtime debugger).

The exact exception is:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; GTB6.4; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; FDM; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; Zune 4.0; OfficeLiveConnector.1.5; OfficeLivePatch.1.3)
Timestamp: Fri, 28 May 2010 10:09:13 UTC


Message: Unhandled Error in Silverlight Application
Code: 4004   
Category: ManagedRuntimeError      
Message: System.MissingMethodException: Save
   at Csla.Xaml.TriggerAction.CallMethod(Object sender, EventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)    

Line: 54
Char: 13
Code: 0
URI: file:///F:/Visual%20Studio%20Projects/CslaMvvmSl/CslaMvvmSl/Bin/Debug/CslaMvvmSlTestPage.html

 

 

What gives?

 

RockfordLhotka replied on Friday, May 28, 2010

That is in /core/trunk, so it is CSLA 4.

The basic concepts and structures should mostly work in 3.8 too - but the code itself won't. There've been some enhancements to the TriggerAction and ViewModel<T> types for CSLA 4.

I think the particular issue you are encountering is due to TriggerAction. The 3.8 TriggerAction doesn't include all the code to dereference CollectionViewSource objects, so you need to make sure the DataContext is set to the current item explicitly (by setting Path to / or CurrentItem).

Ranjini replied on Friday, May 28, 2010

Rocky -

We are in the same boat and would like to look through this sample you have provided a link to, but, we are unable to download it.. I guess it is a part of source control? Can you please provide a link outside of that to download it? Or am i missing something , here?

Thanks much for all the guidance you continue provide

Ranjini

RockfordLhotka replied on Friday, May 28, 2010

Right now it is in the source code repository, and it will be in the CSLA 4 Beta 3 download in a couple weeks.

In the meantime you can use TortoiseSvn or a similar tool to pull it from source control: http://www.lhotka.net/cslanet/Repository.aspx

dagware replied on Friday, May 28, 2010

It's good to know I'm not alone in my issues. And thanks, Rocky, for responding. Unfortunately I won't be able to fiddle with this any further right now, because I'm going a different direction in the project I'm working on right now, and of course, deadlines are deadlines.

But I didn't want you to think I didn't appreciate the response, because I do!

Good luck to everyone else. I'll be curious to see how you all do.

Dan

cds replied on Saturday, May 29, 2010

RockfordLhotka

That is in /core/trunk, so it is CSLA 4.

The basic concepts and structures should mostly work in 3.8 too - but the code itself won't. There've been some enhancements to the TriggerAction and ViewModel<T> types for CSLA 4.

I think the particular issue you are encountering is due to TriggerAction. The 3.8 TriggerAction doesn't include all the code to dereference CollectionViewSource objects, so you need to make sure the DataContext is set to the current item explicitly (by setting Path to / or CurrentItem).

Thanks for the reply Rocky, but I'm still confused. The question about 3.8 vs 4.0 was due to the originator of this thread referring to 3.8.

However, I've now got 4.0 beta 2 downloaded and am trying to run the CslaMvvmSl sample - targetting Silverlight 4, using CSLA 4 beta 2. It runs, but the Save and Cancel buttons both cause the exception. Are you saying that in 4.0 this doesn't actually work?

RockfordLhotka replied on Saturday, May 29, 2010

It works in CSLA 4 beta 3. Please remember, this is beta code. So as I'm working with it (and hopefully you and others too) I find bugs/issues and address them. While the rate of change is becoming very low at this point, there are still some changes - including in ViewModelBase<T> and TriggerAction.

The changes to TriggerAction aren't strictly necessary. You can always force the binding Path to 'CurrentItem' or '/', but I figured out a way to make that unnecessary, by having TriggerAction be aware of the CollectionViewSource type. But that change was put in place after beta 2...

I realize the original question was for 3.8 - but 3.8 is in maintenance mode now, and all my attention is on 4. These periods of transition are always a little complicated, but ultimately I only have so much bandwidth...

But things are a little different in .NET 3.5 anyway. While you can use a CollectionViewSource in 3.5, most people don't. They set the datacontext directly to the viewmodel - in which case the 3.8.3 TriggerAction works great. I had never used CollectionViewSource at all until I started using the VS10 XAML designer...

It is really important to remember that MVVM is a design pattern - a broad guideline of a concept. The specific details of implementing any pattern often vary depending on technology, product versions and so forth. That's true for all patterns.

cds replied on Saturday, May 29, 2010

Thanks for the claification.

So if I us SVN to get the latest to play with it should work then.

 

Copyright (c) Marimer LLC