コード例 #1
0
        public void TryGetConnectionString_NotFound()
        {
            string key        = "TheSecondAllMightyKey";
            string connString = "mongodb://some-mongo-instance:27017/some-db";

            ConfigurationManager.ConnectionStrings.SetWritable().Add(new ConnectionStringSettings(key + "Thou shall not find it!", connString));
            IConnectionStringProvider provider = new ConnectionStringsConnectionStringProvider(key);
            bool result = provider.TryGetConnectionString(out string outConnection);

            Assert.False(result, "The connection string should not be found.");
            Assert.Null(outConnection);
        }
コード例 #2
0
        public void TryGetConnectionString()
        {
            string key        = "TheAllMightyKey";
            string connString = "mongodb://some-mongo-instance:27017/some-db";

            ConfigurationManager.ConnectionStrings.SetWritable().Add(new ConnectionStringSettings(key, connString));
            IConnectionStringProvider provider = new ConnectionStringsConnectionStringProvider(key);
            bool result = provider.TryGetConnectionString(out string outConnection);

            Assert.True(result, "Unable to get the connection string.");
            Assert.Equal(outConnection, connString);
        }
コード例 #3
0
        private static IConnectionStringProvider ParseConnectionStringProvider(JsonNode node, IFilePathMapper pathMapper)
        {
            if (node.AsObject == null)
            {
                return(new StaticConnectionStringProvider(node.Value));
            }

            JsonClass objClass = node.AsObject;
            SupportedConnectionStringLocators locator;

            if (!Enum.TryParse(node["locator"]?.Value, out locator))
            {
                throw new NAMEException($"The locator {node["locator"]?.Value} is not supported.");
            }
            IConnectionStringProvider provider = null;
            string key;

            switch (locator)
            {
#if NET45
            case SupportedConnectionStringLocators.ConnectionStrings:
                key      = node["key"]?.Value;
                provider = new ConnectionStringsConnectionStringProvider(key);
                break;

            case SupportedConnectionStringLocators.AppSettings:
                key      = node["key"]?.Value;
                provider = new AppSettingsConnectionStringProvider(key);
                break;

            case SupportedConnectionStringLocators.VSSettingsFile:
                key = node["key"]?.Value;
                string section = node["section"]?.Value;
                if (string.IsNullOrEmpty(section))
                {
                    throw new ArgumentNullException("section", "The section must be specified.");
                }
                provider = new VisualStudioSetingsFileConnectionStringProvider(section, key);
                break;
#endif
            case SupportedConnectionStringLocators.JSONPath:
            {
                key = node["expression"]?.Value;
                string file = node["file"]?.Value;
                if (string.IsNullOrEmpty(file))
                {
                    throw new ArgumentNullException("file", "The file must be specified.");
                }
                provider = new JsonPathConnectionStringProvider(pathMapper.MapPath(file), key);
            }
            break;

            case SupportedConnectionStringLocators.XPath:
            {
                key = node["expression"]?.Value;
                string file = node["file"]?.Value;
                if (string.IsNullOrEmpty(file))
                {
                    throw new ArgumentNullException("file", "The file must be specified.");
                }
                provider = new XpathConnectionStringProvider(pathMapper.MapPath(file), key);
            }
            break;

            default:
                throw new NAMEException($"The locator {locator.ToString()} is not supported..");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException($"The connection string key/expression must be specified.");
            }

            return(provider);
        }