예제 #1
0
        /// <summary>
        /// Get an entity with the given type and ID from the database.
        /// If it can't find the object, an exception will be thrown.
        /// </summary>
        /// <param name="entityID">The primary key value of the object to load.</param>
        public static IEntity Get(object entityID, Type objectType)
        {
            if (objectType == null)
            {
                return(null);
            }

            // First try -> session memory:
            var result = SessionMemory.Get(objectType, entityID);

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

            if (NeedsTypeResolution(objectType))
            {
                foreach (var provider in ResolveDataProviders(objectType))
                {
                    try
                    {
                        result = Cache.Current.Get(entityID.ToString());
                        if (result != null)
                        {
                            return(result);
                        }

                        result = provider.Get(objectType, entityID);
                        if (result != null)
                        {
                            break;
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }

                //   return EntityFinder.Find(entityID, objectType);
            }
            else
            {
                result = GetConcrete(entityID, objectType);
            }

            if (result != null)
            {
                return(result);
            }
            else
            {
                throw new ArgumentException("Could not load the " + objectType.FullName + " instance with the ID of " + entityID + ".");
            }
        }
        static List <T> GetConcreteList <T>(IEnumerable <ICriterion> conditions, params QueryOption[] options) where T : IEntity
        {
            #region Load the instances
            List <T> rawObjects;
            var      objectType = typeof(T);
            if (NeedsTypeResolution(objectType))
            {
                rawObjects = new List <T>();

                foreach (var provider in ResolveDataProviders(objectType))
                {
                    rawObjects.AddRange(provider.GetList(objectType, conditions, options).Cast <T>().ToList());
                }
            }
            else
            {
                rawObjects = GetProvider <T>().GetList(objectType, conditions, options).Cast <T>().ToList();
            }
            #endregion

            var result = new List <T>();

            foreach (var item in rawObjects)
            {
                // In-session objects has higher priority:
                var inSession = SessionMemory.Get(typeof(T), item.GetId());

                if (inSession != null)
                {
                    result.Add((T)inSession);
                }
                else
                {
                    var inCache = default(T);

                    if (inCache == null)
                    {
                        inCache = (T)(object)Cache.Current.Get(item.GetType(), item.GetId().ToString());
                    }

                    if (inCache == null)
                    {
                        var asEntity = item as Entity;
                        EntityManager.RaiseOnLoaded(asEntity);

                        // Don't cache the result if it is fetched in a transaction.
                        if (!AnyOpenTransaction())
                        {
                            Cache.Current.Add(asEntity);
                        }

                        result.Add(item);
                    }
                    else
                    {
                        result.Add(inCache);
                    }
                }
            }

            if (options.OfType <SortQueryOption>().None() && options.OfType <PagingQueryOption>().None())
            {
                // Sort the collection if T is a generic IComparable:
                if (typeof(T).Implements <IComparable <T> >() || typeof(T).Implements <IComparable>()) // Note: T is always IComparable!
                {
                    result.Sort();
                }
            }

            return(result);
        }
예제 #3
0
        static List <T> GetConcreteList <T>(IEnumerable <ICriterion> conditions, params QueryOption[] options) where T : IEntity
        {
            DateTime?timestamp = null;

            if (IsCacheConcurrencyAware)
            {
                timestamp = DateTime.UtcNow;
            }

            #region Load the instances

            List <T> rawObjects;
            var      objectType = typeof(T);

            if (NeedsTypeResolution(objectType))
            {
                rawObjects = new List <T>();

                var takeOption = options.OfType <ResultSetSizeQueryOption>().FirstOrDefault();
                options = options.Except(i => i is ResultSetSizeQueryOption).ToArray();

                foreach (var provider in ResolveDataProviders(objectType))
                {
                    var query = new DatabaseQuery(provider.EntityType).Where(conditions).Config(options);
                    rawObjects.AddRange(provider.GetList(query).Cast <T>().ToList());
                }

                if (takeOption != null && takeOption.Number.HasValue)
                {
                    rawObjects = rawObjects.Take(takeOption.Number.Value).ToList();
                }
            }
            else
            {
                var query = new DatabaseQuery(objectType).Where(conditions).Config(options);
                rawObjects = GetProvider <T>().GetList(query).Cast <T>().ToList();
            }

            #endregion

            var result = new List <T>();

            foreach (var item in rawObjects)
            {
                // In-session objects has higher priority:
                var inSession = SessionMemory.Get(typeof(T), item.GetId());

                if (inSession != null)
                {
                    result.Add((T)inSession);
                }
                else
                {
                    var inCache = (T)(object)Cache.Current.Get(item.GetType(), item.GetId().ToString());

                    if (inCache == null)
                    {
                        var asEntity = item as Entity;
                        EntityManager.RaiseOnLoaded(asEntity);

                        if (!AnyOpenTransaction()) // Don't cache the result if it is fetched in a transaction.
                        {
                            if (IsCacheConcurrencyAware)
                            {
                                if (!Cache.Current.IsUpdatedSince(asEntity, timestamp.Value))
                                {
                                    Cache.Current.Add(asEntity);
                                }
                            }
                            else
                            {
                                Cache.Current.Add(asEntity);
                            }
                        }

                        result.Add(item);
                    }
                    else
                    {
                        result.Add(inCache);
                    }
                }
            }

            if (options.OfType <SortQueryOption>().None() && options.OfType <PagingQueryOption>().None())
            {
                // Sort the collection if T is a generic IComparable:
                if (typeof(T).Implements <IComparable <T> >() || typeof(T).Implements <IComparable>()) // Note: T is always IComparable!
                {
                    result.Sort();
                }
            }

            return(result);
        }