/// <summary>
        /// Returns DefinedType object from cache.  If definedType does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DefinedTypeCache Read(int id)
        {
            string cacheKey = DefinedTypeCache.CacheKey(id);

            ObjectCache      cache       = MemoryCache.Default;
            DefinedTypeCache definedType = cache[cacheKey] as DefinedTypeCache;

            if (definedType != null)
            {
                return(definedType);
            }
            else
            {
                Rock.Model.DefinedTypeService definedTypeService = new Rock.Model.DefinedTypeService();
                Rock.Model.DefinedType        definedTypeModel   = definedTypeService
                                                                   .Queryable("DefinedValues")
                                                                   .Where(t => t.Id == id)
                                                                   .FirstOrDefault();

                if (definedTypeModel != null)
                {
                    definedType = CopyModel(definedTypeModel);

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set(cacheKey, definedType, cachePolicy);
                    cache.Set(definedType.Guid.ToString(), definedType.Id, cachePolicy);

                    return(definedType);
                }
                else
                {
                    return(null);
                }
            }
        }
        /// <summary>
        /// Reads the specified GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public static DefinedTypeCache Read(Guid guid)
        {
            ObjectCache cache    = MemoryCache.Default;
            object      cacheObj = cache[guid.ToString()];

            if (cacheObj != null)
            {
                return(Read((int)cacheObj));
            }
            else
            {
                Rock.Model.DefinedTypeService definedTypeService = new Rock.Model.DefinedTypeService();
                Rock.Model.DefinedType        definedTypeModel   = definedTypeService
                                                                   .Queryable("DefinedValues")
                                                                   .Where(t => t.Guid == guid)
                                                                   .FirstOrDefault();

                if (definedTypeModel != null)
                {
                    var definedType = new DefinedTypeCache(definedTypeModel);

                    var cachePolicy = new CacheItemPolicy();
                    cache.Set(DefinedTypeCache.CacheKey(definedType.Id), definedType, cachePolicy);
                    cache.Set(definedType.Guid.ToString(), definedType.Id, cachePolicy);

                    return(definedType);
                }
                else
                {
                    return(null);
                }
            }
        }
        /// <summary>
        /// Copies the model.
        /// </summary>
        /// <param name="definedTypeModel">The defined type model.</param>
        /// <returns></returns>
        public static DefinedTypeCache CopyModel(Rock.Model.DefinedType definedTypeModel)
        {
            DefinedTypeCache definedType = new DefinedTypeCache(definedTypeModel);

            definedType.definedValueIds = definedTypeModel.DefinedValues
                                          .Select(v => v.Id).ToList();

            return(definedType);
        }
        /// <summary>
        /// Reads the specified defined type model.
        /// </summary>
        /// <param name="definedTypeModel">The defined type model.</param>
        /// <returns></returns>
        public static DefinedTypeCache Read(Rock.Model.DefinedType definedTypeModel)
        {
            string cacheKey = DefinedTypeCache.CacheKey(definedTypeModel.Id);

            ObjectCache      cache       = MemoryCache.Default;
            DefinedTypeCache definedType = cache[cacheKey] as DefinedTypeCache;

            if (definedType != null)
            {
                return(definedType);
            }
            else
            {
                definedType = new DefinedTypeCache(definedTypeModel);

                var cachePolicy = new CacheItemPolicy();
                cache.Set(cacheKey, definedType, cachePolicy);
                cache.Set(definedType.Guid.ToString(), definedType.Id, cachePolicy);

                return(definedType);
            }
        }
 private DefinedTypeCache(Rock.Model.DefinedType model) : base(model)
 {
 }