MarimerLLC / cslaforum

Discussion forum for CSLA .NET
https://cslanet.com
Other
31 stars 6 forks source link

The proper use of commands #848

Open angtianqiang opened 4 years ago

angtianqiang commented 4 years ago

public static class MyCommands { public static async Task DoExampleAsync(...) { var cmd = new ExampleCommand(...);

    cmd     = await DataPortal.ExecuteAsync(cmd);

    return cmd.Result;
}

[Serializable]
private class ExampleCommand
    : CommandBase<ExampleCommand>
{
    public bool Result { get; private set; }

    public ExampleCommand(...)
    {
    }

    protected override void DataPortal_Execute()
    {
        Result = ...
    }
}

}

The above code works well in 4.6, but cannot work after upgrading to 5.0.1. What is the recommended usage

Version and Platform CSLA version: 5.0.1 OS: Windows, Platform: WPF

angtianqiang commented 4 years ago

One more problem: non-backing fields from previous versions of commands can also flow between clients and servers, and the new version seems to require backing fields. [Serializable] public class ClearCacheCommand : CommandBase { public string CacheKey { get; set; }

    protected override void DataPortal_Execute()
    {
        Helpers.CacheHelper.RemoveAllCache(CacheKey);
    }
    //bill status
    public static void ClearCache(string cacheKey)
    {
        Helpers.CacheHelper.RemoveAllCache(cacheKey);

        var cmd = new ClearCacheCommand() { CacheKey = cacheKey };
        cmd = DataPortal.Execute<ClearCacheCommand>(cmd);
        return;
    }
}

}

ajj7060 commented 4 years ago

Your result property is not a Csla managed property; it should be:

public static readonly PropertyInfo<bool> ResultProperty = RegisterProperty<bool>(nameof(Result));
public bool Result {
    get => ReadProperty(ResultProperty);
    private set => LoadProperty(ResultProperty, value);
}

using normal C# properties has not been recommended for some time now; its possible Csla 5 no longer supports serialization of normal C# properties. That's no a bug if that's the case, its deliberate, and the only solution is to stay on csla 4 or move to managed properties.

Also it seems like your static factory method should be Task<bool> if you want to return the bool result.