Is there anything wrong with using the DataPortal like this:
public class FieldMustBeUnique : Csla.Rules.BusinessRule
{
FieldIsUniqueCommand _cmd = null;
public FieldMustBeUnique(IPropertyInfo primaryProperty, FieldIsUniqueCommand cmd)
: base(primaryProperty)
{
if (InputProperties == null) InputProperties = new System.Collections.Generic.List<IPropertyInfo> { primaryProperty };
_cmd = cmd;
}
protected override void Execute(RuleContext context)
{
string field = context.InputPropertyValues[PrimaryProperty].ToString();
_cmd.Field = field;
_cmd = DataPortal.Update(_cmd);
if (!_cmd.IsUnique)
{
context.AddErrorResult(Localization.Localization.GetMessage(Localization.Messages.ObjectAlreadyExists));
}
}
}
[Serializable]
public abstract class FieldIsUniqueCommand
{
public bool IsUnique
{
get;
protected set;
}
public string Field
{
get;
set;
}
protected abstract void DataPortal_Update();
}
You can invoke the data portal from inside a business rule, yes. That is supported.
My one comment, is that by implementing this as a synchronous rule you'll block the user from interacting with the UI until that rule completes. You might choose to make the rule async (as described in the 'Using CSLA 4' books) so the UI doesn't freeze.
Oh, I see - your question was about the Command object not inheriting from CommandBase?
The answer to that is a little more complex.
In pure .NET code the data portal will do what you describe, because it only cares that the type is Serializable and that you implement the appropriate DataPortal_XYZ methods.
But in Silverlight, Windows Phone, and WinRT code this won't work because your type also needs to implement a serialization interface so the MobileFormatter can work. That interface is tricky to implement, and you are better off subclassing one of the CSLA base classes (at a minimum the Csla.Core.MobileObject base class).
Just a couple of suggestions:
Otherwise the code is good.
I have taken your suggestion into account. Thanks for your help.
Copyright (c) Marimer LLC