Validate Data Passed to Properties and Communicate Errors to Developers
Số trang: 7
Loại file: pdf
Dung lượng: 21.69 KB
Lượt xem: 11
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Qua xác minh dữ liệu để tiết và Truyền đạt lỗi để phát triển Để thực hiện một lớp học mà kết thúc tốt đẹp truy cập vào một bảng, nó là rất quan trọng mà các lớp học và không phải là phát triển những người đang sử dụng các lớp bảo đảm rằng tất cả dữ liệu có giá trị trước khi viết nó cơ sở dữ liệu
Nội dung trích xuất từ tài liệu:
Validate Data Passed to Properties and Communicate Errors to Developers 9.6 Validate Data Passed to Properties and Communicate Errors to DevelopersTo make a class that wraps up access to a table, it is critical that the class-and not thedeveloper who is using the class-makes sure that all data is valid before writing it to thedatabase.For example, in the Customers table, the CustomerID field must be five characters inlength-no more, no less-and, of course, it must be unique.Phone numbers and fax numbers must also be validated. Although we dont necessarilyknow how many digits are in a phone number (France has eight-digit numbers, Spain hasnine, and the U.S. has ten), we do know that only numbers and characters such asperiods, parentheses, and hyphens are allowed.You also need to communicate with other developers when their data is not valid. It is thedata classs job to make sure that invalid data doesnt make it to the database.This section presents three tasks: adding code to make sure that data passed to an objectmatches the column properties in the database, adding code that validates complex datatypes and business rules, and communicating errors to the classs consumers.TechniqueFirst, you need to pass errors back up the call stack. In Visual Basic 6, the acceptedmethod was to use Err.Raise, which propagates an error number and an error message.This technique is still available to Visual Basic developers, but it retains all the oldproblems. If you arent careful, programmers who are working on different componentscan raise the same error numbers, making error handling a potential nightmare. (If youarent careful, you can end up raising the same error number in your own code.)You learned how to use structured exception handling in earlier chapters, but the beautyis that you can define your own exceptions as needed, with almost no code. Also, becauseexceptions have a name as opposed to a number, it is far easier for developers to workwith your code. Finally, because exceptions are defined with a Namespace, even if twoprojects define an InvalidCustomerIDException, each exception will be unique.Second, you need to write code to check whether a value is valid. For the CustomerID,you simply need to check the length of the string. (Later on, youll need to add code tocheck whether the ID already exists.) For phone numbers, youll need to examine thestring for invalid characters.StepsBecause validating the maximum allowable length for all of our properties is simplest,tackle this task first. 1. Define a new exception class by inheriting from System.ApplicationException. As mentioned before, Microsoft recommends that all custom exceptions inherit from ApplicationException rather than Exception. System.ApplicationException has most of the methods and properties that you will need to communicate an exception to consumers of this class. The only property that a consumer might find useful is one that exposes what the maximum length of the property is. This would allow the consumer to communicate the error back to the user or truncate the string without having to hard-code the maximum length into his code. Adding the name of the property and the value is also a good idea. Some developers who are using your class might write one long Try...Catch block, so this information will help debug going forward. Paste the code in Listing 9.33 defining the MaximumStringLengthExceededException into your code. Listing 9.33 frmHowTo9_6.vb: Class Declaration for the MaximumStringLengthExceededException Public Class MaximumStringLengthExceededException Inherits System.ApplicationException Private nMaxLen As Integer Public Sub New(ByVal pMaxLen As Integer, ByVal pPropertyName As String, ByVal pValue As String) You need to initialize the base class of this exception. If you do not specifically call a constructor of the base class, the default constructor (the one without parameters) will be called, if it exists. MyBase must precede the call to the base classs constructor so that the .NET runtime knows not to call a constructor in the derived class. MyBase.new(The value specified, & pValue & _ , exceeds the maximum & length of & pMaxLen & allowable by the & _ pPropertyName & property.) End Sub Public ReadOnly Property MaxLength() As Integer Get Return nMaxLen End Get End Property End Class2. Next, modify the set block of each property in the class to check the length of the new value to see if it exceeds the maximum length of the column in the Customers table. If the length of the new value does exceed the maximum length, t ...
Nội dung trích xuất từ tài liệu:
Validate Data Passed to Properties and Communicate Errors to Developers 9.6 Validate Data Passed to Properties and Communicate Errors to DevelopersTo make a class that wraps up access to a table, it is critical that the class-and not thedeveloper who is using the class-makes sure that all data is valid before writing it to thedatabase.For example, in the Customers table, the CustomerID field must be five characters inlength-no more, no less-and, of course, it must be unique.Phone numbers and fax numbers must also be validated. Although we dont necessarilyknow how many digits are in a phone number (France has eight-digit numbers, Spain hasnine, and the U.S. has ten), we do know that only numbers and characters such asperiods, parentheses, and hyphens are allowed.You also need to communicate with other developers when their data is not valid. It is thedata classs job to make sure that invalid data doesnt make it to the database.This section presents three tasks: adding code to make sure that data passed to an objectmatches the column properties in the database, adding code that validates complex datatypes and business rules, and communicating errors to the classs consumers.TechniqueFirst, you need to pass errors back up the call stack. In Visual Basic 6, the acceptedmethod was to use Err.Raise, which propagates an error number and an error message.This technique is still available to Visual Basic developers, but it retains all the oldproblems. If you arent careful, programmers who are working on different componentscan raise the same error numbers, making error handling a potential nightmare. (If youarent careful, you can end up raising the same error number in your own code.)You learned how to use structured exception handling in earlier chapters, but the beautyis that you can define your own exceptions as needed, with almost no code. Also, becauseexceptions have a name as opposed to a number, it is far easier for developers to workwith your code. Finally, because exceptions are defined with a Namespace, even if twoprojects define an InvalidCustomerIDException, each exception will be unique.Second, you need to write code to check whether a value is valid. For the CustomerID,you simply need to check the length of the string. (Later on, youll need to add code tocheck whether the ID already exists.) For phone numbers, youll need to examine thestring for invalid characters.StepsBecause validating the maximum allowable length for all of our properties is simplest,tackle this task first. 1. Define a new exception class by inheriting from System.ApplicationException. As mentioned before, Microsoft recommends that all custom exceptions inherit from ApplicationException rather than Exception. System.ApplicationException has most of the methods and properties that you will need to communicate an exception to consumers of this class. The only property that a consumer might find useful is one that exposes what the maximum length of the property is. This would allow the consumer to communicate the error back to the user or truncate the string without having to hard-code the maximum length into his code. Adding the name of the property and the value is also a good idea. Some developers who are using your class might write one long Try...Catch block, so this information will help debug going forward. Paste the code in Listing 9.33 defining the MaximumStringLengthExceededException into your code. Listing 9.33 frmHowTo9_6.vb: Class Declaration for the MaximumStringLengthExceededException Public Class MaximumStringLengthExceededException Inherits System.ApplicationException Private nMaxLen As Integer Public Sub New(ByVal pMaxLen As Integer, ByVal pPropertyName As String, ByVal pValue As String) You need to initialize the base class of this exception. If you do not specifically call a constructor of the base class, the default constructor (the one without parameters) will be called, if it exists. MyBase must precede the call to the base classs constructor so that the .NET runtime knows not to call a constructor in the derived class. MyBase.new(The value specified, & pValue & _ , exceeds the maximum & length of & pMaxLen & allowable by the & _ pPropertyName & property.) End Sub Public ReadOnly Property MaxLength() As Integer Get Return nMaxLen End Get End Property End Class2. Next, modify the set block of each property in the class to check the length of the new value to see if it exceeds the maximum length of the column in the Customers table. If the length of the new value does exceed the maximum length, t ...
Tìm kiếm theo từ khóa liên quan:
công nghệ máy tính phần mềm kỹ thuật lập trình dữ liệu Validate Data PassedGợi ý tài liệu liên quan:
-
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 247 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 187 0 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 180 0 0 -
6 trang 171 0 0
-
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 147 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 145 0 0 -
Báo cáo thực tập Công nghệ thông tin: Lập trình game trên Unity
27 trang 115 0 0 -
Giáo trình về phân tích thiết kế hệ thống thông tin
113 trang 113 0 0 -
LUẬN VĂN: Tìm hiểu kỹ thuật tạo bóng cứng trong đồ họa 3D
41 trang 104 0 0 -
Bài giảng Kỹ thuật lập trình - Chương 10: Tổng kết môn học (Trường Đại học Bách khoa Hà Nội)
67 trang 102 0 0