浏览代码

Extract out hard coded to CongigurationManager to interface

Nick Babcock 6 年之前
父节点
当前提交
f2bacf920e

+ 9 - 0
OhmGraphite/AppConfigManager.cs

@@ -0,0 +1,9 @@
+using System.Configuration;
+  
+namespace OhmGraphite
+{
+    class AppConfigManager : IAppConfig
+    {
+        public string this[string name] => ConfigurationManager.AppSettings[name];
+    }
+}

+ 5 - 7
OhmGraphite/GraphiteConfig.cs

@@ -1,6 +1,4 @@
-using System.Configuration;
-
-namespace OhmGraphite
+namespace OhmGraphite
 {
     public class GraphiteConfig
     {
@@ -15,15 +13,15 @@ namespace OhmGraphite
         public int Port { get; }
         public bool Tags { get; }
 
-        public static GraphiteConfig ParseAppSettings()
+        public static GraphiteConfig ParseAppSettings(IAppConfig config)
         {
-            string host = ConfigurationManager.AppSettings["host"] ?? "localhost";
-            if (!int.TryParse(ConfigurationManager.AppSettings["port"], out int port))
+            string host = config["host"] ?? "localhost";
+            if (!int.TryParse(config["port"], out int port))
             {
                 port = 2003;
             }
 
-            if (!bool.TryParse(ConfigurationManager.AppSettings["tags"], out bool tags))
+            if (!bool.TryParse(config["tags"], out bool tags))
             {
                 tags = false;
             }

+ 7 - 0
OhmGraphite/IAppConfig.cs

@@ -0,0 +1,7 @@
+namespace OhmGraphite
+{
+    public interface IAppConfig
+    {
+        string this[string name] { get; }
+    }
+}

+ 5 - 6
OhmGraphite/InfluxConfig.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Configuration;
 
 namespace OhmGraphite
 {
@@ -18,22 +17,22 @@ namespace OhmGraphite
         public string User { get; }
         public string Password { get; }
 
-        public static InfluxConfig ParseAppSettings()
+        public static InfluxConfig ParseAppSettings(IAppConfig config)
         {
-            string influxAddress = ConfigurationManager.AppSettings["influx_address"];
+            string influxAddress = config["influx_address"];
             if (!Uri.TryCreate(influxAddress, UriKind.Absolute, out var addr))
             {
                 throw new ApplicationException($"Unable to parse {influxAddress} into a Uri");
             }
 
-            var db = ConfigurationManager.AppSettings["influx_db"];
+            var db = config["influx_db"];
             if (string.IsNullOrEmpty(db))
             {
                 throw new ApplicationException("influx_db must be specified in the config");
             }
 
-            var user = ConfigurationManager.AppSettings["influx_user"];
-            var password = ConfigurationManager.AppSettings["influx_password"];
+            var user = config["influx_user"];
+            var password = config["influx_password"];
 
             return new InfluxConfig(addr, db, user, password);
         }

+ 5 - 6
OhmGraphite/MetricConfig.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Configuration;
 
 namespace OhmGraphite
 {
@@ -16,27 +15,27 @@ namespace OhmGraphite
         public GraphiteConfig Graphite { get; }
         public InfluxConfig Influx { get; }
 
-        public static MetricConfig ParseAppSettings()
+        public static MetricConfig ParseAppSettings(IAppConfig config)
         {
-            if (!int.TryParse(ConfigurationManager.AppSettings["interval"], out int seconds))
+            if (!int.TryParse(config["interval"], out int seconds))
             {
                 seconds = 5;
             }
 
             var interval = TimeSpan.FromSeconds(seconds);
 
-            var type = ConfigurationManager.AppSettings["type"] ?? "graphite";
+            var type = config["type"] ?? "graphite";
             GraphiteConfig gconfig = null;
             InfluxConfig iconfig = null;
 
             switch (type.ToLowerInvariant())
             {
                 case "graphite":
-                    gconfig = GraphiteConfig.ParseAppSettings();
+                    gconfig = GraphiteConfig.ParseAppSettings(config);
                     break;
                 case "influxdb":
                 case "influx":
-                    iconfig = InfluxConfig.ParseAppSettings();
+                    iconfig = InfluxConfig.ParseAppSettings(config);
                     break;
             }
 

+ 1 - 1
OhmGraphite/Program.cs

@@ -17,7 +17,7 @@ namespace OhmGraphite
                 {
                     // We need to know where the graphite server lives and how often
                     // to poll the hardware
-                    var config = Logger.LogFunction("parse config", MetricConfig.ParseAppSettings);
+                    var config = Logger.LogFunction("parse config", () => MetricConfig.ParseAppSettings(new AppConfigManager()));
                     double seconds = config.Interval.TotalSeconds;
                     IWriteMetrics writer;
                     if (config.Graphite != null)