Пример #1
0
        public void Handle(IList <IObjectFuture> objectFutures)
        {
            IEntityFactory  entityFactory = EntityFactory;
            IList <IObjRef> oris          = new List <IObjRef>(objectFutures.Count);

            // ObjectFutures have to be handled in order
            for (int i = 0, size = objectFutures.Count; i < size; i++)
            {
                IObjectFuture objectFuture = objectFutures[i];
                if (!(objectFuture is ObjRefFuture))
                {
                    throw new ArgumentException("'" + GetType().Name + "' cannot handle " + typeof(IObjectFuture).Name
                                                + " implementations of type '" + objectFuture.GetType().Name + "'");
                }
                if (objectFuture.Value != null)
                {
                    continue;
                }

                ObjRefFuture objRefFuture = (ObjRefFuture)objectFuture;
                IObjRef      ori          = objRefFuture.Ori;
                if (ori.Id != null && !Object.Equals(ori.Id, 0))
                {
                    oris.Add(ori);
                }
                else if (ori is IDirectObjRef && ((IDirectObjRef)ori).Direct != null)
                {
                    Object entity = ((IDirectObjRef)ori).Direct;
                    objRefFuture.Value = entity;
                    oris.Add(null);
                }
                else
                {
                    Object newEntity = entityFactory.CreateEntity(ori.RealType);
                    objRefFuture.Value = newEntity;
                    oris.Add(null);
                }
            }

            IList <Object> objects = Cache.GetObjects(oris, CacheDirective.ReturnMisses);

            for (int i = 0, size = objectFutures.Count; i < size; i++)
            {
                if (oris[i] == null)
                {
                    continue;
                }

                ObjRefFuture objRefFuture = (ObjRefFuture)objectFutures[i];
                Object       obj          = objects[i];
                objRefFuture.Value = obj;
            }
        }
Пример #2
0
        protected void BuildSetterCommands(Object entity, IObjRef[] newORIs, RelationMember member, IReader reader)
        {
            if (!member.IsToMany)
            {
                if (newORIs.Length == 0)
                {
                    return;
                }
                else if (newORIs.Length == 1)
                {
                    IObjectFuture  objectFuture = new ObjRefFuture(newORIs[0]);
                    IObjectCommand command      = CommandBuilder.Build(reader.CommandTypeRegistry, objectFuture, entity, member);
                    reader.AddObjectCommand(command);
                }
                else
                {
                    throw new ArgumentException("Multiple values for to-one relation");
                }
            }
            else
            {
                Object     coll       = ListUtil.CreateCollectionOfType(member.RealType, newORIs.Length);
                MethodInfo addMethod  = coll.GetType().GetMethod("Add");
                Object[]   parameters = new Object[1];

                bool                 useObjectFuture     = false;
                ICommandBuilder      commandBuilder      = CommandBuilder;
                ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry;
                foreach (IObjRef ori in newORIs)
                {
                    if (!(ori is IDirectObjRef))
                    {
                        IObjectFuture  objectFuture = new ObjRefFuture(ori);;
                        IObjectCommand command      = commandBuilder.Build(commandTypeRegistry, objectFuture, coll, addMethod);
                        reader.AddObjectCommand(command);
                        useObjectFuture = true;
                        continue;
                    }

                    Object item = ((IDirectObjRef)ori).Direct;
                    if (useObjectFuture)
                    {
                        IObjectCommand command = commandBuilder.Build(commandTypeRegistry, null, coll, addMethod, item);
                        reader.AddObjectCommand(command);
                    }
                    else
                    {
                        parameters[0] = item;
                        addMethod.Invoke(coll, parameters);
                    }
                }
            }
        }