Ways of keeping configuration code out of logic code using Dependency Injection

前端 未结 4 580
盖世英雄少女心
盖世英雄少女心 2020-12-08 02:23

How can keep all the configuration file code out of my logic code using Settings (ApplicationSettingsBase) and Dependency Injection?

With configuration I mean a cust

4条回答
  •  离开以前
    2020-12-08 02:53

    The important part to realize is that configuration is only one among several sources of values that drive your application's behavior.

    The second option (non-static configuration) is best because it enables you to completely decouple the consumer from the source of the configuration values. However, the interface isn't required, as configuration settings are normally best modeled as Value Objects.

    If you still want to read the values from a configuration file, you can do that from the application's Composition Root. With StructureMap, it might looks something like this:

    var config = (MyConfigurationSection)ConfigurationManager.GetSection("myConfig");
    
    container.Configure(r => r
        .For()
        .Ctor()
        .Is(config));
    

提交回复
热议问题