Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8f7cde785e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 68 lines (58 sloc) 2.427 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
using System;
using System.Collections.Generic;

using MongoDB.Configuration.Mapping.Model;

namespace MongoDB.Serialization.Builders
{
    internal class PolymorphicClassMapBuilder : IObjectBuilder
    {
        private IClassMap _classMap;
        private ConcreteClassMapBuilder _concreteEntityBuilder;
        private readonly Dictionary<string, object> _properties;

        public PolymorphicClassMapBuilder(IClassMap classMap)
        {
            _classMap = classMap;
            _properties = new Dictionary<string, object>();
        }

        public void AddProperty(string name, object value)
        {
            if (_concreteEntityBuilder != null)
                _concreteEntityBuilder.AddProperty(name, value);
            else if (_classMap.DiscriminatorAlias == name)
            {
                //we have found our discriminator and *can* instantiate our type
                _classMap = _classMap.GetClassMapFromDiscriminator(value);
                _concreteEntityBuilder = new ConcreteClassMapBuilder(_classMap);
                foreach (var pair in _properties)
                    _concreteEntityBuilder.AddProperty(pair.Key, pair.Value);

                _properties.Clear();
            }
            else
                _properties.Add(name, value);
        }

        public object BuildObject()
        {
            if (_concreteEntityBuilder == null)
            {
                //we'll assume that this is the root class in the hierarchy.
                _concreteEntityBuilder = new ConcreteClassMapBuilder(_classMap);
            }

            return _concreteEntityBuilder.BuildObject();
        }

        public PropertyDescriptor GetPropertyDescriptor(string name)
        {
            var memberMap = _classMap.GetMemberMapFromAlias(name);
            if (memberMap == null)
                return null;

            var type = memberMap.MemberReturnType;
            bool isDictionary = false;
            if (memberMap is CollectionMemberMap)
                type = ((CollectionMemberMap)memberMap).ElementType;
            else if (memberMap is DictionaryMemberMap)
            {
                type = ((DictionaryMemberMap)memberMap).ValueType;
                isDictionary = true;
            }

            return new PropertyDescriptor { Type = type, IsDictionary = isDictionary };
        }
    }
}
Something went wrong with that request. Please try again.