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

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

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

                return(retrieved);
            }
        }
Example #2
0
        public static List <T> RetrieveAll <T>()
            where T : Entity
        {
            try
            {
                using (HeavyProfiler.Log("DBRetrieve", () => "All {0}".FormatWith(typeof(T).TypeName())))
                {
                    var cc = GetCacheController <T>();
                    if (cc != null)
                    {
                        var filter = GetFilterQuery <T>();
                        if (filter == null || filter.InMemoryFunction != null)
                        {
                            List <T> result;
                            using (new EntityCache())
                                using (var r = EntityCache.NewRetriever())
                                {
                                    result = cc.GetAllIds().Select(id => r.Request <T>(id)).ToList();
                                }

                            if (filter != null)
                            {
                                result = result.Where(filter.InMemoryFunction).ToList();
                            }

                            return(result);
                        }
                    }

                    return(Database.Query <T>().ToList());
                }
            }
            catch (Exception e)
            {
                e.Data["type"] = typeof(T).TypeName();
                throw;
            }
        }
Example #3
0
        public T Complete <T>(PrimaryKey?id, Action <T> complete) where T : Entity
        {
            if (id == null)
            {
                return(null);
            }

            IdentityTuple tuple = new IdentityTuple(typeof(T), id.Value);

            Entity result;

            if (entityCache.TryGetValue(tuple, out result))
            {
                return((T)result);
            }

            if (retrieved.TryGetValue(tuple, out result))
            {
                return((T)result);
            }

            T entity;

            if (TryGetRequest(tuple, out result))
            {
                entity = (T)result;
                requests[typeof(T)].Remove(id.Value);
            }
            else
            {
                entity = EntityCache.Construct <T>(id.Value);
            }

            retrieved.Add(tuple, entity);
            complete(entity);

            return(entity);
        }
Example #4
0
        public T Request <T>(PrimaryKey?id) where T : Entity
        {
            if (id == null)
            {
                return(null);
            }

            IdentityTuple tuple = new IdentityTuple(typeof(T), id.Value);

            Entity ident;

            if (entityCache.TryGetValue(tuple, out ident))
            {
                return((T)ident);
            }

            if (retrieved.TryGetValue(tuple, out ident))
            {
                return((T)ident);
            }

            ident = (T)requests?.TryGetC(typeof(T))?.TryGetC(id.Value);
            if (ident != null)
            {
                return((T)ident);
            }

            T entity = EntityCache.Construct <T>(id.Value);

            if (requests == null)
            {
                requests = new Dictionary <Type, Dictionary <PrimaryKey, Entity> >();
            }

            requests.GetOrCreate(tuple.Type).Add(tuple.Id, entity);

            return(entity);
        }
Example #5
0
        public static void Save(Entity[] entities)
        {
            if (entities == null || entities.Any(e => e == null))
            {
                throw new ArgumentNullException("entity");
            }

            using (var log = HeavyProfiler.LogNoStackTrace("PreSaving"))
            {
                Schema schema = Schema.Current;
                DirectedGraph <Modifiable> modifiables = PreSaving(() => GraphExplorer.FromRoots(entities));

                HashSet <Entity> wasNew          = modifiables.OfType <Entity>().Where(a => a.IsNew).ToHashSet(ReferenceEqualityComparer <Entity> .Default);
                HashSet <Entity> wasSelfModified = modifiables.OfType <Entity>().Where(a => a.Modified == ModifiedState.SelfModified).ToHashSet(ReferenceEqualityComparer <Entity> .Default);

                log.Switch("Integrity");

                var error = GraphExplorer.FullIntegrityCheck(modifiables);
                if (error != null)
                {
#if DEBUG
                    throw new IntegrityCheckException(error.WithEntities(modifiables));
#else
                    throw new IntegrityCheckException(error);
#endif
                }

                log.Switch("Graph");

                GraphExplorer.PropagateModifications(modifiables.Inverse());

                //colapsa modifiables (collections and embeddeds) keeping indentifiables only
                DirectedGraph <Entity> identifiables = GraphExplorer.ColapseIdentifiables(modifiables);

                foreach (var node in identifiables)
                {
                    schema.OnSaving(node);
                }

                //Remove all the edges that doesn't mean a dependency
                identifiables.RemoveEdges(identifiables.Edges.Where(e => !e.To.IsNew).ToList());

                //Remove all the nodes that are not modified
                List <Entity> notModified = identifiables.Where(node => !node.IsGraphModified).ToList();

                notModified.ForEach(node => identifiables.RemoveFullNode(node, None));

                log.Switch("SaveGroups");

                SaveGraph(schema, identifiables);

                foreach (var node in identifiables)
                {
                    schema.OnSaved(node, new SavedEventArgs
                    {
                        IsRoot          = entities.Contains(node),
                        WasNew          = wasNew.Contains(node),
                        WasSelfModified = wasSelfModified.Contains(node),
                    });
                }

                EntityCache.Add(identifiables);
                EntityCache.Add(notModified);

                GraphExplorer.CleanModifications(modifiables);
            }
        }
Example #6
0
 public void Dispose()
 {
     EntityCache.ReleaseRetriever(this);
 }