示例#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 virtual Object PostProcess(Object obj, IPostProcessReader postProcessReader)
 {
     if (obj is IObjectFuture)
     {
         IObjectFuture        objectFuture        = (IObjectFuture)obj;
         ICommandTypeRegistry commandTypeRegistry = postProcessReader.CommandTypeRegistry;
         IObjectCommand       objectCommand       = CommandBuilder.Build(commandTypeRegistry, objectFuture, null);
         postProcessReader.AddObjectCommand(objectCommand);
         postProcessReader.ExecuteObjectCommands();
         obj = objectFuture.Value;
     }
     else
     {
         postProcessReader.ExecuteObjectCommands();
     }
     return(obj);
 }
示例#3
0
        public virtual Object ReadObject(Type returnType, Type objType, int id, IReader reader)
        {
            Object obj = Activator.CreateInstance(objType);

            if (id > 0)
            {
                reader.PutObjectWithId(obj, id);
            }
            reader.NextTag();
            ITypeInfoItem[] members = reader.GetMembersOfType(objType);

            int                  index               = 0;
            String               valueAttribute      = XmlDictionary.ValueAttribute;
            String               primitiveElement    = XmlDictionary.PrimitiveElement;
            ICommandBuilder      commandBuilder      = CommandBuilder;
            ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry;
            IConversionHelper    conversionHelper    = ConversionHelper;

            while (reader.IsStartTag())
            {
                ITypeInfoItem member = members[index++];
                Object        memberValue;
                if (primitiveElement.Equals(reader.GetElementName()))
                {
                    String value = reader.GetAttributeValue(valueAttribute);
                    memberValue = conversionHelper.ConvertValueToType(member.RealType, value);
                    reader.MoveOverElementEnd();
                }
                else
                {
                    memberValue = reader.ReadObject(member.RealType);
                }
                if (memberValue is IObjectFuture)
                {
                    IObjectFuture  objectFuture = (IObjectFuture)memberValue;
                    IObjectCommand command      = commandBuilder.Build(commandTypeRegistry, objectFuture, obj, member);
                    reader.AddObjectCommand(command);
                }
                else
                {
                    member.SetValue(obj, memberValue);
                }
            }
            return(obj);
        }
示例#4
0
        protected Object PostProcess(Object obj, IPostProcessReader pullParserReader)
        {
            HandlePostProcessingTag(pullParserReader);

            if (obj is IObjectFuture)
            {
                IObjectFuture        objectFuture        = (IObjectFuture)obj;
                ICommandTypeRegistry commandTypeRegistry = pullParserReader.CommandTypeRegistry;
                IObjectCommand       objectCommand       = CommandBuilder.Build(commandTypeRegistry, objectFuture, null);
                pullParserReader.AddObjectCommand(objectCommand);
                pullParserReader.ExecuteObjectCommands();
                obj = objectFuture.Value;
            }
            else
            {
                pullParserReader.ExecuteObjectCommands();
            }

            return(obj);
        }
示例#5
0
        public void Handle(IList <IObjectFuture> objectFutures)
        {
            List <IEnumerable> allToPrefetch = new List <IEnumerable>(objectFutures.Count);

            for (int i = 0, size = objectFutures.Count; i < size; i++)
            {
                IObjectFuture objectFuture = objectFutures[i];
                if (!(objectFuture is PrefetchFuture))
                {
                    throw new ArgumentException("'" + GetType().Name + "' cannot handle " + typeof(IObjectFuture).Name
                                                + " implementations of type '" + objectFuture.GetType().Name + "'");
                }

                PrefetchFuture prefetchFuture = (PrefetchFuture)objectFuture;
                IEnumerable    toPrefetch     = prefetchFuture.ToPrefetch;
                allToPrefetch.Add(toPrefetch);
            }

            PrefetchHelper.Prefetch(allToPrefetch);
        }
示例#6
0
        protected IDictionary <Type, ISet <IObjectFuture> > BucketSortObjectFutures(IList <IObjectCommand> objectCommands)
        {
            IDictionary <Type, ISet <IObjectFuture> > sortedObjectFutures = new Dictionary <Type, ISet <IObjectFuture> >((int)(objectCommands.Count / 0.75));

            for (int i = 0, size = objectCommands.Count; i < size; i++)
            {
                IObjectCommand objectCommand = objectCommands[i];
                IObjectFuture  objectFuture  = objectCommand.ObjectFuture;
                if (objectFuture != null)
                {
                    Type type = objectFuture.GetType();
                    ISet <IObjectFuture> objectFutures = DictionaryExtension.ValueOrDefault(sortedObjectFutures, type);
                    if (objectFutures == null)
                    {
                        objectFutures = new HashSet <IObjectFuture>();
                        sortedObjectFutures.Add(type, objectFutures);
                    }
                    objectFutures.Add(objectFuture);
                }
            }

            return(sortedObjectFutures);
        }
示例#7
0
        public IObjectCommand Build(ICommandTypeRegistry commandTypeRegistry, IObjectFuture objectFuture, Object parent, params Object[] optionals)
        {
            IObjectCommand command;

            if (parent == null)
            {
                command = BuildIntern <ResolveObjectCommand>(commandTypeRegistry, objectFuture, parent).Finish();
            }
            else if (parent.GetType().IsArray)
            {
                command = BuildIntern <ArraySetterCommand>(commandTypeRegistry, objectFuture, parent).PropertyValue("Index", optionals[0]).Finish();
            }
            else if (parent is IEnumerable && !(parent is String))
            {
                IBeanRuntime <CollectionSetterCommand> beanRuntime = BuildIntern <CollectionSetterCommand>(commandTypeRegistry, objectFuture, parent);
                if (optionals.Length > 0)
                {
                    beanRuntime.PropertyValue("AddMethod", optionals[0]);
                }
                if (optionals.Length > 1)
                {
                    beanRuntime.PropertyValue("Obj", optionals[1]);
                }
                command = beanRuntime.Finish();
            }
            else if (parent is CreateContainer || parent is UpdateContainer)
            {
                command = BuildIntern <MergeCommand>(commandTypeRegistry, objectFuture, parent).Finish();
            }
            else
            {
                command = BuildIntern <ObjectSetterCommand>(commandTypeRegistry, objectFuture, parent).PropertyValue("Member", optionals[0]).Finish();
            }

            return(command);
        }
示例#8
0
        public virtual Object ReadObject(Type returnType, String elementName, int id, IReader reader)
        {
            if (!XmlDictionary.ArrayElement.Equals(elementName))
            {
                throw new Exception("Element '" + elementName + "' not supported");
            }
            int  length        = Int32.Parse(reader.GetAttributeValue(XmlDictionary.SizeAttribute));
            Type componentType = ClassElementHandler.ReadFromAttribute(reader);

            Array targetArray;

            if (!reader.IsEmptyElement())
            {
                reader.NextTag();
            }
            if ("values".Equals(reader.GetElementName()))
            {
                String listOfValuesString = reader.GetAttributeValue("v");

                if (typeof(char).Equals(componentType) || typeof(byte).Equals(componentType) || typeof(sbyte).Equals(componentType) ||
                    typeof(bool).Equals(componentType))
                {
                    targetArray = (Array)ConversionHelper.ConvertValueToType(componentType.MakeArrayType(), listOfValuesString, EncodingInformation.SOURCE_BASE64 | EncodingInformation.TARGET_PLAIN);
                    reader.PutObjectWithId(targetArray, id);
                }
                else
                {
                    targetArray = Array.CreateInstance(componentType, length);
                    reader.PutObjectWithId(targetArray, id);
                    String[] items = splitPattern.Split(listOfValuesString);
                    for (int a = 0, size = items.Length; a < size; a++)
                    {
                        String item = items[a];
                        if (item == null || item.Length == 0)
                        {
                            continue;
                        }
                        Object convertedValue = ConversionHelper.ConvertValueToType(componentType, items[a]);
                        targetArray.SetValue(convertedValue, a);
                    }
                }
                reader.MoveOverElementEnd();
            }
            else
            {
                if (returnType.IsGenericType)
                {
                    componentType = returnType.GetGenericArguments()[0];
                }
                targetArray = Array.CreateInstance(componentType, length);
                reader.PutObjectWithId(targetArray, id);
                ICommandBuilder      commandBuilder      = CommandBuilder;
                ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry;
                for (int index = 0; index < length; index++)
                {
                    Object item = reader.ReadObject(componentType);
                    if (item is IObjectFuture)
                    {
                        IObjectFuture  objectFuture = (IObjectFuture)item;
                        IObjectCommand command      = commandBuilder.Build(commandTypeRegistry, objectFuture, targetArray, index);
                        reader.AddObjectCommand(command);
                    }
                    else
                    {
                        targetArray.SetValue(item, index);
                    }
                }
            }
            return(targetArray);
        }
示例#9
0
        public virtual Object ReadObject(Type returnType, String elementName, int id, IReader reader)
        {
            if (!XmlDictionary.SetElement.Equals(elementName) && !XmlDictionary.ListElement.Equals(elementName))
            {
                throw new Exception("Element '" + elementName + "' not supported");
            }
            String lengthValue = reader.GetAttributeValue(XmlDictionary.SizeAttribute);
            int    length      = lengthValue != null && lengthValue.Length > 0 ? Int32.Parse(lengthValue) : 0;
            // Do not remove although in Java it is not necessary to extract the generic type information of a collection.
            // This code is important for environments like C#
            Type componentType = ClassElementHandler.ReadFromAttribute(reader);

            if (returnType.IsGenericType)
            {
                componentType = returnType.GetGenericArguments()[0];
            }
            MethodInfo addMethod = null;

            Object[]    parameters = new Object[1];
            IEnumerable coll;

            if (XmlDictionary.SetElement.Equals(elementName))
            {
                Type setType = typeof(HashSet <>).MakeGenericType(componentType);
                coll      = (IEnumerable)Activator.CreateInstance(setType);
                addMethod = setType.GetMethod("Add");
            }
            else
            {
                Type listType = typeof(List <>).MakeGenericType(componentType);
                coll      = (IEnumerable)(length > 0 ? Activator.CreateInstance(listType, length) : Activator.CreateInstance(listType));
                addMethod = listType.GetMethod("Add");
            }
            reader.PutObjectWithId(coll, id);
            reader.NextTag();
            bool                 useObjectFuture     = false;
            ICommandBuilder      commandBuilder      = CommandBuilder;
            ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry;

            while (reader.IsStartTag())
            {
                Object item = reader.ReadObject(componentType);
                if (item is IObjectFuture)
                {
                    IObjectFuture  objectFuture = (IObjectFuture)item;
                    IObjectCommand command      = commandBuilder.Build(commandTypeRegistry, objectFuture, coll, addMethod);
                    reader.AddObjectCommand(command);
                    useObjectFuture = true;
                }
                else if (useObjectFuture)
                {
                    IObjectCommand command = commandBuilder.Build(commandTypeRegistry, null, coll, addMethod, item);
                    reader.AddObjectCommand(command);
                }
                else
                {
                    parameters[0] = item;
                    addMethod.Invoke(coll, parameters);
                }
            }
            return(coll);
        }
示例#10
0
        protected IBeanRuntime <C> BuildIntern <C>(ICommandTypeRegistry commandTypeRegistry, IObjectFuture objectFuture, Object parent) where C : IObjectCommand
        {
            Type commandType           = typeof(C);
            Type overridingCommandType = commandTypeRegistry.GetOverridingCommandType(commandType);

            if (overridingCommandType != null)
            {
                commandType = overridingCommandType;
            }
            IBeanRuntime <C> beanRuntime = BeanContext.RegisterBean <C>(commandType).PropertyValue("ObjectFuture", objectFuture).PropertyValue("Parent", parent);

            return(beanRuntime);
        }