コード例 #1
0
ファイル: AzureDB.cs プロジェクト: HolyNoodle/Utility
 public void ResetCache(IDbProcedure procedure)
 {
     if (CacheProvider != null)
     {
         CacheProvider.Cache(procedure, null);
     }
 }
コード例 #2
0
ファイル: AzureDB.cs プロジェクト: HolyNoodle/Utility
        public void ExecuteNonQuery(IDbProcedure procedure)
        {
            var connection = Connect();
            var command    = CreateCommand(procedure);

            command.Connection = connection;
            command.ExecuteNonQuery();
        }
コード例 #3
0
 public async Task ExecuteNonQueryAsync(IDbProcedure procedure)
 {
     using (var connection = await ConnectAsync())
     {
         var command = CreateCommand(procedure);
         command.Connection = connection;
         await command.ExecuteNonQueryAsync();
     }
 }
コード例 #4
0
ファイル: AzureDB.cs プロジェクト: HolyNoodle/Utility
        private SqlCommand CreateCommand(IDbProcedure procedure)
        {
            if (procedure is IDbValidator)
            {
                var result = ((IDbValidator)procedure).Validate(this);
                if (!result.Item1)
                {
                    throw new AzureDbValidationException(result.Item2);
                }
            }

            var command = new SqlCommand()
            {
                CommandText = procedure.Procedure,
                CommandType = System.Data.CommandType.StoredProcedure,
            };

            var parameters = procedure.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            if (parameters != null && parameters.Count() > 0)
            {
                foreach (var property in parameters)
                {
                    var          attributeTab = property.GetCustomAttributes(typeof(DalAttribute), false);
                    DalAttribute attribute    = null;
                    foreach (var a in attributeTab)
                    {
                        if (a is DalAttribute)
                        {
                            attribute = (DalAttribute)a;
                            break;
                        }
                    }
                    if (attribute == null)
                    {
                        continue;
                    }

                    object value = property.GetValue(procedure);
                    if (attribute.Crypter != null)
                    {
                        var instance = Activator.CreateInstance(attribute.Crypter) as ICrypter;
                        value = instance.Crypt(value);
                    }

                    command.Parameters.Add(new SqlParameter(attribute.DBName, value));
                }
            }

            return(command);
        }
コード例 #5
0
ファイル: AzureDB.cs プロジェクト: HolyNoodle/Utility
        public List <T> Execute <T>(IDbProcedure procedure) where T : IDalObject
        {
            if (UseCache)
            {
                var cacheResult = CacheProvider.Get <List <T> >(procedure);
                if (cacheResult != null)
                {
                    return(cacheResult);
                }
            }
            var connection = Connect();

            var command = CreateCommand(procedure);
            var result  = new ConcurrentBag <T>();
            var values  = new List <List <DalObjectValue> >();

            command.Connection = connection;

            using (var reader = command.ExecuteReader())
            {
                if (reader == null)
                {
                    throw new Exception("DataReader is null and can't be browse");
                }

                while (reader.Read())
                {
                    var value = new List <DalObjectValue>();
                    for (var i = 0; i < reader.FieldCount; ++i)
                    {
                        value.Add(new DalObjectValue
                        {
                            Key   = reader.GetName(i),
                            Value = reader.GetValue(i)
                        });
                    }
                    values.Add(value);
                }
            }

            Parallel.ForEach(values, (v) =>
            {
                result.Add(Load <T>(v));
            });

            if (UseCache)
            {
                CacheProvider.Cache(procedure, result.ToList());
            }
            return(result.ToList());
        }
コード例 #6
0
        private string GetHash(IDbProcedure procedure)
        {
            var hash = new StringBuilder(procedure.GetType().FullName.ToLower());

            foreach (var property in procedure.GetType().GetProperties())
            {
                if (property.GetCustomAttribute <DalAttribute>() != null)
                {
                    hash.Append("_");
                    hash.Append(property.GetValue(procedure).ToString().ToLower());
                }
            }
            return(hash.ToString());
        }
コード例 #7
0
        public T Get <T>(IDbProcedure key)
        {
            var hash = GetHash(key);

            if (CacheContainerMap.ContainsKey(hash))
            {
                var container   = CacheContainerMap[hash];
                var elapsedTime = DateTime.Now - container.LastAccess;
                if (elapsedTime.TotalMinutes >= 5)
                {
                    container.LastAccess = DateTime.MinValue;
                    return((T)(object)null);
                }

                return((T)container.Value);
            }
            return((T)(object)null);
        }
コード例 #8
0
        public void Cache(IDbProcedure key, object value)
        {
            var attribute = key.GetType().GetTypeInfo().GetCustomAttribute <CacheObject>();

            if (attribute != null)
            {
                foreach (var procedure in CacheContainerMap)
                {
                    if (attribute.IsImpacted(procedure.Value.Procedure))
                    {
                        procedure.Value.LastAccess = DateTime.MinValue;
                    }
                }
            }

            if (value == null || (attribute != null && attribute.DoNotCache))
            {
                return;
            }

            var hash = GetHash(key);

            if (CacheContainerMap.ContainsKey(hash))
            {
                var container = CacheContainerMap[hash];
                container.LastAccess = DateTime.Now;
                container.Value      = value;
            }
            else
            {
                var newContainer = new CacheContainer {
                    LastAccess = DateTime.Now, Value = value, Procedure = key
                };
                var container = CacheContainerMap.GetOrAdd(hash, newContainer);
                if (container != null && container.LastAccess < newContainer.LastAccess)
                {
                    Cache(key, value);
                }
            }
        }
コード例 #9
0
 public object ExecuteScalar(IDbProcedure procedure)
 {
     if (UseCache)
     {
         var cacheResult = CacheProvider.Get <object>(procedure);
         if (cacheResult != null)
         {
             return(cacheResult);
         }
     }
     using (var connection = Connect())
     {
         var command = CreateCommand(procedure);
         command.Connection = connection;
         var result = command.ExecuteScalar();
         if (UseCache)
         {
             CacheProvider.Cache(procedure, result);
         }
         return(result);
     }
 }
コード例 #10
0
        public async Task <object> ExecuteScalarAsync(IDbProcedure procedure)
        {
            if (UseCache)
            {
                var cacheResult = CacheProvider.Get <object>(procedure);
                if (cacheResult != null)
                {
                    return(cacheResult);
                }
            }
            using (var connection = await ConnectAsync())
            {
                var command = CreateCommand(procedure);
                command.Connection = connection;
                var result = await command.ExecuteScalarAsync();

                if (UseCache)
                {
                    CacheProvider.Cache(procedure, result);
                }
                return(result);
            }
        }
コード例 #11
0
        public List <T> Execute <T>(IDbProcedure procedure) where T : IDalObject
        {
            if (UseCache)
            {
                var cacheResult = CacheProvider.Get <List <T> >(procedure);
                if (cacheResult != null)
                {
                    return(cacheResult);
                }
            }
            var result = new ConcurrentBag <T>();
            var values = new List <List <DalObjectValue> >();

            using (var connection = Connect())
            {
                var command = CreateCommand(procedure);
                command.Connection = connection;
                try
                {
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader == null)
                        {
                            throw new Exception("DataReader is null and can't be browse");
                        }

                        if (reader.GetSchemaTable() == null)
                        {
                            return(result.ToList());
                        }

                        var columnStructure = new List <string>();

                        foreach (DataRow r in reader.GetSchemaTable().Rows)
                        {
                            columnStructure.Add(r["ColumnName"].ToString());
                        }

                        while (reader.Read())
                        {
                            var value = new List <DalObjectValue>();
                            foreach (var c in columnStructure)
                            {
                                value.Add(new DalObjectValue
                                {
                                    Key   = c,
                                    Value = reader.GetValue(reader.GetOrdinal(c))
                                });
                            }
                            values.Add(value);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            Parallel.ForEach(values, (v) =>
            {
                result.Add(Load <T>(v));
            });

            if (UseCache)
            {
                CacheProvider.Cache(procedure, result.ToList());
            }
            return(result.ToList());
        }
コード例 #12
0
 public bool IsImpacted(IDbProcedure procedure)
 {
     return(Types.Any(t => t.FullName == procedure.GetType().FullName));
 }