Ejemplo n.º 1
0
 public ModelMapRegistry(IModelMapCache cache)
 {
     _cache = cache;
 }
        void IExpandableMap.Expand(IModelMapCache cache)
        {
            var replacements = new List <Tuple <int, IncludePartial> >();

            for (var i = 0; i < _instructions.Count; ++i)
            {
                var partial = _instructions[i] as IncludePartial;
                if (partial != null)
                {
                    replacements.Add(new Tuple <int, IncludePartial>(i, partial));
                }
            }

            var offset = 0;

            foreach (var replacement in replacements)
            {
                var partial = cache.Partials().SingleOrDefault(_ => _.Name.EqualsIgnoreCase(replacement.Item2.Name));
                if (partial == null)
                {
                    continue;
                }

                partial.As <IExpandableMap>().Expand(cache);

                var index = replacement.Item1 + offset;
                _instructions.RemoveAt(index);

                _instructions.Insert(index, new PushVariableContext
                {
                    Attributes = replacement.Item2.Attributes
                });

                offset++;
                index++;

                var instructionsToAdd = partial
                                        ._instructions
                                        .Where(_ => _.GetType() != typeof(BeginModelMap) && _.GetType() != typeof(EndModelMap))
                                        .Select(CloneInstruction)
                                        .ToArray();

                for (var i = 0; i < instructionsToAdd.Length; ++i)
                {
                    var instruction = instructionsToAdd[i];
                    var targetIndex = index + i;
                    if (targetIndex > _instructions.Count)
                    {
                        _instructions.Add(instruction);
                    }
                    else
                    {
                        _instructions.Insert(index + i, instruction);
                    }
                }

                offset += instructionsToAdd.Length - 1;
                var endIndex = index + instructionsToAdd.Length;

                _instructions.Insert(endIndex, new PopVariableContext());
                offset++;
            }
        }