Silverlight BeginSave()

Silverlight BeginSave()

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


vinco83 posted on Thursday, November 18, 2010

Hello everyone! i'm hoping someone can point me in the right direction here. I've searched high and low and can't find any help on this! I've got a viewmodel, which is inherits from "ViewModel", and a View who's datacontext is set to the "Model" property.

I'm calling BeginRefresh("factoryMethod"), and everything works great. Now, if I change a "Model" value...I want to save this. However, my code is not working.

public void SaveData() { Model.Save() }

Now, my factory method looks like this:

public override void BeginSave(bool forceUpdate, EventHandler handler, object userState)

 {

try

{

var dp = new DataPortal();

//dp.UpdateCompleted += handler;

bool d = IsDirty; dp.BeginUpdate(new SingleCriteria (2));

} catch (Exception ex) { string msg = ex.Message; }

 }

I'm sure i'm doing something wrong here. Can someone show me an example of implementing a simple BeginSave() (including if I need to override the "BeginSave()" method in the Business Object?)... Thanks in advance!

RockfordLhotka replied on Thursday, November 18, 2010

ViewModel<T> has a Save method that does all the work for you, just use that and let it do its thing - no need to write your own.

Also, BusinessBase has a BeginSave method on it that does all the work for you, and that's what is called by ViewModel<T>.

If you are using all these things together, you should have to do no actual work, all the hard stuff is done for you.

The SimpleApp and CslaMvvmSl samples demonstrate these techniques.

Gort replied on Friday, January 14, 2011

I have searched within both the CslaMvvmSl and SimpeApp examples in the 4.1 Beta samples and found no reference to 'BeginSave'.  There is one overridden BeginSave method in the CustomerEdit BO, but that's it.  Am I missing something here? 

Just trying to find a sample of saving data from a SL app.

Tks.

RockfordLhotka replied on Friday, January 14, 2011

The ViewModel<T> class has a Save method. This invokes BeginSave in Silverlight and WP7. Since the samples you mention are using MVVM and ViewModel<T>, they call Save on the viewmodel (using TriggerAction), which calls BeginSave on the business object and automatically handles the async callback.

Gort replied on Friday, January 14, 2011

Is there a way for me to simply save an object in my viewmodel?  I'd like to just call:

MyObject.Save() or MyObject.BeginSave()

I am doing this now and nothing is happening.

Tks.

RockfordLhotka replied on Friday, January 14, 2011

Of course, just look at the code in ViewModelBase and copy the implementation.

The key thing to remember is that Save and BeginSave return a new business object as a result, and you need to update your Model property to reflect that new object (or if you are doing total containment of the business object you need to refresh all the viewmodel properties).

Save directly returns the new object, while BeginSave returns the new object in the async callback.

Gort replied on Friday, January 14, 2011

I'm a bit confused.  I am inheriting from ViewModel<T> in my viewmodel.  Why would I need to re-implement the BeginSave() method? 

Tks.

RockfordLhotka replied on Friday, January 14, 2011

Then I'm confused. I obviously don't understand what you are trying to do.

ViewModelBase has a set of protected methods that are used by ViewModel to implement its public methods. If you are subclassing ViewModel then you have access to the same protected methods, and can use them in your public methods.

ViewModel has a public Save method that "does the right thing" - which means it calls the protected BeginSave method from ViewModelBase.

That BeginSave method is protected, which means you can call BeginSave from other methods you implement in your subclass.

Gort replied on Friday, January 14, 2011

Ok, that is what I was trying to do but apparently there were other issues with my code that we failing so I just needed reassurance that what I was trying to do SHOULD work.  I figured out my issues and it's working now. 

Thanks so much for your help, Rocky!

Copyright (c) Marimer LLC