csla 中的异步验证规则

发布于 2024-08-28 05:33:03 字数 132 浏览 10 评论 0原文

有谁有在 csla 中实现异步验证规则的简单示例吗?

我已经查看了 Rolodex 示例中 Company 类中的示例,但这对我来说并不是特别清楚:为什么我需要命令类?

我在 WPF 应用程序中使用 csla 3.8。

Does anyone have a simple example of implementing an async validation rule in csla?

I have looked at the example in the Company class in the Rolodex sample but this isn't particularly clear to me: why do I need a command class?

I'm using csla 3.8 in a WPF application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2024-09-04 05:33:03

好吧,我想出了如何做到这一点,但最终结果证明答案打开了一大堆蠕虫。

将异步规则添加到您的业务类:

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;
        }
    }
}

就是这样。我遇到的问题是Command的DataPortal_Execute中的所有代码都需要是线程安全的。就我而言,这导致了严重的问题,所以现在我恢复到同步规则。

OK I figured out how to do this, but in the end it turned out that the answer opened a rather large can of worms.

Adding the Async rule to your business class:

protected override void AddBusinessRules()
{
    base.AddBusinessRules();

    ValidationRules.AddRule(UserNameIsUniqueAsync, new AsyncRuleArgs(UserRefProperty, NameProperty));
}

The first arg for AddRule is a delegate shown below:

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);
}

The delegate in turn calls a DuplicateUserNameCommand which is new type you need to create:

[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;
        }
    }
}

That's about it. The problem that I encountered is that all the code in Command's DataPortal_Execute needs to be thread safe. In my case this caused severak issues so for now I'm reverting back to synchronous rules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文