Example #1
0
        internal IEnumerable GetObjects(Type objectType, IList <string> memberNames, CriteriaOperator criteria, List <SortProperty> sorting, int topReturnedObjectsCount)
        {
            Guard.ArgumentNotNull(objectType, "objectType");

            IList <ISortPropertyInfo> sortInfos = null;

            if (sorting != null)
            {
                sortInfos = sorting.Select(sp => new SortPropertyInfo {
                    PropertyName = sp.PropertyName, Descending = sp.Direction == SortingDirection.Descending
                })
                            .Cast <ISortPropertyInfo>().ToList();
            }


            var secureCriteria = GetSecurityCriteria(objectType);

            if (!ReferenceEquals(null, criteria))
            {
                secureCriteria.Add(criteria);
            }

            string criteriaString = secureCriteria.Count > 0 ? CriteriaOperator.And(secureCriteria).ToString() : null;

            var objects = persistenceManager.GetObjects(objectType.AssemblyQualifiedName, criteriaString,
                                                        sortInfos, topReturnedObjectsCount);

            var keyInstanceCache = instances.Values.Where(ii => objectType.IsInstanceOfType(ii.Instance)).ToDictionary(ii => GetKeyValue(ii.Instance));

            for (int i = 0; i < objects.Count; i++)
            {
                var obj = objects[i];
                ObjectSpaceInstanceInfo existingInstanceInfo;
                keyInstanceCache.TryGetValue(GetKeyValue(obj), out existingInstanceInfo);
                if (existingInstanceInfo != null)
                {
                    objects[i] = existingInstanceInfo.Instance;
                }
                else
                {
                    AddObject(obj, InstanceState.Unchanged);
                }
            }
            return(objects);
        }