public void ResetCache(IDbProcedure procedure) { if (CacheProvider != null) { CacheProvider.Cache(procedure, null); } }
public void ExecuteNonQuery(IDbProcedure procedure) { var connection = Connect(); var command = CreateCommand(procedure); command.Connection = connection; command.ExecuteNonQuery(); }
public async Task ExecuteNonQueryAsync(IDbProcedure procedure) { using (var connection = await ConnectAsync()) { var command = CreateCommand(procedure); command.Connection = connection; await command.ExecuteNonQueryAsync(); } }
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 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()); }
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()); }
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); }
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 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); } }
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); } }
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()); }
public bool IsImpacted(IDbProcedure procedure) { return(Types.Any(t => t.FullName == procedure.GetType().FullName)); }