Prism & CSLA

Nov 4, 2008 at 9:16 PM
I've been working on creating a test app that uses CSLA business objects, and I've run into some problems.

I have a DelegateCommand called SaveEditsCommand in my ViewModel that takes in a "Widget" object and provides it two the execute and canexecute methods.
In the view, I have a button Command that is bound to the SaveEditsCommand, and a CommandParameter that is bound to a "Widget" property in the ViewModel.

The code in the SaveEdits method will complete when the command is executed, and I can see at the end of the method that the Widget state has been updated, and has been persisted.
What I don't understand is why the command parameter in the CanSaveEdits method doesn't reflect the updated state after the command has been executed.

private bool CanSaveEdits(CSLABusinessObjects.Widget widget)
{
    if (widget== null) return false;
    return widget.IsSavable;
}

private void SaveEdits(CSLABusinessObjects.Widget widget)
{
    if (widget == null) return;
    try
    
{
        widget.ApplyEdit();
        this.Widget = widget.Save();
        ((Csla.Core.ISavable)widget).SaveComplete(widget);
    }
    catch(Exception ex)
    {
    }
    SaveEditsCommand.RaiseCanExecuteChanged();
}

Developer
Nov 7, 2008 at 2:03 PM

If your WPF window/user control is bound to the Widget property on your ViewModel, you will need to make sure your ViewModel implements INotifyPropertyChanged and the Widget property is raising the property changed event.

HTH,

-b
Nov 7, 2008 at 6:26 PM
I had tried that earlier, and it doesn't seem to fix the issue.  Here is an example of what I had tried....

public event PropertyChangedEventHandler PropertyChanged;

public WidgetViewModel()
{
    PropertyChanged += new PropertyChangedEventHandler(WidgetViewModel_PropertyChanged);
}

private void SaveEdits(CSLABusinessObjects.Widget widget)
{
    if (widget == null) return;
    try
    
{
        widget.ApplyEdit();
        this.Widget = widget.Save();
        ((Csla.Core.ISavable)widget).SaveComplete(widget);
        PropertyChanged(this, new PropertyChangedEventArgs("Widget"));
        SaveEditsCommand.RaiseCanExecuteChanged();
    }
    catch(Exception ex)
    {
    }
    SaveEditsCommand.RaiseCanExecuteChanged();
}

I'm concerned that the issue is that in CSLA the save function actually makes a clone of the object it's trying to persist.  So the DataContext is pointing to the original object, then after the save operation, a new widget object is created.  I'm thinking that the DataContext needs to be assigned to the new widget object.  The line that says this.Widget = widget.Save() is not enough.  I tried to add a handler to the PropertyChanged event to set the DataContext again, but that has no effect.

void WidgetViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    View.Model = this;  // This sets DataContext = to the viewmodel in the View object.
}

Any thoughts?

Dec 9, 2008 at 11:12 AM
Did u found a solution?

Im wondered because we're going to use CSLA and Prism..
Dec 9, 2008 at 1:45 PM
I did not get it to work the way I wanted, and decided to modify the design and move on.
Over all, I feel like CSLA and Prism can work together, but you have to think about how to marry up the two things.

I like the save function in winforms, but I havn't got it to work well with Prism.  Perhaps I'm overlooking the obvious.
The app I'm working on is a pet project to really learn WPF.  It's pretty simple.  If there is enough response, I would be willing to share it.

Jeff
Dec 11, 2008 at 12:57 PM
1 response ;)
Dec 13, 2008 at 12:09 AM
Edited Dec 13, 2008 at 12:09 AM
Me too.  Me too.

--Dave
http://dave.thehorners.com/
Dec 16, 2008 at 7:02 AM
I've been reading Rocky's new book, and I feel like some stuff is starting to gel.  Rather then posting my old pet project, I've decided to work on implementing PTracker using Prisim.
Hopefully people will look at it, and offer suggestions on ways to improve it.
Check out my blog for updates.  www.codingandcoffee.com

Jeff
Dec 16, 2008 at 3:18 PM
Ahhh u started a blog, very very new... just 2 text's ;)
Dec 16, 2008 at 6:07 PM
Yea....I spend so much time on it, I must have worn out the password.  : ^ ) I'll have to work on it tonight.

So far I've got the login module working.  The one thing that is bugging me is that there are two commands, one for logging in, and one for logging out. 
I've got the enabled/disabled thing working, but I really want to adjust their visibility.  I don't want to put a bunch of code in the PresenterModel every time I need to alter the visibility of a control that is bound to a command, so I need to think about how to handle that.  I think Rocky is using a decorator control to do something similar, so perhaps I'll go down that road.

Jeff

http://www.codingandcoffee.com/Downloads/CompositePTracker.zip