예제 #1
0
        // <summary>
        // Sets the EntityFramework "defaultConnectionFactory" in the given XML document representing a
        // .config file to use th given specification. This method differs from AddConnectionFactoryToConfig
        // in that it always sets the entry to use the given specification even if it was already present
        // and set to something else.
        // </summary>
        // <param name="config"> An XML document representing the config file. </param>
        // <param name="specification"> Specifies the connection factory and constructor arguments to use. </param>
        // <returns> True if the document was modified; false if no change was made. </returns>
        public virtual bool AddOrUpdateConnectionFactoryInConfig(XDocument config, ConnectionFactorySpecification specification)
        {
            DebugCheck.NotNull(config);

            var connectionFactoryElement = config
                                           .GetOrCreateElement(ConfigurationElementName)
                                           .GetOrCreateElement(EntityFrameworkElementName)
                                           .GetOrCreateElement(DefaultConnectionFactoryElementName);

            var currentFactoryAttribute = connectionFactoryElement.Attribute("type");

            if (currentFactoryAttribute != null &&
                specification.ConnectionFactoryName.Equals(currentFactoryAttribute.Value, StringComparison.OrdinalIgnoreCase) &&
                FactoryArgumentsMatch(connectionFactoryElement, specification))
            {
                return(false);
            }

            connectionFactoryElement.RemoveAll();
            connectionFactoryElement.Add(new XAttribute("type", specification.ConnectionFactoryName));

            AddFactoryArguments(connectionFactoryElement, specification);

            return(true);
        }
예제 #2
0
 private void AddFactoryArguments(XElement factoryElement, ConnectionFactorySpecification specification)
 {
     if (specification.ConstructorArguments.Any())
     {
         var parametersElement = factoryElement.GetOrCreateElement(ParametersElementName);
         specification.ConstructorArguments.Each(
             a => parametersElement.Add(new XElement(ParameterElementName, new XAttribute("value", a))));
     }
 }
예제 #3
0
        private bool FactoryArgumentsMatch(XElement factoryElement, ConnectionFactorySpecification specification)
        {
            var parametersElement = factoryElement.Element(ParametersElementName);
            var currentParameters = parametersElement == null
                                        ? new string[0]
                                        : parametersElement.Elements(ParameterElementName)
                                    .Select(e => e.Attribute("value").Value);

            return(currentParameters.SequenceEqual(specification.ConstructorArguments));
        }
예제 #4
0
        // <summary>
        // Checks whether or not the given XML document representing a .config file contains
        // an EntityFramework "defaultConnectionFactory" entry or not. If no entry is found then one
        // is added for the given connection factory specification.
        // </summary>
        // <param name="config"> An XML document representing the config file. </param>
        // <param name="specification"> Specifies the connection factory and constructor arguments to use. </param>
        // <returns> True if the document was modified; false if no change was made. </returns>
        public virtual bool AddConnectionFactoryToConfig(XDocument config, ConnectionFactorySpecification specification)
        {
            DebugCheck.NotNull(config);
            DebugCheck.NotNull(specification);

            var entityFramework = config
                                  .GetOrCreateElement(ConfigurationElementName)
                                  .GetOrCreateElement(EntityFrameworkElementName);

            if (entityFramework.Elements(DefaultConnectionFactoryElementName).Any())
            {
                return(false);
            }

            var factoryElement = entityFramework
                                 .GetOrCreateElement(
                DefaultConnectionFactoryElementName,
                new XAttribute("type", specification.ConnectionFactoryName));

            AddFactoryArguments(factoryElement, specification);

            return(true);
        }
        // <summary>
        // Checks whether or not the given XML document representing a .config file contains
        // an EntityFramework "defaultConnectionFactory" entry or not. If no entry is found then one
        // is added for the given connection factory specification.
        // </summary>
        // <param name="config"> An XML document representing the config file. </param>
        // <param name="specification"> Specifies the connection factory and constructor arguments to use. </param>
        // <returns> True if the document was modified; false if no change was made. </returns>
        public virtual bool AddConnectionFactoryToConfig(XDocument config, ConnectionFactorySpecification specification)
        {
            DebugCheck.NotNull(config);
            DebugCheck.NotNull(specification);

            var entityFramework = config
                .GetOrCreateElement(ConfigurationElementName)
                .GetOrCreateElement(EntityFrameworkElementName);

            if (entityFramework.Elements(DefaultConnectionFactoryElementName).Any())
            {
                return false;
            }

            var factoryElement = entityFramework
                .GetOrCreateElement(
                    DefaultConnectionFactoryElementName,
                    new XAttribute("type", specification.ConnectionFactoryName));

            AddFactoryArguments(factoryElement, specification);

            return true;
        }
        /// <summary>
        /// Sets the EntityFramework "defaultConnectionFactory" in the given XML document representing a
        /// .config file to use th given specification. This method differs from AddConnectionFactoryToConfig
        /// in that it always sets the entry to use the given specification even if it was already present
        /// and set to something else.
        /// </summary>
        /// <param name="config"> An XML document representing the config file.</param>
        /// <param name="specification"> Specifies the connection factory and constructor arguments to use.</param>
        /// <returns>True if the document was modified; false if no change was made.</returns>
        public virtual bool AddOrUpdateConnectionFactoryInConfig(XDocument config, ConnectionFactorySpecification specification)
        {
            Contract.Requires(config != null);

            var connectionFactoryElement = config
                .GetOrCreateElement(ConfigurationElementName)
                .GetOrCreateElement(EntityFrameworkElementName)
                .GetOrCreateElement(DefaultConnectionFactoryElementName);

            var currentFactoryAttribute = connectionFactoryElement.Attribute("type");
            if (currentFactoryAttribute != null
                && specification.ConnectionFactoryName.Equals(currentFactoryAttribute.Value, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            connectionFactoryElement.RemoveAll();
            connectionFactoryElement.Add(new XAttribute("type", specification.ConnectionFactoryName));

            AddFactoryArguments(connectionFactoryElement, specification);

            return true;
        }
 private void AddFactoryArguments(XElement factoryElement, ConnectionFactorySpecification specification)
 {
     if (specification.ConstructorArguments.Any())
     {
         var parametersElement = factoryElement.GetOrCreateElement(ParametersElementName);
         specification.ConstructorArguments.Each(
             a => parametersElement.Add(new XElement(ParameterElementName, new XAttribute("value", a))));
     }
 }
        private bool FactoryArgumentsMatch(XElement factoryElement, ConnectionFactorySpecification specification)
        {
            var parametersElement = factoryElement.Element(ParametersElementName);
            var currentParameters = parametersElement == null
                                        ? new string[0]
                                        : parametersElement.Elements(ParameterElementName)
                                                           .Select(e => e.Attribute("value").Value);

            return currentParameters.SequenceEqual(specification.ConstructorArguments);
        }