Exemple #1
0
        public static List <T> RetrieveList <T>(List <PrimaryKey> ids)
            where T : Entity
        {
            using (HeavyProfiler.Log("DBRetrieve", () => "List<{0}>".FormatWith(typeof(T).TypeName())))
            {
                if (ids == null)
                {
                    throw new ArgumentNullException("ids");
                }
                List <PrimaryKey>          remainingIds;
                Dictionary <PrimaryKey, T> result = null;
                if (EntityCache.Created)
                {
                    result = ids.Select(id => EntityCache.Get <T>(id)).NotNull().ToDictionary(a => a.Id);
                    if (result.Count == 0)
                    {
                        remainingIds = ids;
                    }
                    else
                    {
                        remainingIds = ids.Where(id => !result.ContainsKey(id)).ToList();
                    }
                }
                else
                {
                    remainingIds = ids;
                }

                if (remainingIds.Count > 0)
                {
                    var retrieved = RetrieveFromDatabaseOrCache <T>(remainingIds).ToDictionary(a => a.Id);

                    var missing = ids.Except(retrieved.Keys);

                    if (missing.Any())
                    {
                        throw new EntityNotFoundException(typeof(T), missing.ToArray());
                    }

                    if (result == null)
                    {
                        result = retrieved;
                    }
                    else
                    {
                        result.AddRange(retrieved);
                    }
                }
                else
                {
                    if (result == null)
                    {
                        result = new Dictionary <PrimaryKey, T>();
                    }
                }

                return(ids.Select(id => result[id]).ToList()); //Preserve order
            }
        }
Exemple #2
0
        public static T Retrieve <T>(PrimaryKey id) where T : Entity
        {
            using (HeavyProfiler.Log("DBRetrieve", () => typeof(T).TypeName()))
            {
                if (EntityCache.Created)
                {
                    T cached = EntityCache.Get <T>(id);

                    if (cached != null)
                    {
                        return(cached);
                    }
                }

                var alternateEntity = Schema.Current.OnAlternativeRetriving(typeof(T), id);
                if (alternateEntity != null)
                {
                    return((T)alternateEntity);
                }

                var cc = GetCacheController <T>();
                if (cc != null)
                {
                    var filter = GetFilterQuery <T>();
                    if (filter == null || filter.InMemoryFunction != null)
                    {
                        T result;
                        using (new EntityCache())
                            using (var r = EntityCache.NewRetriever())
                            {
                                result = r.Request <T>(id);
                            }

                        if (filter != null && !filter.InMemoryFunction(result))
                        {
                            throw new EntityNotFoundException(typeof(T), id);
                        }

                        return(result);
                    }
                }

                T retrieved = Database.Query <T>().SingleOrDefaultEx(a => a.Id == id);

                if (retrieved == null)
                {
                    throw new EntityNotFoundException(typeof(T), id);
                }

                return(retrieved);
            }
        }