Exemplo n.º 1
0
        private object GetNavObject(object entity, FieldNavigationList fieldList)
        {
            var    accessor   = GetTypeAccessor(entity.GetType());
            var    parent     = entity;
            var    fieldCount = fieldList.Fields.Count();
            object child      = null;

            //Start at field 1 as 0 is the entity itself
            for (int i = 1; i < fieldCount; i++)
            {
                if (child != null)
                {
                    parent = child;
                }

                child = accessor[parent, fieldList.Fields[i]];

                if (child == null)
                {
                    break;
                }

                accessor = GetTypeAccessor(child.GetType());
            }

            return(child);
        }
Exemplo n.º 2
0
        private object ProcessField(object entity, List <FieldNavigationList> fieldLists)
        {
            //Start with least specific so we don't overwrite things
            fieldLists = fieldLists.OrderBy(x => x.Fields.Count).ToList();

            //Get type accessor for root object/collection
            var entityType       = entity.GetType();
            var rootTypeAccessor = GetTypeAccessor(entity.GetType());
            var newEntity        = ShallowClone(entity);

            //Loop through field lists
            foreach (var fieldList in fieldLists)
            {
                //Plan here is to build object structure from the deepest level up
                var fieldDepthCount = fieldList.Fields.Count();
                FieldNavigationList currentFieldList = fieldList;
                object parentObject = null;
                object childObject  = null;

                switch (fieldDepthCount)
                {
                case 1:
                    //Nothing to do here as newEntity is already a shallow clone of entity
                    break;

                default:
                    for (int i = (fieldDepthCount - 1); i >= 0; i--)
                    {
                        //As we loop through we lop off the end field to go down through the graph
                        currentFieldList = new FieldNavigationList {
                            Fields = fieldList.Fields.GetRange(0, i + 1)
                        };

                        //Get object - use GetNavObject if we're not at the root
                        if (i > 0)
                        {
                            parentObject = GetNavObject(entity, currentFieldList);
                            parentObject = ShallowClone(parentObject);
                        }
                        //We're at root so use our new entity that we will return
                        else
                        {
                            parentObject = newEntity;
                        }

                        //If we have a child then attach it to parent
                        if (childObject != null)
                        {
                            var parentAccessor = GetTypeAccessor(parentObject.GetType());
                            parentAccessor[parentObject, fieldList.Fields[i + 1]] = childObject;
                        }

                        //Keep ref to add and add it to next one
                        childObject = parentObject;
                    }
                    break;
                }
            }

            return(newEntity);
        }