/// <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; })); }
void ICommandTracker.Write(IDbCommand command, TimeSpan period) { CreateDirectory(); locker.LockWrite(() => { var fileName = Path.Combine(logPath, DateTime.Today.ToString("yyyy-MM-dd") + ".log"); using (var stream = new StreamWriter(fileName, true)) { stream.WriteLine("time: " + DateTime.Now); stream.WriteLine("sql: " + command.Output()); stream.WriteLine("timer(s): " + period); stream.WriteLine("==========================================================="); stream.WriteLine(); } }); }
/// <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); } }
/// <summary> /// 将抛出的异常写入到日志记录器。 /// </summary> /// <param name="logType">信息类别。</param> /// <param name="message">异常的说明信息。</param> /// <param name="exception">应用程序异常。</param> private void Write(string logType, object message, Exception exception) { var content = GetLogContent(message, exception); var fileName = CreateLogFileName(logType); locker.LockWrite(() => { using (var writer = new StreamWriter(fileName, true, Encoding.Default)) { writer.WriteLine(content); writer.Flush(); } }); }
/// <summary> /// 添加类型与缓存键的映射。 /// </summary> /// <param name="types"></param> /// <param name="cacheKey"></param> public static void AddKeys(List <Type> types, string cacheKey) { foreach (var ctype in types) { var list = cache.GetOrAdd(ctype, t => new List <string>()); locker.LockWrite(() => { if (!list.Contains(cacheKey)) { list.Add(cacheKey); } }); } }
/// <summary> /// 将抛出的异常写入到日志记录器。 /// </summary> /// <param name="logType">信息类别。</param> /// <param name="message">异常的说明信息。</param> /// <param name="exception">应用程序异常。</param> private void Write(string logType, object message, Exception exception) { locker.LockWrite(() => { var fileName = CreateLogFileName(logType); using (var writer = new StreamWriter(fileName, true, Encoding.GetEncoding(0))) { writer.WriteLine("Time: " + DateTime.Now); writer.WriteLine(); if (message != null) { writer.WriteLine(message); } writer.WriteLine(); if (exception != null) { writer.WriteLine("--Exceptions--"); #if !NET35 var aggExp = exception as AggregateException; if (aggExp != null && aggExp.InnerExceptions.Count > 0) { foreach (var e in aggExp.InnerExceptions) { WriteException(writer, e); } } else { WriteException(writer, exception); } #else WriteException(writer, exception); #endif } writer.WriteLine("*****************************************************************"); writer.Flush(); } }); }
/// <summary> /// 添加类型与缓存键的映射。 /// </summary> /// <param name="types"></param> /// <param name="cacheKey"></param> public static void AddKeys(List <Type> types, string cacheKey) { var cacheMgr = CacheManagerFactory.CreateManager(); var hashSet = cacheMgr.GetHashSet <string, List <string> >(ROOT_KEY); foreach (var ctype in types) { locker.LockWrite(() => { var keys = hashSet.TryGet(ctype.Name, () => new List <string>()); if (!keys.Contains(cacheKey)) { keys.Add(cacheKey); } hashSet.Add(ctype.Name, keys); }); } }