Ejemplo n.º 1
0
        /*******************************************/
        /**** Public Methods                    ****/
        /*******************************************/

        public static void RegisterClassMap(Type type)
        {
            if (!BsonClassMap.IsClassMapRegistered(type))
            {
                try
                {
                    if (type.IsEnum)
                    {
                        MethodInfo generic = m_CreateEnumSerializer.MakeGenericMethod(type);
                        generic.Invoke(null, null);
                    }
                    else if (!type.IsGenericTypeDefinition)
                    {
                        BsonClassMap cm = new BsonClassMap(type);
                        cm.AutoMap();
                        cm.SetDiscriminator(type.FullName);
                        cm.SetDiscriminatorIsRequired(true);
                        cm.SetIgnoreExtraElements(true);   // It would have been nice to use cm.MapExtraElementsProperty("CustomData") but it doesn't work for inherited properties
                        cm.SetIdMember(null);

                        BsonClassMap.RegisterClassMap(cm);

                        BsonSerializer.RegisterDiscriminatorConvention(type, new GenericDiscriminatorConvention());
                    }
                    else
                    {
                        BsonSerializer.RegisterDiscriminatorConvention(type, new GenericDiscriminatorConvention());
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.ToString());
                }
            }
        }
 public virtual void Apply([NotNull] BsonClassMap classMap)
 {
     Check.NotNull(classMap, nameof(classMap));
     if (classMap.ClassType.GetTypeInfo().IsAbstract)
     {
         classMap.SetDiscriminatorIsRequired(discriminatorIsRequired: true);
     }
 }
Ejemplo n.º 3
0
 /// <inheritdoc />
 /// <summary>
 /// Process the conventions on <paramref name="classMap"/> according to the given <paramref name="attribute"/>.
 /// </summary>
 /// <param name="classMap">The <see cref="BsonClassMap"/> to which the conventions will be assigned.</param>
 /// <param name="attribute">The <see cref="BsonKnownTypesAttribute" /> that defines the convention.</param>
 protected override void Apply(BsonClassMap classMap, BsonKnownTypesAttribute attribute)
 {
     Check.NotNull(classMap, nameof(classMap));
     if (!classMap.DiscriminatorIsRequired)
     {
         classMap.SetDiscriminatorIsRequired(classMap.ClassType.IsAbstract);
     }
 }
Ejemplo n.º 4
0
 private static void Initializer <TClass>(BsonClassMap <TClass> cm)
     where TClass : IModel
 {
     cm.AutoMap();
     cm.MapIdMember(c => c.Id).SetIdGenerator(CombGuidGenerator.Instance);
     cm.SetDiscriminator(typeof(TClass).Name);
     cm.SetDiscriminatorIsRequired(true);
     cm.SetIgnoreExtraElements(true);
 }
Ejemplo n.º 5
0
 // public methods
 /// <summary>
 /// Applies a modification to the class map.
 /// </summary>
 /// <param name="classMap">The class map.</param>
 public void Apply(BsonClassMap classMap)
 {
     if (_discriminator != null)
     {
         classMap.SetDiscriminator(_discriminator);
     }
     classMap.SetDiscriminatorIsRequired(_required);
     classMap.SetIsRootClass(_rootClass);
 }
 // public methods
 /// <summary>
 /// Applies a modification to the class map.
 /// </summary>
 /// <param name="classMap">The class map.</param>
 public void Apply(BsonClassMap classMap)
 {
     if (_discriminator != null)
     {
         classMap.SetDiscriminator(_discriminator);
     }
     classMap.SetDiscriminatorIsRequired(_required);
     classMap.SetIsRootClass(_rootClass);
 }
 public virtual void Apply([NotNull] BsonClassMap classMap)
 {
     if (classMap == null)
     {
         throw new ArgumentNullException(nameof(classMap));
     }
     classMap.SetDiscriminator(Discriminator);
     if (classMap.ClassType.GetTypeInfo().IsAbstract)
     {
         classMap.SetDiscriminatorIsRequired(true);
     }
 }
        /// <summary>
        /// Register a type to be serialized correctly on MongoDB
        /// </summary>
        /// <param name="type">Object type</param>
        /// <param name="discriminatorName">Unique name that will be associated to provided type</param>
        public static void Register(Type type, string discriminatorName)
        {
            if (BsonClassMap.IsClassMapRegistered(type))
            {
                return;
            }

            var cm = new BsonClassMap(type);

            cm.AutoMap();
            cm.SetDiscriminatorIsRequired(true);
            cm.SetDiscriminator(discriminatorName);

            BsonClassMap.RegisterClassMap(cm);
        }
Ejemplo n.º 9
0
        /*******************************************/

        private static void RegisterClassMap(Type type)
        {
            try
            {
                BsonClassMap cm = new BsonClassMap(type);
                cm.AutoMap();
                cm.SetDiscriminator(type.FullName);
                cm.SetDiscriminatorIsRequired(true);
                cm.SetIgnoreExtraElements(false);   // It would have been nice to use cm.MapExtraElementsProperty("CustomData") but it doesn't work for inherited properties
                BsonClassMap.RegisterClassMap(cm);

                BsonSerializer.RegisterDiscriminatorConvention(type, new GenericDiscriminatorConvention());
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
Ejemplo n.º 10
0
        protected override void BeginProcessing()
        {
            // | registered by Mdbc
            if (ClassMap.Contains(Type))
            {
                WriteVerbose($"Type {Type} was registered by Mdbc, doing nothing.");
                return;
            }

            // | registered by driver
            if (BsonClassMap.IsClassMapRegistered(Type))
            {
                if (ParameterSetName != psForce)
                {
                    WriteException(new PSInvalidOperationException("Class map is registered by driver. If this is expected invoke with just -Type and -Force."), Type);
                    return;
                }

                WriteVerbose($"Type {Type} was registered by driver, registering by Mdbc.");
                ClassMap.Add(Type);
                return;
            }

            try
            {
                var cm = new BsonClassMap(Type);
                if (Init == null)
                {
                    cm.AutoMap();
                }
                else
                {
                    var res = Actor.InvokeScript(Init, cm);
                    if (res.Count > 0)
                    {
                        throw new PSArgumentException("The Init script must not return anything.");
                    }
                }

                if (IdProperty != null)
                {
                    cm.MapIdProperty(IdProperty);
                }

                if (Discriminator != null)
                {
                    cm.SetDiscriminator(Discriminator);
                }
                if (DiscriminatorIsRequired)
                {
                    cm.SetDiscriminatorIsRequired(true);
                }

                if (IgnoreExtraElements)
                {
                    cm.SetIgnoreExtraElements(IgnoreExtraElements);
                }
                if (ExtraElementsProperty != null)
                {
                    cm.MapExtraElementsProperty(ExtraElementsProperty);
                }

                // in theory, may throw if the map is registered in another thread
                BsonClassMap.RegisterClassMap(cm);

                // done
                ClassMap.Add(Type);
            }
            catch (ArgumentException exn)
            {
                WriteException(exn, Type);
            }
        }
 public void Apply(BsonClassMap classMap)
 {
     classMap.SetDiscriminatorIsRequired(false);
 }