コード例 #1
0
        /// <summary>
        /// Adds an object to the cache. It is upto the implementer on how long it wishes to keep the item in cache.
        /// Typically it will be govered by the TimeToExpire property being set.
        /// </summary>
        /// <param name="key">Key which will be used to retrieve the same object.</param>
        /// <param name="value">The instance of the object that should be stored in cache.</param>
        public void Add(string key, object value)
        {
            Asserter.AssertIsNotNullOrEmptyString("key", key);
            Asserter.AssertIsNotNull("value", value);

            LocalCacheStorage.Add(key, value);
        }
コード例 #2
0
        /// <summary>
        /// Executes the specified command text as a non-query returning the number of records affected. If you need
        /// the records affected, be sure that SET NOCOUNT OFF is enabled so it can return the number of records
        /// affected, otherwise, it will return -1.
        /// </summary>
        /// <param name="dbContext">The IDbContext to use.</param>
        /// <param name="commandText">The command text to execute.</param>
        /// <param name="parameters">The parameters to use during execution.</param>
        /// <param name="commandType">The type of command the command text is.</param>
        /// <returns>Returns a System.Int32 based on the number of record affected during the specified query.</returns>
        public int ExecuteNonQuery(string commandText, Dictionary <string, object> parameters, CommandType commandType, bool useReturnForCount)
        {
            Asserter.AssertIsNotNullOrEmptyString("commandText", commandText);
            Asserter.AssertIsNotNull("parameters", parameters);

            int       recordsAffected = 0;
            DbContext dbContext       = (DbContext)_Context;

            try
            {
                if (dbContext.Database.Connection.State != System.Data.ConnectionState.Open)
                {
                    dbContext.Database.Connection.Open();
                }

                using (DbCommand dbCommand = dbContext.Database.Connection.CreateCommand())
                {
                    dbCommand.CommandTimeout = CommonSettingsConfigSection.GetSection().DbContextCommandTimeout;
                    dbCommand.CommandText    = commandText;
                    dbCommand.CommandType    = commandType;

                    foreach (string key in parameters.Keys)
                    {
                        DbParameter dbParameter = dbCommand.CreateParameter();
                        dbParameter.ParameterName = key;
                        dbParameter.Value         = parameters[key];
                        dbCommand.Parameters.Add(dbParameter);
                    }

                    // Uses the RETURN value of the stored procedure for the count, otherwise,
                    // uses the total @@ROWCOUNT, will return -1 if SET NOCOUNT ON is specified. -cbb
                    if (useReturnForCount)
                    {
                        DbParameter dbParameter = dbCommand.CreateParameter();
                        dbParameter.Direction     = ParameterDirection.ReturnValue;
                        dbParameter.ParameterName = "returnValue";
                        dbCommand.Parameters.Add(dbParameter);
                        dbCommand.ExecuteNonQuery();

                        if (dbParameter != null && dbParameter.Value != DBNull.Value)
                        {
                            recordsAffected = (int)dbParameter.Value;
                        }
                    }
                    else
                    {
                        recordsAffected = dbCommand.ExecuteNonQuery();
                    }
                }
            }
            finally
            {
                if (dbContext.Database.Connection != null && dbContext.Database.Connection.State != ConnectionState.Closed)
                {
                    dbContext.Database.Connection.Close();
                }
            }

            return(recordsAffected);
        }
コード例 #3
0
        /// <summary>
        /// Adds an object to the cache and uses generic type.
        /// </summary>
        /// <param name="timeout">Time the object should be kept, after which it can be disposed.</param>
        /// <param name="key">Key which will be used to retrieve the same object.</param>
        /// <param name="value">The instance of the object that should be stored in cache.</param>
        public void Add <T>(TimeSpan timeout, string key, T value)
        {
            Asserter.AssertIsNotNullOrEmptyString("key", key);
            Asserter.AssertIsNotNull("value", value);

            LocalCacheStorage.Add(key, value);
        }
コード例 #4
0
        /// <summary>
        /// Retrives an object from cache using the specified key.
        /// </summary>
        /// <param name="key">Key which will be used to retrieve the same object.</param>
        /// <returns>Object retrieved from the cache based on the key specified.</returns>
        public object Get(string key)
        {
            Asserter.AssertIsNotNullOrEmptyString("key", key);
            object value = null;
            object returnValue;

            if (LocalCacheStorage.TryGetValue(key, out returnValue))
            {
                value = returnValue;
            }

            return(value);
        }
コード例 #5
0
        /// <summary>
        /// Updates an object in cache.
        /// </summary>
        /// <param name="key">The key of the item to update.</param>
        /// <param name="value">The value to update it to.</param>
        public void Update(TimeSpan timeout, string key, object value)
        {
            Asserter.AssertIsNotNullOrEmptyString("key", key);
            Asserter.AssertIsNotNull("value", value);
            object returnValue;

            if (LocalCacheStorage.TryGetValue(key, out returnValue))
            {
                LocalCacheStorage[key] = value;
            }
            else
            {
                throw new CacheKeyNotFoundException(key, CacheStoreName, GetType());
            }
        }
コード例 #6
0
        // TODO: Utilize IUnitOfWork, and create a DbTransaction manager for IUnitOfWork. This applies to all of the Execute.*(.*) methods below.
        // TODO: RE-FACTOR Executes to use a single private method for executing the actual query to keep from all of this duplicate connection code.
        public IEnumerable <T> Execute <T>(string commandText, Dictionary <string, object> parameters, CommandType commandType)
        {
            Asserter.AssertIsNotNullOrEmptyString("commandText", commandText);
            Asserter.AssertIsNotNull("parameters", parameters);
            List <T>  results   = new List <T>();
            DbContext dbContext = (DbContext)_Context;

            try
            {
                if (dbContext.Database.Connection.State != System.Data.ConnectionState.Open)
                {
                    dbContext.Database.Connection.Open();
                }

                using (DbCommand dbCommand = dbContext.Database.Connection.CreateCommand())
                {
                    dbCommand.CommandTimeout = CommonSettingsConfigSection.GetSection().DbContextCommandTimeout;
                    dbCommand.CommandText    = commandText;
                    dbCommand.CommandType    = commandType;

                    foreach (string key in parameters.Keys)
                    {
                        DbParameter dbParameter = dbCommand.CreateParameter();
                        dbParameter.ParameterName = key;
                        dbParameter.Value         = parameters[key];
                        dbCommand.Parameters.Add(dbParameter);
                    }

                    using (DbDataReader dataReader = dbCommand.ExecuteReader())
                    {
                        if (dataReader.HasRows)
                        {
                            Mapper.CreateMap <IDataReader, T>().IgnoreAllNonExisting();
                            results = Mapper.Map <IDataReader, List <T> >(dataReader);
                        }
                    }
                }
            }
            finally
            {
                if (dbContext.Database.Connection != null && dbContext.Database.Connection.State != ConnectionState.Closed)
                {
                    dbContext.Database.Connection.Close();
                }
            }

            return(results);
        }
コード例 #7
0
        /// <summary>
        /// Checks to see if the specified parameter name is already in the collection, if
        /// that's the case, we generated a new name with a number at the end, ie: ParameterName0, ParameterName1, ParameterName2.
        /// </summary>
        /// <param name="parameterName">The specified name of the parameter.</param>
        /// <returns>String containing the name of the parameter to use when adding it to the collection.</returns>
        private string getParameterName(string parameterName)
        {
            Asserter.AssertIsNotNullOrEmptyString("parameterName", parameterName);

            if (Contains(parameterName))
            {
                int parameterIndex = 0;
                if (_innerParameterIndexList.Count > 0 && _innerParameterIndexList.ContainsKey(parameterName))
                {
                    _innerParameterIndexList[parameterName]++;
                }
                else
                {
                    _innerParameterIndexList[parameterName] = 0;
                }
                parameterIndex = _innerParameterIndexList[parameterName];
                // Change the parameter's name to ParameterName + index (with the specified format string.)
                return(string.Format(IndexedParameterFormatString, parameterName, parameterIndex));
            }
            return(parameterName);
        }
コード例 #8
0
        public object ExecuteScalar(string commandText, Dictionary <string, object> parameters, CommandType commandType)
        {
            Asserter.AssertIsNotNullOrEmptyString("commandText", commandText);
            Asserter.AssertIsNotNull("parameters", parameters);

            object    scalarValue = null;
            DbContext dbContext   = (DbContext)_Context;

            try
            {
                if (dbContext.Database.Connection.State != System.Data.ConnectionState.Open)
                {
                    dbContext.Database.Connection.Open();
                }

                using (DbCommand dbCommand = dbContext.Database.Connection.CreateCommand())
                {
                    dbCommand.CommandTimeout = CommonSettingsConfigSection.GetSection().DbContextCommandTimeout;
                    dbCommand.CommandText    = commandText;
                    dbCommand.CommandType    = commandType;

                    foreach (string key in parameters.Keys)
                    {
                        DbParameter dbParameter = dbCommand.CreateParameter();
                        dbParameter.ParameterName = key;
                        dbParameter.Value         = parameters[key];
                        dbCommand.Parameters.Add(dbParameter);
                    }
                    scalarValue = dbCommand.ExecuteScalar();
                }
            }
            finally
            {
                if (dbContext.Database.Connection != null && dbContext.Database.Connection.State != ConnectionState.Closed)
                {
                    dbContext.Database.Connection.Close();
                }
            }
            return(scalarValue);
        }
コード例 #9
0
        public SearchResults <T> ExecuteWithPaging(string commandText, Dictionary <string, object> parameters, CommandType commandType, PageSort pageSort)
        {
            Asserter.AssertIsNotNullOrEmptyString("commandText", commandText);
            Asserter.AssertIsNotNull("parameters", parameters);

            SearchResults <T> searchResults = new SearchResults <T>();

            DbContext dbContext = (DbContext)_Context;

            try
            {
                if (dbContext.Database.Connection.State != System.Data.ConnectionState.Open)
                {
                    dbContext.Database.Connection.Open();
                }

                using (DbCommand dbCommand = dbContext.Database.Connection.CreateCommand())
                {
                    dbCommand.CommandTimeout = CommonSettingsConfigSection.GetSection().DbContextCommandTimeout;
                    dbCommand.CommandText    = commandText;
                    dbCommand.CommandType    = commandType;

                    if (pageSort != null)
                    {
                        dbCommand.Parameters.AddPageSort(pageSort);
                    }

                    foreach (string key in parameters.Keys)
                    {
                        DbParameter dbParameter = dbCommand.CreateParameter();
                        dbParameter.ParameterName = key;
                        dbParameter.Value         = parameters[key];
                        dbCommand.Parameters.Add(dbParameter);
                    }

                    using (DbDataReader dataReader = dbCommand.ExecuteReader())
                    {
                        if (dataReader.HasRows)
                        {
                            List <T> results = new List <T>();
                            Mapper.CreateMap <IDataReader, T>().IgnoreAllNonExisting();
                            results = Mapper.Map <IDataReader, List <T> >(dataReader);
                            if (results != null)
                            {
                                searchResults.Results = results;
                            }
                            else
                            {
                                searchResults.Results = new List <T>();
                            }
                            if (dataReader.NextResult())
                            {
                                if (dataReader.Read())
                                {
                                    searchResults.RowsAffected = Convert.ToInt32(dataReader[0]);
                                }
                                else
                                {
                                    throw new DataException("The query or stored procedure for a paged query must return TWO record sets, the record set of the actual records in the first record set and the number of total rows as a scalar value in the second record set.");
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (dbContext.Database.Connection != null && dbContext.Database.Connection.State != ConnectionState.Closed)
                {
                    dbContext.Database.Connection.Close();
                }
            }

            return(searchResults);
        }
コード例 #10
0
 public static T FirstOrDefaultValue <T>(this IEnumerable <FilterParameter> parameters, string parameterName)
 {
     Asserter.AssertIsNotNullOrEmptyString("parameterName", parameterName);
     return(parameters.FirstOrDefault(parameterName).Value <T>());
 }
コード例 #11
0
 /// <summary>
 /// Returns all cache keys that start with the specified cache key.
 /// </summary>
 /// <param name="key">Key which will be used to check whether the cache contains it or not.</param>
 /// <returns>IEnumerable<string> </string> of cache keys that have the specified key prefix.</returns>
 public IEnumerable <string> SearchKeys(string key)
 {
     Asserter.AssertIsNotNullOrEmptyString("key", key);
     return(LocalCacheStorage.Keys.Where(k => k.StartsWith(key)).ToList());
 }
コード例 #12
0
 /// <summary>
 /// Checks whether the cache contains the specified key or not.
 /// </summary>
 /// <param name="key">Key which will be used to check whether the cache contains it or not.</param>
 /// <returns>true if the cache contains the specified key; otherwise, false.</returns>
 public bool ContainsKey(string key)
 {
     Asserter.AssertIsNotNullOrEmptyString("key", key);
     return(LocalCacheStorage.ContainsKey(key));
 }
コード例 #13
0
 /// <summary>
 /// Removes an object from the cache using the specified key.
 /// </summary>
 /// <param name="key">Key which will be used to remove the same object from cache.</param>
 public void Remove(string key)
 {
     Asserter.AssertIsNotNullOrEmptyString("key", key);
     LocalCacheStorage.Remove(key);
 }
コード例 #14
0
 public bool Contains(string parameterName)
 {
     Asserter.AssertIsNotNullOrEmptyString("parameterName", parameterName);
     return(this[parameterName] != null);
 }