CSLA 如何验证 StartDate 和 FinishDate?

CSLA How to validate StartDate and FinishDate?

我是 CSLA 的新手,我已经开始了解 validation.I 有一个简单的形式 Form1 包含 datagridview1 并且在 Column2 和 Column3 中 StartDate 和 FinishDate 是 shown.My 的想法是在这个 WinForm 中使用 ErrorProvider 并制定一些像这样的验证规则 StartDate < FinishDate 并且如果对象无效则禁止将对象保存到数据库。

知道我该怎么做吗?

提前致谢! (我知道如何在没有 csla 的情况下使用 INotifyPropertyChanged 和 IDataErrorInfor 接口做到这一点)

这是一些示例代码,我有根可编辑的集合

[Serializable]
public class PDVCollection : BusinessBindingListBase<PDVCollection,PDV>
{
    private PDVCollection()
    {
        AllowNew = true;
    }
    protected override object AddNewCore()
    {
        var item = PDV.PDV();
        Add(item);
        return item;
    }

    #region Factory Methods


    public static PDVCollection GetAll()
    {
        return DataPortal.Fetch<PDVCollection>();
    }

    protected override void DataPortal_Update()
    {
        Child_Update();
    }

    #endregion

    #region Data Access
    private void DataPortal_Fetch()
    {
        RaiseListChangedEvents = false;

        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVSelect", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        try
        {
            con.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                var stopa = PDV.GetPDV(dr);
                Add(stopa);
            }
            con.Close();
        }
        catch (Exception xcp)
        {
            throw xcp;
        }

        RaiseListChangedEvents = true;
    }

}
    #endregion

以及可编辑子项的代码

[Serializable]
public class PDV : BusinessBase<PDV>
{
    public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
    public int Id
    {
        get { return GetProperty(IdProperty); }
        private set { LoadProperty(IdProperty, value); }
    }

    public static readonly PropertyInfo<Guid> UidProperty = RegisterProperty<Guid>(c => c.Uid);
    public Guid Uid
    {
        get { return GetProperty(UidProperty); }
        private set { LoadProperty(UidProperty, value); }
    }

    public static readonly PropertyInfo<DateTime> StartDateProperty = RegisterProperty<DateTime>(c => c.Stopa);
    public DateTime StartDate
    {
        get { return GetProperty(StartDateProperty); }
        set { SetProperty(StartDateProperty, value); }
    }

    public static readonly PropertyInfo<DateTime> FinishDateProperty = RegisterProperty<DateTime>(c => c.Stopa);
    public DateTime FinishDate
    {
        get { return GetProperty(FinishDateProperty); }
        set { SetProperty(FinishDateProperty, value); }
    }


    #region Factory Methods

    internal static PDV NewPDV()
    {
        return DataPortal.CreateChild<PDV>();
    }

    internal static PDV GetPDV(MySqlDataReader dr)
    {
        return DataPortal.FetchChild<StopaPDV>(dr);
    }

    private StopaPDV()
    {

    }

    #endregion



    #region DataAccess

    protected override void Child_Create()
    {
        LoadProperty(UidProperty, Guid.NewGuid());
        base.Child_Create();
    }

    private void Child_Fetch(MySqlDataReader dr)
    {
        LoadProperty(IdProperty,Int32.Parse(dr[0].ToString()));
        LoadProperty(UidProperty, Guid.Parse(dr[1].ToString()));
        LoadProperty(StartDateProperty,DateTime.Parse(dr[2].ToString()));
        LoadProperty(FinishDateProperty, DateTime.Parse(dr[3].ToString()));

    }

    private void Child_Insert()
    {
        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVInsert", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add("_uid", MySqlDbType.VarChar, 36).Value = Uid;
        cmd.Parameters.Add("_startTime", MySqlDbType.DataTime).Value = StartDate;
        cmd.Parameters.Add("_finishTime", MySqlDbType.DataTime).Value = FinishDate;
        cmd.Parameters.Add("_id", MySqlDbType.Int32).Direction = System.Data.ParameterDirection.Output;
        int ID = 0;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            ID =  Convert.ToInt32(cmd.Parameters["_id"].Value);
            con.Close();
        }
        catch (Exception xcp)
        {

            throw xcp;
        }



    }

    private void Child_Update()
    {
        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVUpdate", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add("_startTime", MySqlDbType.DataTime).Value = StartDate;
        cmd.Parameters.Add("_finishTime", MySqlDbType.DataTime).Value = FinishDate;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception xcp)
        {

            throw xcp;
        }
    }

    private void Child_DeleteSelf()
    {
        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVDeleteById", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add("_id", MySqlDbType.Int32).Value = Id;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception xcp)
        {

            throw xcp;
        }
    }
    #endregion
}

好吧,我终于发现 solution.It 很简单,只有几行代码。 所以,如果有人对此感兴趣,就在这里。

首先,我们需要创建class并覆盖里面的Execute方法

 public class StartFinishTimeValidation : Csla.Rules.BusinessRule
    {
        protected override void Execute(Csla.Rules.RuleContext context)
        {
            var target = (PDV)context.Target;
            //var od = (DateTime)ReadProperty(target, PDV.StartDateProperty);
            //var doo = (DateTime)ReadProperty(target, PDV.FinishDateProperty);
            if (target.StartDate > target.FinishDate)
            {
                context.AddErrorResult("The date is not correct");

            }


        }
    }

创建 class 后,我们需要重写 AddBusinessRule,并在两个方向上使用 Dependency。

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

        BusinessRules.AddRule(new StartFinishTimeValidation {PrimaryProperty = StartDateProperty });
        BusinessRules.AddRule(new Dependency(StartDate,FinishDate));
        BusinessRules.AddRule(new Dependency(FinishDate, StartDate));


    }