Beispiel #1
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);
        }