cslaに非同期検証ルールを実装する簡単な例はありますか?
RolodexサンプルのCompanyクラスの例を見ましたが、これは特に明確ではありません。なぜコマンドクラスが必要なのですか?
WPFアプリケーションでcsla3.8を使用しています。
cslaに非同期検証ルールを実装する簡単な例はありますか?
RolodexサンプルのCompanyクラスの例を見ましたが、これは特に明確ではありません。なぜコマンドクラスが必要なのですか?
WPFアプリケーションでcsla3.8を使用しています。
OK、これを行う方法を理解しましたが、最終的には、答えがかなり大きなワームの缶を開いたことがわかりました。
ビジネスクラスへの非同期ルールの追加:
protected override void AddBusinessRules()
{
base.AddBusinessRules();
ValidationRules.AddRule(UserNameIsUniqueAsync, new AsyncRuleArgs(UserRefProperty, NameProperty));
}
AddRuleの最初の引数は、以下に示すデリゲートです。
private static void UserNameIsUniqueAsync(AsyncValidationRuleContext context)
{
DuplicateUserNameCommand command = new DuplicateUserNameCommand((int)context.PropertyValues["UserRef"], context.PropertyValues["Name"].ToString());
DataPortal<DuplicateUserNameCommand> dp = new DataPortal<DuplicateUserNameCommand>();
dp.ExecuteCompleted += (o, e) =>
{
if (e.Error != null)
{
context.OutArgs.Description = "Error checking for duplicate user name. " + e.Error.ToString();
context.OutArgs.Severity = RuleSeverity.Error;
context.OutArgs.Result = false;
}
else
{
if (e.Object.IsDuplicate)
{
context.OutArgs.Description = "Duplicate user name.";
context.OutArgs.Severity = RuleSeverity.Error;
context.OutArgs.Result = false;
}
else
{
context.OutArgs.Result = true;
}
}
context.Complete();
System.Diagnostics.Debug.WriteLine("Context.Complete()");
};
dp.BeginExecute(command);
}
次に、デリゲートは、作成する必要のある新しいタイプであるDuplicateUserNameCommandを呼び出します。
[Serializable]
public class DuplicateUserNameCommand : CommandBase
{
private int _userRef;
private string _userName;
private bool _isDuplicate;
public DuplicateUserNameCommand(int userRef, string userName)
{
_userRef = userRef;
_userName = userName;
}
public bool IsDuplicate
{
get { return _isDuplicate; }
private set { _isDuplicate = value; }
}
protected override void DataPortal_Execute()
{
// Check for an existing user in the database with the same username
var repository = new NHibernateUserDAORepository();
var existingUser = repository.FindByUserName(_userName);
if (existingUser != null && existingUser.UserRef != _userRef)
{
_isDuplicate = true;
}
else
{
_isDuplicate = false;
}
}
}
それについてです。私が遭遇した問題は、コマンドのDataPortal_Execute内のすべてのコードがスレッドセーフである必要があるということです。私の場合、これにより重大な問題が発生したため、今のところ同期ルールに戻ります。