Best practice for config file in Visual Studio F # solution

I wonder if there is a best practice for setting up a config file in an F # solution. Below is an example of my config file being used by various projects in my F # solution.

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>

      

I can put the content above in the inline App.config. Since App.config doesn't allow nested tags this will get messy as my config grows.

So far, the best we've found is the following. Create a top level project configuration and in that project, create a read source file myConfig.xml using the xml type provider. Then collapse the values ​​in myConfig.xml into static properties of the AppConfigObj class. Other projects in the solution will access configuration properties via 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

      

+3


source to share


1 answer


While the default configuration mechanism is sufficient, there is a very practical alternative that uses the custom configuration type provider: FSharp.Configuration



This way you work with typed classes for configuration and customization files.

+3


source







All Articles