且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从.NET Core / ASP.NET Core中的类库访问App关键数据

更新时间:2023-02-17 07:44:47

根据面向对象编程中信息隐藏的原理,大多数类不需要访问您的应用程序配置。只有您的主应用程序类才需要直接访问此信息。您的类库应公开其属性和方法,以根据调用者认为必要的任何条件改变其行为,并且您的应用程序应使用其配置来设置正确的属性。

By the principle of Information Hiding in Object-Oriented Programming, most classes should not need to have access to your application configuration. Only your main application class should need to directly have access to this information. Your class libraries should expose properties and methods to alter their behavior based on whatever criteria their callers deem necessary, and your application should use its configuration to set the right properties.

例如,DateBox不需要知道时区信息如何存储在应用程序配置文件中-它只需要知道它具有DateBox.TimeZone属性,就可以在运行时检查它所在的时区。

For example, a DateBox shouldn't need to know how timezone information is stored in your application configuration file - all it needs to know is that it has a DateBox.TimeZone property that it can check at runtime to see what timezone it is in.