【问题标题】:Subsonic - Where do i include my busines logic or custom validationSubsonic - 我在哪里包含我的业务逻辑或自定义验证
【发布时间】:2010-10-07 19:24:00
【问题描述】:

我正在使用亚音速 2.2

我尝试用另一种方式问这个问题,但没有得到我想要的答案。

基本上,我通常在页面级别或我的用户控件或 aspx 页面的代码中包含验证。然而,我看到一些小信息建议这可以在亚音速生成的部分类中完成。

所以我的问题是,我将这些放在哪里,是否有我添加我的验证/业务逻辑的特定事件,例如插入或更新。 - 如果是这样,并且验证不满足,我如何停止插入或更新。如果有人有一个代码示例来说明这看起来如何,那我就开始吧。

非常感谢任何信息。

【问题讨论】:

    标签: subsonic subsonic3 subsonic2.2


    【解决方案1】:

    首先,您应该为要使用的 DAL 对象创建一个分部类。 在我的项目中,我有一个文件夹Generated,生成的类就在其中,我还有另一个文件夹Extended

    假设您有一个 Subsonic 生成的类 Product。在您的扩展(或其他)文件夹中创建一个新文件 Product.cs 并创建一个部分类 Product 并确保命名空间与亚音速生成的类命名空间相匹配。

    namespace Your.Namespace.DAL
    {
        public partial class Product
        {
        }
    }
    

    现在您可以扩展产品类。有趣的是 subsonic 提供了一些方法来覆盖。

    namespace Your.Namespace.DAL
    {
        public partial class Product
        {
    
            public override bool Validate()
            {
    
                ValidateColumnSettings();
    
                if (string.IsNullOrEmpty(this.ProductName))
                    this.Errors.Add("ProductName cannot be empty");
    
                return Errors.Count == 0;
            }
    
            // another way
            protected override void BeforeValidate()
            {
                if (string.IsNullOrEmpty(this.ProductName))
                    throw new Exception("ProductName cannot be empty");
            }
    
            protected override void BeforeInsert()
            {
               this.ProductUUID = Guid.NewGuid().ToString();
            }
    
            protected override void BeforeUpdate()
            {
               this.Total = this.Net + this.Tax;
            }
    
            protected override void AfterCommit()
            {
                DB.Update<ProductSales>()
                      .Set(ProductSales.ProductName).EqualTo(this.ProductName)
                      .Where(ProductSales.ProductId).IsEqualTo(this.ProductId)
                      .Execute();
            }
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      回答丹的问题:

      首先,看看这里:http://github.com/subsonic/SubSonic-2.0/blob/master/SubSonic/ActiveRecord/ActiveRecord.cs

      在这个文件中包含了我在另一篇文章中展示的全部逻辑。

      • Validate:在 Save() 期间调用,如果 Validate() 返回 false,则会引发异常。 仅当 Property ValidateWhenSaving(这是一个常量,因此您必须重新编译 SubSonic 才能更改它)为 true(默认)时才调用 Get。

      • BeforeValidate:当 ValidateWhenSaving 为真时在 Save() 期间调用。默认什么都不做

      • BeforeInsert:如果记录是新的,则在 Save() 期间调用。默认情况下什么都不做。

      • BeforeUpdate:如果记录是新的,则在 Save() 期间调用。默认情况下什么都不做。

      • AfterCommit:在成功插入/更新记录后调用。默认情况下什么都不做。

      在我的 Validate() 示例中,我首先让默认的 ValidatColumnSettings() 方法运行,如果产品名称长于数据库中定义的值,它将添加诸如“列 ProductName 的最大字符串长度超出”之类的错误。然后,如果 ProductName 为空,则添加另一个错误字符串,如果总体错误计数大于零,则返回 false。

      这将在 Save() 期间引发异常,因此您无法将记录存储在数据库中。

      我建议你自己调用 Validate(),如果它返回 false,你在页面底部显示 this.Errors 的元素(简单的方法)或者(更优雅)你创建一个 Dictionary&lt;string, string&gt;,其中的关键是列名,值是原因。

          private Dictionary<string, string> CustomErrors = new Dictionary<string, string>
          protected override bool Validate()
          {
      
              this.CustomErrors.Clear();
              ValidateColumnSettings();
      
              if (string.IsNullOrEmpty(this.ProductName))
                  this.CustomErrors.Add(this.Columns.ProductName, "cannot be empty");
      
              if (this.UnitPrice < 0)
                  this.CustomErrors.Add(this.Columns.UnitPrice, "has to be 0 or bigger");
      
              return this.CustomErrors.Count == 0 && Errors.Count == 0;
          }
      

      如果 Validate() 返回 false,您可以直接在网页右侧字段的旁边/下方添加原因。

      如果 Validate() 返回 true,您可以安全地调用 Save(),但请记住,Save() 可能会在持久性期间引发其他错误,例如“复制键 ...”;

      【讨论】:

        【解决方案3】:

        感谢您的回复,但是如果您在 validate() 或 beforevalidate() 中验证列 (ProductName) 值是字符串空或 NULL,您能否为我确认这一点,这是否意味着插入/update 已经被执行,否则它不会知道你试图从页面内的 UI / aspx 字段插入或更新空值到列?

        另外,在 asp.net 插入或更新事件中,我们使用 e.cancel = true 来停止插入更新,如果 beforevalidate 失败,它会自动停止插入或更新操作吗?

        如果是这种情况,是否更容易添加页面级验证以首先停止触发插入或更新。

        我想我对这些方法的生命周期以及它们何时发挥作用有点困惑

        【讨论】:

        • 我添加了另一篇更详细的解释
        猜你喜欢
        • 2019-10-16
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-20
        • 2015-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多