public static DbSqlQuery <TEntity> ApplyTracking <TEntity>(this DbSqlQuery <TEntity> query, bool tracking) where TEntity : class { return(tracking ? query : query.AsNoTracking()); }
/// <summary> /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached yet, the query /// is materialized and cached before being returned. /// </summary> /// <typeparam name="T">The generic type of the query.</typeparam> /// <param name="query">The query to cache the result.</param> /// <param name="policy">The eviction details for the cached result.</param> /// <param name="tags">(Optional) The tag list that can be used to expire cached result.</param> /// <returns>The result of the query.</returns> public static IEnumerable <T> FromCache <T>(this DbSqlQuery <T> query, CacheItemPolicy policy, params string[] tags) where T : class { if (!QueryCacheManager.IsEnabled) { return(query.AsNoTracking().ToList()); } var key = QueryCacheManager.GetCacheKey(query, tags); var item = QueryCacheManager.Get <List <T> >(key); if (item == null) { item = query.AsNoTracking().ToList(); item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item; QueryCacheManager.AddCacheTag(key, tags); } return((IEnumerable <T>)item); }
/// <summary> /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached yet, the query /// is materialized and cached before being returned. /// </summary> /// <typeparam name="T">The generic type of the query.</typeparam> /// <param name="query">The query to cache the result.</param> /// <param name="policy">The eviction details for the cached result.</param> /// <param name="tags">(Optional) The tag list that can be used to expire cached result.</param> /// <returns>The result of the query.</returns> public static IEnumerable <T> FromCache <T>(this DbSqlQuery <T> query, CacheItemPolicy policy, params string[] tags) where T : class { if (!QueryCacheManager.IsEnabled) { return(query.AsNoTracking().ToList()); } var key = QueryCacheManager.GetCacheKey(query, tags); var item = QueryCacheManager.Get <List <T> >(key); if (item == null) { using (var handler = new QueryCacheItemTracker().Initialize(query)) { item = query.AsNoTracking().ToList(); item = QueryCacheManager.AddOrGetExisting(key, item, policy) ?? item; QueryCacheManager.AddCacheTag(handler, key, tags); QueryCacheManager.AddCacheTag(key, typeof(T).Name + QueryCacheManager.CacheTypeSuffix); } } return((IEnumerable <T>)item); }
/// <summary> /// Return the result of the <paramref name="query" /> from the cache. If the query is not cached yet, the query /// is materialized and cached before being returned. /// </summary> /// <typeparam name="T">The generic type of the query.</typeparam> /// <param name="query">The query to cache the result.</param> /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param> /// <param name="tags">(Optional) The tag list that can be used to expire cached result.</param> /// <returns>The result of the query.</returns> public static IEnumerable <T> FromCache <T>(this DbSqlQuery <T> query, DateTimeOffset absoluteExpiration, params string[] tags) where T : class { if (!QueryCacheManager.IsEnabled) { return(query.AsNoTracking().ToList()); } var key = QueryCacheManager.GetCacheKey(query, tags); var item = QueryCacheManager.Get <List <T> >(key); if (item == null) { using (var handler = new QueryCacheItemTracker().Initialize(query)) { item = query.AsNoTracking().ToList(); item = QueryCacheManager.AddOrGetExisting(key, item, absoluteExpiration) ?? item; QueryCacheManager.AddCacheTag(handler, key, tags); } } return((IEnumerable <T>)item); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { Type objectType = validationContext.ObjectType; if (objectType.BaseType != typeof(object)) { objectType = objectType.BaseType; } Console.Write(objectType.Name); string objectName = objectType.Name; if (!string.IsNullOrEmpty(validationContext.MemberName)) { PropertyInfo currentProperty = objectType.GetProperty(validationContext.MemberName); if (currentProperty != null && currentProperty.GetGetMethod().IsVirtual) { return(null); } } Context context = new Context(); DbSet set = context.Set(objectType); string schemaName = "dbo"; if (objectType.GetCustomAttributes(typeof(TableAttribute), true).Length > 0) { TableAttribute tableAttribute = objectType.GetCustomAttributes(typeof(TableAttribute), true)[0] as TableAttribute; schemaName = tableAttribute.Schema; } StringBuilder queryBuilder = new StringBuilder("SELECT "); PropertyInfo[] propertyInfos = validationContext.ObjectType.GetProperties(BindingFlags.Instance | BindingFlags.Public); bool hasAddedFirstField = false; for (int i = 0; i < propertyInfos.Length; i++) { if (propertyInfos[i].GetGetMethod().IsVirtual) { continue; } if (hasAddedFirstField) { queryBuilder.Append(", "); } hasAddedFirstField = true; queryBuilder.Append($"[{propertyInfos[i].Name}]"); } queryBuilder.Append($" FROM [{schemaName}].[{objectName}] WHERE "); List <object> parameters = new List <object>(); for (int i = 0; i < UniqueFields.Length; i++) { if (i != 0) { queryBuilder.Append(" AND "); } object propertyValue = validationContext.ObjectType.GetProperty(UniqueFields[i]).GetValue(validationContext.ObjectInstance); if (propertyValue != null) { queryBuilder.Append(string.Format("[{0}]=@{1}", UniqueFields[i], parameters.Count)); parameters.Add(new SqlParameter(string.Format("@{0}", parameters.Count), propertyValue)); } else { queryBuilder.Append(string.Format("[{0}] IS NULL", UniqueFields[i])); } } object primaryKey = validationContext.ObjectType.GetProperty($"{objectName}ID").GetValue(validationContext.ObjectInstance); if (primaryKey != null) { queryBuilder.Append($" AND {objectName}ID != @{parameters.Count}"); parameters.Add(new SqlParameter($"@{parameters.Count}", primaryKey)); } DbSqlQuery dbRawSqlQuery = set.SqlQuery(queryBuilder.ToString(), parameters.ToArray()); bool isUnique = true; foreach (var item in dbRawSqlQuery.AsNoTracking()) { isUnique = false; break; } if (!isUnique) { StringBuilder errorBuilder = new StringBuilder(); for (int i = 0; i < UniqueFields.Length; i++) { if (i != 0) { errorBuilder.Append(", "); } errorBuilder.Append(UniqueFields[i]); } errorBuilder.Append(" must be unique"); return(new ValidationResult(errorBuilder.ToString(), UniqueFields)); } return(null); }