Exemplo n.º 1
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());
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
0
 public bool IsImpacted(IDbProcedure procedure)
 {
     return(Types.Any(t => t.FullName == procedure.GetType().FullName));
 }