Write Data Validation Code That Can Be Reused in Other Classes

Viết liệu Xác Nhận Mã có thể dùng lại trong lớp học khác Khi bạn đã có văn bản PhoneNumber và mã xác nhận CustomerID trong phần trước, bạn có thể có suy nghĩ rằng điều này sẽ là mã đột xuất hữu ích trong các phần khác của ứng dụng Northwind | Write Data Validation Code That Can Be Reused in Other Classes As you were writing the PhoneNumber and CustomerlD validation code in the previous section you might have thought that this code would be extraordinarily useful in other parts of the Northwind application. For example the Suppliers and Employees tables also have Phone and Fax fields and the CustomerlD column is also defined in the Orders table. Technique In this section you will pull the data validation code you wrote for both the PhoneNumber and CustomerlD columns and create independent objects that encapsulate your existing validation logic. Then you will update the CCustomer class to make use of these new classes. The CCustomerlD class will be a simple class that performs two functions checking the length of the ID and looking in the database to see if the CustomerlD exists. For the PhoneNumber data validation you will learn how to create an entire object model that will provide you with data validation for different types of phone numbers with a bare minimum of code. And just for the fun of it you will learn how to use the same base class you use to validate phone numbers to validate Social Security numbers. Steps 1. Add a new class file to your project by right-clicking on the project in the Solution Explorer window and selecting Add Class from the Add submenu. Name the new class . 2. Copy the DoesCustomerlDExist and ValidateCustomerlD methods from the CCustomer class into the CCustomerlD class. Rename them Exists and Validate respectively. 3. Copy the InvalidCustomerlDException from and paste it inside the CCustomerID class. This makes your exceptions directly related to the CCustomerID. 4. One gap in splitting off the CustomerlD property into its own class is that some parts of your application might want a read write CustomerlD property whereas others such as the CCustomer class need a ReadOnly property. Instead of having a Boolean set to lock the property a .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.