CommandBase in silverlight

CommandBase in silverlight

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


zhengokusa posted on Wednesday, June 10, 2009

In the pass, I have learned how to use CommandBase in BusinessBase as a Business rule.

My question here is How can I use it in my silverlight application directly, just like use any other BusinessBase classes. 

The task is that, in silverlight app I have a button, when user click the button, it need to fire a database store procedure call and this procedure just move some rows from one table to another. The user just need to know if the store procedure finished or not. That's it.

I was trying to avoid to create an simple ReadOnly BusinessBase Object just for firing such call to the database.

Can anyone point me to the right direction, an example maybe ?

Thanks,

 

 

RockfordLhotka replied on Wednesday, June 10, 2009

CommandBase works almost the same in SL as in .NET.

I do walk through an example of using a CommandBase in my CSLA .NET for Silverlight video series, including demo code.

There are two main differences from .NET:

  1. The data portal call is async in SL, due to the nature of SL, and this means your command will run asynchronously - and this usually impacts your factory method and UI code
  2. You have to use managed properties (or override the serialization methods) in your command object or your property values won't serialize to/from the server

So a command object looks (more or less) like this in SL:

using Csla;
using Csla.Serialization;

[Serializable]
public class MyCommand : CommandBase
{
  public MyCommand()
  { }

  public MyCommand(string someValue)
  {
    this.SomeValue = someValue;
  }

  private static PropertyInfo<string> SomevalueProperty =
    RegisterProperty<string>(typeof(MyCommand), new PropertyInfo<string>("SomeValue"));
  public string SomeValue
  {
    get { return ReadProperty(SomeValueProperty); }
    set { LoadProperty(SomeValueProperty, value); }
  }

  public static void DoWork(
    EventHandler<DataPortalResult<MyCommand>> callback,
    string someValue)
  {
    var dp = new DataPortal<MyCommand();
    dp.ExecuteComplete += callback;
    dp.BeginExecute(new MyCommand(someValue));
  }

  protected override void DataPortal_Execute()
  {
    // do server-side stuff here
  }
}

 

zhengokusa replied on Wednesday, June 10, 2009

Thank you Rock

It works perfectly. except in the "DoWork" factory function, your custom parameter must put before EventHandler.

In my code, I create three properties

Can I bind those command properties to my UI elements just like I bind BB object?

 

 

 

RockfordLhotka replied on Wednesday, June 10, 2009

CommandBase doesn’t fully support data binding like BusinessBase, no. It doesn’t support data binding on the Windows side either.

 

Rocky

 

Copyright (c) Marimer LLC