tailieunhanh - Pro ASP.NET MVC Framework phần 10
Nếu bạn viết code lấy dữ liệu từ database ngay trong file code behind của 1 trang thì rõ ràng Model và Controller của bạn dính thành 1 cục và không thể test được bằng Unit Test. Như vậy người không biết sẽ vô tình làm code của mình khó test, khó bảo quản và khó sử dụng lại. | CHAPTER 14 DEPLOYMENT 499 rip The WebConfigurationManager API is great for reading configuration settings out of your file it s much easier than retrieving configuration settings from a database table. What s more WebConfigurationManager can write changes and new values back into your file. However for performance scalability and security reasons 10 you should avoid writing changes to frequently and consider storing frequently updated settings such as user preferences in your application s database instead. WebConfigurationManager is best for the sorts of settings that don t change between deployments such as server addresses and disk paths. Configuring Connection Strings Many web applications need to deal with database connection strings. Of course you don t want to hard-code them in your source code it s far more flexible and useful to keep connection strings in a configuration file. ASRNET has a special API for configuring connection strings. If you add entries to your file s connectionStrings node such as the following configuration connectionStrings add name MainDB connectionString Server myServer Database someDB . add name AuditingDB connectionString Server audit01 Database myDB . connectionStrings configuration then you can access those values via . For example you can use the following code to obtain a LINQ to SQL DataContext string connectionstring MainDB var dataContext new DataContext connectionString var query from customer in Customer where . etc Note If you re using an IoC container to instantiate your data access objects you can usually configure connection strings and any other settings for IoC components using the IoC container instead. This was the technique demonstrated in Chapter 4. Configuring Arbitrary Key Value Pairs If you need a way of configuring mail server addresses disk paths or other simple
đang nạp các trang xem trước