Visual Studio F#解决方案中配置文件的最佳实践

时间:2014-08-10 11:54:13

标签: visual-studio f# type-providers f#-data

我想知道在F#解决方案中是否有最佳实践来设置配置文件。 下面是我的配置文件示例,我的F#解决方案中的不同项目使用它。

myConfig.xml:

<?xml version="1.0" encoding="utf-8" ?>
   <Config>
      <Segment>  
        <SegmentTypes>
           <Did>did</Did>
           <PostCode>postcode</PostCode>
           <Ip>/ip</Ip>
       </SegmentTypes>

     </Segment>

     <SThreeConfig>
       <AccessKey>aaa</AccessKey>
       <SecretKey>bb</SecretKey>
     </SThreeConfig>

  </Config>

我可以将上面的包含放在内置的App.config中。由于App.config不允许嵌套 标签,随着配置的增长,它会变得混乱。

到目前为止,我们找到的最佳解决方案如下: 创建顶级项目配置,并在该项目中,    创建一个源文件,使用xml类型提供程序读取myConfig.xml。    然后将myConfig.xml中的值包装在静态属性中    一个AppConfigObj类。解决方案中的其他项目将访问配置属性    通过appConfObj。

module Configuration=
   open FSharp.Data
   open System

   // Other projects acccess the configuration properties using AppConfigObj.
   type  AppConfigObj() =       
      static let [<Literal>] configXmlFile = "myConfig.xml"

      static let config = XmlProvider<configXmlFile>.Load(configXmlFile.ToString())

      static member didPath =  config.Segment.SegmentTypes.Did
      static member postcodePath = config.Segment.SegmentTypes.PostCode
      static member ipPath = config.Segment.SegmentTypes.Ip
      static member s3AccessKey = config.SThreeConfig.AccessKey    
      static member s3SecreteKey=config.SThreeConfig.SecretKey

1 个答案:

答案 0 :(得分:3)

虽然默认配置机制已经足够,但是存在一个非常实用的替代方案,即使用配置定制的类型提供程序:FSharp.Configuration

这样您就可以使用类型化的配置和设置文件。