Duwamish7学习笔记(四)

类别:.NET开发 点击:0 评论:0 推荐:
 BusinessRules项目
 1.Customer类
   长见识了!在《Duwamish7学习笔记(三)》中还在想业务逻辑层写些什么东西。
   校验,还是校验!每一行,每一列,包括类型和长度,想想自已在项目的校验,真的有点汗颜!
   //----------------------------------------------------------------
    // Function Validate:
    //   Validates and customer
    // Returns:
    //   true if validation is successful
    //   false if invalid fields exist
    // Parameters:
    //   [in]  customerRow: CustomerData to be validated
    //   [out] customerRow: Returns customer data.  If there are fields
    //                    that contain errors they are individually marked. 
    //----------------------------------------------------------------
    private bool Validate(DataRow customerRow)
    {
        bool isValid;
       
        customerRow.ClearErrors();
       
        isValid  = IsValidEmail(customerRow);
        isValid &= IsValidField(customerRow, CustomerData.NAME_FIELD, 40);
        isValid &= IsValidField(customerRow, CustomerData.ADDRESS_FIELD, 255);
        isValid &= IsValidField(customerRow, CustomerData.COUNTRY_FIELD, 40);
        isValid &= IsValidField(customerRow, CustomerData.PHONE_FIELD, 30);
   
        if ( !isValid )
        {
            customerRow.RowError = CustomerData.INVALID_FIELDS;
        }
       
        return isValid;
    }
    全部校验通过之后才调用业务访问层的相应方法进行操作,用using,使创建的Customers使用后能马上释放。
    using (Customers customersAccess = new Customers())
    {
        result = customersAccess.UpdateCustomer(customer);
    }
    下面记录一点小知识
    设置行错误:row.RowError
    设置列错误:row.SetColumnError()
    清除行错误:row.ClearErrors()
    邮件校验的正则表达式:REGEXP_ISVALIDEMAIL = @"^\w+((-\w+)|(\.\w+))*\@\w+((\.
   
  2.Order类
    看完Order类觉得这各层的分工非常好,这层才是真正的业务如CalculateTax(),CalculateShipping(),InsertOrder().    业务逻辑层在封装业务操作方法时,进行了非常严格的校验,使系统有非常强健的健壮性。叹为观止!
 
疑问:Duwamish只是MS自带的一个小例子,在实际的大的项目中有非常多的对象,非常且更复杂的业务逻辑,我们的代码还能写得于此之好吗?

本文地址:http://com.8s8s.com/it/it42250.htm