Ejemplo n.º 1
0
        /// <summary>
        /// Connect the related entities based on the foreign key values found in a component type.
        /// This updates the values of the component's properties.
        /// </summary>
        /// <param name="propName">Name of the (component) property of the entity.  May be null if the property is the entity's identifier.</param>
        /// <param name="compType">Type of the component</param>
        /// <param name="entityInfo">Breeze EntityInfo</param>
        /// <param name="meta">Metadata for the entity class</param>
        private void FixupComponentRelationships(string propName, ComponentType compType, EntityInfo entityInfo, IClassMetadata meta)
        {
            var    compPropNames = compType.PropertyNames;
            var    compPropTypes = compType.Subtypes;
            object component     = null;

            object[] compValues = null;
            var      isChanged  = false;

            for (var j = 0; j < compPropNames.Length; j++)
            {
                var compPropType = compPropTypes[j];
                if (compPropType.IsAssociationType && compPropType.IsEntityType)
                {
                    if (compValues == null)
                    {
                        // get the value of the component's subproperties
                        component  = GetPropertyValue(meta, entityInfo.Entity, propName);
                        compValues = compType.GetPropertyValues(component);
                    }
                    if (compValues[j] == null)
                    {
                        // the related entity is null
                        var relatedEntity = GetRelatedEntity(compPropNames[j], (EntityType)compPropType, entityInfo, meta);
                        if (relatedEntity != null)
                        {
                            compValues[j] = relatedEntity;
                            isChanged     = true;
                        }
                    }
                    else if (_removeMode)
                    {
                        // remove the relationship
                        compValues[j] = null;
                        isChanged     = true;
                    }
                }
            }
            if (isChanged)
            {
                compType.SetPropertyValues(component, compValues);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 使用主键批量获取对象集合。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="session"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static IList <T> GetList <T>(this ISession session, IEnumerable ids)
        {
            CacheLoader loader          = new CacheLoader();
            var         entityPersister = session.GetSessionImplementation().GetEntityPersister(typeof(T).FullName, null);
            var         entityName      = typeof(T).FullName;
            var         eventSource     = (IEventSource)session;
            var         loadType        = LoadEventListener.Get;

            List <T>  output   = new List <T>();
            ArrayList missings = new ArrayList();

            foreach (object id in ids)
            {
                EntityKey entityKey = new EntityKey(id, entityPersister, EntityMode.Poco);
                LoadEvent loadEvent = new LoadEvent(id, entityName, false, eventSource);

                var entity = loader.LoadFromSessionCache(loadEvent, entityKey, loadType);
                if (entity == null)
                {
                    entity = loader.LoadFromSecondLevelCache(loadEvent, entityPersister, loadType);
                }

                if (entity != null)
                {
                    output.Add((T)entity);
                }
                else
                {
                    missings.Add(id);
                }
            }

            if (missings.Count > 0)
            {
                //对组合键进行处理
                if (entityPersister.IdentifierType.IsComponentType)
                {
                    ComponentType idType = (ComponentType)entityPersister.IdentifierType;

                    List <HashSet <object> > props = new List <HashSet <object> >();
                    foreach (string name in idType.PropertyNames)
                    {
                        props.Add(new HashSet <object>());
                    }

                    foreach (var missing in missings)
                    {
                        var values = idType.GetPropertyValues(missing, EntityMode.Poco);
                        for (int i = 0; i < values.Length; i++)
                        {
                            props[i].Add(values[i]);
                        }
                    }

                    var cr = session.CreateCriteria(entityName);

                    //合并为查询
                    int index = 0;
                    foreach (HashSet <object> set in props)
                    {
                        string propName = entityPersister.IdentifierPropertyName + "." + idType.PropertyNames[index];
                        if (set.Count == 1)
                        {
                            cr = cr.Add(Expression.Eq(propName, set.First()));
                        }
                        else
                        {
                            cr = cr.Add(Expression.In(propName, set.ToArray()));
                        }
                        index++;
                    }

                    output.AddRange(cr.List <T>());
                }
                else
                {
                    output.AddRange(session.CreateCriteria(entityName)
                                    .Add(Expression.In(entityPersister.IdentifierPropertyName, missings))
                                    .List <T>());
                }
            }

            return(output);
        }