tailieunhanh - Pro WPF in C# 2010 phần 7
Hãy tìm kiếm những tài liệu tiếng Anh hay từ các diễn đàn. Đừng hỏi tôi là làm tìm như thế nào nhé! - Đây là những tài liệu được đánh giá hay trên các điễn đàn Việt Nam cũng như nước ngoài mà tôi đã sưu tầm: | CHAPTER 19 DATA BINDING Note The downloadable code for this chapter includes the custom data access component and a database script that installs the sample data so you can test all the examples. But if you don t have a test database server or you don t want to go to the trouble of creating a new database you can use an alternate version of the data access component that s also included with the code. This version simply loads the data from a file while still exposing the same set of classes and methods. It s perfect for testing but obviously impractical for a real application. Building a Data Access Component In professional applications database code is not embedded in the code-behind class for a window but encapsulated in a dedicated class. For even better componentization these data access classes can be pulled out of your application altogether and compiled in a separate DLL component. This is particularly true when writing code that accesses a database because this code tends to be extremely performance-sensitive but it s a good design no matter where your data lives. Designing Data Access Components No matter how you plan to use data binding or even if you don t your data access code should always be coded in a separate class. This approach is the only way you have the slightest chance to make sure you can efficiently maintain optimize troubleshoot and optionally reuse your data access code. When creating a data class you should follow a few basic guidelines in this section Open and close connections quickly. Open the database connection in every method call and close it before the method ends. This way a connection can t be inadvertently left open. One way to ensure the connection is closed at the appropriate time is with a using block. Implement error handling. Use error handling to make sure that connections are closed even if an exception occurs. Follow stateless design practices. Accept all the information needed for a method in its parameters and .
đang nạp các trang xem trước