コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="personalId"></param>
        /// <param name="allowNull"></param>
        /// <param name="keys"></param>
        /// <exception cref="Exception"></exception>
        /// <exception cref="NullReferenceException"></exception>
        /// <returns></returns>
        public static T Get <T>(string personalId, bool allowNull = false, params object[] keys) where T : BaseEntity, new()
        {
            var cache = new PersonalCacheStruct <T>();
            T   result;

            if (cache.TryFindKey(personalId, out result, keys) != LoadingStatus.Success)
            {
                throw new Exception(string.Format("Entity {0} load  personalId:{1} error", typeof(T).Name, personalId));
            }
            if (!allowNull && result == null)
            {
                throw new NullReferenceException(string.Format("Not found entity {0} personalId:{1}.", typeof(T).Name, personalId));
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="personalId"></param>
        /// <param name="createFactory"></param>
        /// <param name="keys"></param>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public static T GetOrAdd <T>(string personalId, Lazy <T> createFactory, params object[] keys) where T : BaseEntity, new()
        {
            var cache = new PersonalCacheStruct <T>();
            T   result;

            if (cache.TryFindKey(personalId, out result, keys) != LoadingStatus.Success)
            {
                throw new Exception(string.Format("Entity {0} load  personalId:{1} error", typeof(T).Name, personalId));
            }
            if (result == default(T))
            {
                result = createFactory.Value;
                cache.Add(result);
            }
            return(result);
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="personalId"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static T Get <T>(object personalId, params object[] keys) where T : BaseEntity, new()
        {
            var           cache = new PersonalCacheStruct <T>();
            T             result;
            LoadingStatus status;

            if ((status = cache.TryFindKey(personalId.ToString(), out result, keys)) != LoadingStatus.Success)
            {
                throw new Exception(string.Format("Entity {0} load  personalId:{1} error:{2}", typeof(T).Name, personalId, status));
            }
            if (result == null)
            {
                throw new NullReferenceException(string.Format("Not found entity {0} personalId:{1}.", typeof(T).Name, personalId));
            }
            return(result);
        }
コード例 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="personalId"></param>
        /// <param name="createFactory"></param>
        /// <param name="keys"></param>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public static T GetOrAdd <T>(string personalId, Lazy <T> createFactory, params object[] keys) where T : BaseEntity, new()
        {
            var primaryKeys = EntitySchemaSet.Get <T>().Keys;

            if (primaryKeys.Length > 1 && keys.Length != primaryKeys.Length)
            {
                throw new ArgumentNullException("keys", string.Format("Set args:\"{0}\" value error.", string.Join(",", primaryKeys)));
            }
            var           cache = new PersonalCacheStruct <T>();
            T             result;
            LoadingStatus status;

            if ((status = cache.TryFindKey(personalId, out result, keys)) != LoadingStatus.Success)
            {
                throw new Exception(string.Format("Entity {0} load  personalId:{1} error:{2}", typeof(T).Name, personalId, status));
            }
            if (result == default(T))
            {
                result = createFactory.Value;
                cache.Add(result);
            }
            return(result);
        }