Beispiel #1
0
        /// <summary>
        /// Takes a property and database provider, then determines how to populate that property
        /// based on type.
        /// </summary>
        /// <param name="sender">The object in which the connection property resides.</param>
        /// <param name="activeConnectionProperties">
        /// A collection of properties that have already been populated.</param>
        /// <param name="provider">The database provider that will be used for fetching connections.</param>
        /// <param name="databaseName">The name of the database that a connection should be made to.</param>
        /// <param name="connectionProperty">The property that needs to be populated on the sender object.</param>
        private static void SetConnectionProperty(
            object sender,
            List <PropertyInfo> activeConnectionProperties,
            IDatabaseProvider <IDbConnection> provider,
            string databaseName,
            PropertyInfo connectionProperty)
        {
            if (typeof(IDbConnection).IsAssignableFrom(connectionProperty.PropertyType))
            {
                // Property should be populated with an opened connection instance to the target database.
                IDbConnection newConnection = provider.CreateConnection(databaseName);
                Connections.Add(newConnection);
                connectionProperty.SetValue(sender, newConnection);

                activeConnectionProperties.Add(connectionProperty);
            }
            else if (typeof(Func <IDbConnection>).IsAssignableFrom(connectionProperty.PropertyType))
            {
                // Property should be populated with a delegate that can provide a connection instance when requested.
                connectionProperty.SetValue(
                    sender,
                    (Func <IDbConnection>)(() => provider.CreateConnection(databaseName)));

                activeConnectionProperties.Add(connectionProperty);
            }
            else if (connectionProperty.PropertyType == typeof(string))
            {
                // Property should be populated with a connection string that can be used to connect to the target database.
                using (var connection = provider.CreateConnection(databaseName))
                {
                    connectionProperty.SetValue(
                        sender,
                        connection.ConnectionString);
                }
            }
            else
            {
                throw new InvalidOperationException(
                          "A connection was decorated with the DbTestMonkey.Contracts.ConnectionAttribute " +
                          "attribute but was not a type of System.Data.IDbConnection or System.Func<System.Data.IDbConnection>.");
            }
        }
Beispiel #2
0
        /// <summary>
        /// 创建数据库链接
        /// </summary>
        /// <returns></returns>
        public DbConnection CreateConnection()
        {
            if (String.IsNullOrEmpty(ConnectionString))
            {
                throw new ConfigurationErrorsException(String.Format("ConnectionString:{0} can't be found!", AllInOneKey));
            }

            var connection = m_DatabaseProvider.CreateConnection();

            connection.ConnectionString = ConnectionString;
            return(connection);
        }
Beispiel #3
0
        /// <summary>
        /// 创建数据库链接
        /// </summary>
        /// <returns></returns>
        public DbConnection CreateConnection()
        {
            if (String.IsNullOrEmpty(ConnectionString))
            {
                throw new DalException(String.Format("ConnectionString:{0} can't be found!", AllInOneKey));
            }
#if DEBUG
            Debug.WriteLine(ConnectionString);
#endif
            var connection = m_DatabaseProvider.CreateConnection();
            connection.ConnectionString = ConnectionString;
            return(connection);
        }
        /// <summary>
        /// Takes a property and database provider, then determines how to populate that property
        /// based on type.      
        /// </summary>
        /// <param name="sender">The object in which the connection property resides.</param>
        /// <param name="activeConnectionProperties">
        /// A collection of properties that have already been populated.</param>
        /// <param name="provider">The database provider that will be used for fetching connections.</param>
        /// <param name="databaseName">The name of the database that a connection should be made to.</param>
        /// <param name="connectionProperty">The property that needs to be populated on the sender object.</param>
        private static void SetConnectionProperty(
            object sender,
            List<PropertyInfo> activeConnectionProperties,
            IDatabaseProvider<IDbConnection> provider,
            string databaseName,
            PropertyInfo connectionProperty)
        {
            if (typeof(IDbConnection).IsAssignableFrom(connectionProperty.PropertyType))
             {
            // Property should be populated with an opened connection instance to the target database.
            IDbConnection newConnection = provider.CreateConnection(databaseName);
            Connections.Add(newConnection);
            connectionProperty.SetValue(sender, newConnection);

            activeConnectionProperties.Add(connectionProperty);
             }
             else if (typeof(Func<IDbConnection>).IsAssignableFrom(connectionProperty.PropertyType))
             {
            // Property should be populated with a delegate that can provide a connection instance when requested.
            connectionProperty.SetValue(
               sender,
               (Func<IDbConnection>)(() => provider.CreateConnection(databaseName)));

            activeConnectionProperties.Add(connectionProperty);
             }
             else if (connectionProperty.PropertyType == typeof(string))
             {
            // Property should be populated with a connection string that can be used to connect to the target database.
            using (var connection = provider.CreateConnection(databaseName))
            {
               connectionProperty.SetValue(
                  sender,
                  connection.ConnectionString);
            }
             }
             else
             {
            throw new InvalidOperationException(
               "A connection was decorated with the DbTestMonkey.Contracts.ConnectionAttribute " +
               "attribute but was not a type of System.Data.IDbConnection or System.Func<System.Data.IDbConnection>.");
             }
        }