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()); }
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); }
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); } } }
public bool IsImpacted(IDbProcedure procedure) { return(Types.Any(t => t.FullName == procedure.GetType().FullName)); }