Ejemplo n.º 1
0
        public override void Map(BsonClassMap <StatusEventDto> cm)
        {
            cm.SetDiscriminator("status");

            cm.MapProperty(p => p.FromStatus).SetElementName("fromStatus");
            cm.MapProperty(p => p.ToStatus).SetElementName("toStatus");
        }
        public override void Map(BsonClassMap <PriorityEventDto> cm)
        {
            cm.SetDiscriminator("priority");

            cm.MapProperty(p => p.FromPriority).SetElementName("fromPriority");
            cm.MapProperty(p => p.ToPriority).SetElementName("toPriority");
        }
Ejemplo n.º 3
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());
                }
            }
        }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 6
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.º 7
0
 public void RegisterClassWithDiscriminator(Type type)
 {
     string name = _mapper.GetFieldName(type);
     BsonClassMap cm = new BsonClassMap(type);
     cm.AutoMap();
     cm.SetDiscriminator(name);
     ExtraRegistrationSteps(type, cm); // Derived classes can implement this if they need to.
     BsonClassMap.RegisterClassMap(cm);
     // Don't need to RegisterDiscriminatorConvention for each subclass, only for the base class.
     //BsonSerializer.RegisterDiscriminatorConvention(type, new ScalarDiscriminatorConvention("type"));
 }
 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);
     }
 }
    public void Apply(BsonClassMap classMap)
    {
        Type type = classMap.ClassType;

        if (type.IsClass &&
            type != typeof(string) &&
            type != typeof(object) &&
            !type.IsAbstract)
        {
            classMap.SetDiscriminator(GetCustomDiscriminatorFor(type));
        }
    }
Ejemplo n.º 10
0
        public void RegisterClassWithDiscriminator(Type type)
        {
            string       name = _mapper.GetFieldName(type);
            BsonClassMap cm   = new BsonClassMap(type);

            cm.AutoMap();
            cm.SetDiscriminator(name);
            ExtraRegistrationSteps(type, cm);             // Derived classes can implement this if they need to.
            BsonClassMap.RegisterClassMap(cm);
            // Don't need to RegisterDiscriminatorConvention for each subclass, only for the base class.
            //BsonSerializer.RegisterDiscriminatorConvention(type, new ScalarDiscriminatorConvention("type"));
        }
Ejemplo n.º 11
0
        /// <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.º 12
0
        static SpyglassMongoContext()
        {
            var providers = ProviderService.GetProviders();

            foreach (var provider in providers)
            {
                var map = new BsonClassMap(provider.Value);
                map.AutoMap();
                map.SetDiscriminator(provider.Value.Name);

                if (!BsonClassMap.IsClassMapRegistered(provider.Value))
                {
                    BsonClassMap.RegisterClassMap(map);
                }
            }
        }
Ejemplo n.º 13
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.º 14
0
 public override void Map(BsonClassMap <CreatedEventDto> cm)
 {
     cm.SetDiscriminator("created");
 }
Ejemplo n.º 15
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)
        {
            var discriminator = GetDiscriminatorName(classMap.ClassType);

            classMap.SetDiscriminator(discriminator);
        }
        public override void Map(BsonClassMap <CommentEventDto> cm)
        {
            cm.SetDiscriminator("comment");

            cm.MapProperty("Comment").SetElementName("comment");
        }