/// <summary> /// 获取给实体属性定义的所有验证器序列,这些验证器通过 <see cref="MetadataTypeAttribute"/> 特性所指定的类型列定义。 /// </summary> /// <param name="property">要获取验证器的实体属性。</param> /// <exception cref="ArgumentNullException"><paramref name="property"/> 参数为 null。</exception> /// <returns>一个 <see cref="ValidationAttribute"/> 的序列。</returns> public static IEnumerable <ValidationAttribute> GetValidations(IProperty property) { Guard.ArgumentNull(property, "property"); var entityType = property.EntityType; var propertyName = property.Name; return(locker.LockRead(() => { Dictionary <string, List <ValidationAttribute> > pdic = null; if (!propertyValidations.TryGetValue(entityType, out pdic)) { locker.LockWrite(() => { pdic = propertyValidations.TryGetValue(entityType, () => GetPropertyValidations(entityType)); }); } List <ValidationAttribute> attrs = null; if (!pdic.TryGetValue(propertyName, out attrs)) { locker.LockWrite(() => { attrs = pdic.TryGetValue(propertyName, () => new List <ValidationAttribute>()); }); } return attrs; })); }
/// <summary> /// 尝试获取指定缓存键的对象,如果没有则使用工厂函数添加对象到缓存中。 /// </summary> /// <typeparam name="T">缓存对象的类型。</typeparam> /// <param name="cacheKey">用于引用对象的缓存键。</param> /// <param name="factory">用于添加缓存对象的工厂函数。</param> /// <param name="expiration">判断对象过期的对象。</param> /// <returns></returns> public T TryGet <T>(string cacheKey, System.Func <T> factory, System.Func <ICacheItemExpiration> expiration = null) { try { using (var client = manager.GetClient()) { var set = client.As <T>(); T value = default(T); locker.LockRead(() => { if (!set.ContainsKey(cacheKey)) { if (factory != null) { locker.LockWrite(() => { value = factory(); DateTime?time = null; if (expiration != null) { var relative = expiration() as RelativeTime; if (relative != null) { time = DateTime.Now + relative.Expiration; } } if (time == null) { client.Set <T>(cacheKey, value); } else { client.Set <T>(cacheKey, value, time.Value); } }); } } else { value = set.GetValue(cacheKey); } }); return(value); } } catch (Exception exp) { throw new InvalidOperationException("无法完成缓存操作。", exp); } }