Đang chuẩn bị liên kết để tải về tài liệu:
Apress Expert C sharp 2005 (Phần 7)
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'apress expert c sharp 2005 (phần 7)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 6323_c05_final.qxd 2 27 06 1 28 PM Page 274 274 CHAPTER 5 COMPLETING THE FRAMEWORK A CallByName helper method is used to abstract the use of reflection to retrieve the property value based on the property name. It simply uses reflection to get a Propertyinfo object for the specified property and then uses it to retrieve the property value. If the property value is null or is an empty string then the rule is violated so the Description property of the RuleArgs object is set to describe the nature of the rule. Then false is returned from the rule method to indicate that the rule is broken. Otherwise the rule method simply returns true to indicate that that rule is not broken. This rule is used within a business object by associating it with a property. A business object does this by overriding the AddBusinessRules method defined by BusinessBase. Such code would look like this assuming a using statement for Csla.Validation Serializable public class Customer BusinessBase Customer protected override void AddBusinessRules ValidationRules.AddRule new RuleHandler CommonRules.StringRequired Name rest of class This associates the rule method with the Name property so that the PropertyHasChanged call within the property s set block will automatically invoke the rule. You ll see this and other rule methods used in Chapter 8 within the sample application s business objects. StringMaxLength A slightly more complex variation is where the rule method needs extra information beyond that provided by the basic RuleArgs parameter. In these cases the RuleArgs class must be subclassed to create a new object that adds the extra information. A rule method to enforce a maximum length on a string for instance requires the maximum length value. Custom RuleArgs Class Here s a subclass of RuleArgs that provides the maximum length value public class MaxLengthRuleArgs RuleArgs private int _maxLength public int MaxLength get return _maxLength public MaxLengthRuleArgs string propertyName int .