public void TestLookupIdGeneratorConventionWithTestClassB()
 {
     var convention = new LookupIdGeneratorConvention();
     var classMap = new BsonClassMap<TestClassB>();
     classMap.MapIdMember(x => x.GuidId);
     convention.PostProcess(classMap);
     Assert.IsNotNull(classMap.IdMemberMap.IdGenerator);
     Assert.IsInstanceOf<GuidGenerator>(classMap.IdMemberMap.IdGenerator);
 }
 public override void Map(BsonClassMap <IdentifiableReadModel> config)
 {
     config.MapIdMember(m => m.Id)
     .SetIdGenerator(StringObjectIdGenerator.Instance);
     config.IdMemberMap
     .SetSerializer(new StringSerializer(BsonType.ObjectId));
 }
        public void Apply(BsonClassMap classMap)
        {
            var constructors = classMap
                               .ClassType
                               .GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
                               .Concat(classMap.ClassType.GetConstructors(BindingFlags.Public | BindingFlags.Instance));

            var shortestContructor = constructors
                                     .OrderBy(ctor => ctor.GetParameters().Length)
                                     .ThenBy(ctor => ctor.IsPublic) //Prioritize protected/private constructors
                                     .FirstOrDefault();

            classMap.MapConstructor(shortestContructor);

            var publicProperties = classMap.ClassType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                   .Where(p => p.CanRead);

            foreach (var publicProperty in publicProperties)
            {
                if (publicProperty.Name == "Id")
                {
                    classMap.MapIdMember(publicProperty).SetIdGenerator(StringObjectIdGenerator.Instance);
                }
            }
        }
 public static BsonMemberMap MapStringObjectId <TEntity>(this BsonClassMap <TEntity> classMap)
     where TEntity : IEntity <string>
 {
     return(classMap.MapIdMember(c => c.Id)
            .SetSerializer(new StringSerializer(BsonType.ObjectId))
            .SetIdGenerator(StringObjectIdGenerator.Instance));
 }
        public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
        {
            var entityType = definition.EntityType;

            //Find the first property with the "Key" attribute to use as the Id
            var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            var idProperty = properties.Where(p => p.GetCustomAttribute <KeyAttribute>() != null).FirstOrDefault();

            if (idProperty != null)
            {
                classMap.MapIdMember(idProperty);
            }

            //If there is no Id generator, set a default based on the member type
            if (classMap.IdMemberMap != null && classMap.IdMemberMap.IdGenerator == null)
            {
                var idMemberMap = classMap.IdMemberMap;
                var memberType  = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
                if (memberType == typeof(string))
                {
                    idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
                }
                else if (memberType == typeof(Guid))
                {
                    idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
                }
                else if (memberType == typeof(ObjectId))
                {
                    idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
                }
            }
        }
Beispiel #6
0
 protected override void Map(BsonClassMap <Entity <long> > map)
 {
     map.MapIdMember(x => x.Id)
     .SetIdGenerator(new Int64IdGenerator())
     .SetElementName("_id")
     ;
     map.SetIsRootClass(true);
 }
Beispiel #7
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);
 }
 public void TestLookupIdGeneratorConventionWithTestClassA()
 {
     var convention = new LookupIdGeneratorConvention();
     var classMap = new BsonClassMap<TestClassA>();
     classMap.MapIdMember(x => x.ObjectId);
     convention.PostProcess(classMap);
     Assert.NotNull(classMap.IdMemberMap.IdGenerator);
     Assert.IsType<ObjectIdGenerator>(classMap.IdMemberMap.IdGenerator);
 }
 public void Configure(BsonClassMap <Integration> classMap)
 {
     classMap.AutoMap();
     classMap.MapIdMember(c => c.Id);
     classMap.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
     classMap.MapMember(c => c.NickName).SetElementName("nickName");
     classMap.MapMember(c => c.Props).SetElementName("props");
     classMap.MapMember(c => c.Type).SetElementName("type");
 }
Beispiel #10
0
            public void Apply(BsonClassMap classMap)
            {
                var idMember = DataPropertyHelper.GetIdMember(classMap.ClassType);

                if (idMember != null)
                {
                    classMap.MapIdMember(idMember);
                }
            }
Beispiel #11
0
        public override void Map(BsonClassMap <Entity> config)
        {
            config.MapIdMember(m => m.Id)
            .SetIdGenerator(StringObjectIdGenerator.Instance);
            config.IdMemberMap
            .SetSerializer(new StringSerializer(BsonType.ObjectId));

            config.UnmapMember(e => e.DomainEvents);
        }
Beispiel #12
0
        private static BsonMemberMap MapMember(
            BsonClassMap bsonClassMap,
            MemberInfo member)
        {
            var result = DefaultIdMemberName.Equals(member.Name, StringComparison.OrdinalIgnoreCase)
                ? bsonClassMap.MapIdMember(member) // TODO: add logic to make sure ID is of acceptable type here...
                : bsonClassMap.MapMember(member);

            return(result);
        }
Beispiel #13
0
        public void TestLookupIdGeneratorConventionWithTestClassB()
        {
            var convention = new LookupIdGeneratorConvention();
            var classMap   = new BsonClassMap <TestClassB>();

            classMap.MapIdMember(x => x.GuidId);
            convention.PostProcess(classMap);
            Assert.NotNull(classMap.IdMemberMap.IdGenerator);
            Assert.IsType <GuidGenerator>(classMap.IdMemberMap.IdGenerator);
        }
        public void TestLookupIdGeneratorConventionWithTestClassA()
        {
            var convention = new LookupIdGeneratorConvention();
            var classMap   = new BsonClassMap <TestClassA>();

            classMap.MapIdMember(x => x.ObjectId);
            convention.PostProcess(classMap);
            Assert.IsNotNull(classMap.IdMemberMap.IdGenerator);
            Assert.IsInstanceOf <ObjectIdGenerator>(classMap.IdMemberMap.IdGenerator);
        }
 /// <summary>
 /// Configure the default mapping for the model.
 /// </summary>
 protected virtual void ConfigureDetaultTableMappings <TType, TKey>(
     BsonClassMap <TType> c,
     Expression <Func <TType, TKey> > idMap)
 {
     c.AutoMap();
     if (idMap != null)
     {
         c.MapIdMember(idMap);
     }
     c.SetIgnoreExtraElements(true);
 }
Beispiel #16
0
 private static BsonMemberMap MapMember(BsonClassMap bsonClassMap, MemberInfo member)
 {
     if (DefaultIdMemberName.Equals(member.Name, StringComparison.OrdinalIgnoreCase))
     {
         // TODO: add logic to make sure ID is of acceptable type here...
         return(bsonClassMap.MapIdMember(member));
     }
     else
     {
         return(bsonClassMap.MapMember(member));
     }
 }
        // public methods
        /// <summary>
        /// Applies a modification to the class map.
        /// </summary>
        /// <param name="classMap">The class map.</param>
        public void Apply(BsonClassMap classMap)
        {
            foreach (var name in _names)
            {
                var member = classMap.ClassType.GetMember(name, _memberTypes, _bindingFlags).SingleOrDefault();

                if (member != null)
                {
                    classMap.MapIdMember(member);
                    return;
                }
            }
        }
        private void RegisterClass <T>() where T : IUnifiedIMObject
        {
            Type inheritType = typeof(UnifiedIMObject <>).MakeGenericType(typeof(T));

            if (typeof(T).IsSubclassOf(inheritType))
            {
                BsonClassMap basecm = new BsonClassMap(inheritType);
                basecm.MapIdMember(inheritType.GetProperty("ObjectID"))
                .SetSerializer(new IdSerializer())
                .SetIdGenerator(StringObjectIdGenerator.Instance)
                .SetElementName("_id");
                basecm.SetIsRootClass(true);
                BsonClassMap.RegisterClassMap(basecm);
                BsonClassMap.RegisterClassMap <T>();
            }
        }
        public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
        {
            IEntityProperty idProperty = default;

            foreach (var property in definition.Properties)
            {
                if (property.PropertyInfo.GetCustomAttribute <KeyAttribute>() != null)
                {
                    idProperty = property;
                    break;
                }

                if (property.ElementName.Equals("id", StringComparison.InvariantCultureIgnoreCase))
                {
                    //We don't break here just in case another property has the KeyAttribute
                    //We preference the attribute over the name match
                    idProperty = property;
                }
            }

            if (idProperty is EntityProperty entityProperty)
            {
                classMap.MapIdMember(idProperty.PropertyInfo);
                entityProperty.IsKey = true;

                //Set an Id Generator based on the member type
                var idMemberMap = classMap.IdMemberMap;
                var memberType  = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
                if (memberType == typeof(string))
                {
                    idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
                    definition.KeyGenerator = new EntityKeyGenerator(StringObjectIdGenerator.Instance);
                }
                else if (memberType == typeof(Guid))
                {
                    idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
                    definition.KeyGenerator = new EntityKeyGenerator(CombGuidGenerator.Instance);
                }
                else if (memberType == typeof(ObjectId))
                {
                    idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
                    definition.KeyGenerator = new EntityKeyGenerator(ObjectIdGenerator.Instance);
                }
            }
        }
Beispiel #20
0
        // public methods
        /// <summary>
        /// Applies a modification to the class map.
        /// </summary>
        /// <param name="classMap">The class map.</param>
        public void Apply(BsonClassMap classMap)
        {
            foreach (var name in _names)
            {
                Type classType = classMap.ClassType;
                var  members   = classType.GetMember2(name, _memberTypes, _bindingFlags);
                var  member    = members.SingleOrDefault();

                if (member != null)
                {
                    if (IsValidIdMember(classMap, member))
                    {
                        classMap.MapIdMember(member);
                        return;
                    }
                }
            }
        }
            public void Apply(BsonClassMap classMap)
            {
                if (classMap.ClassType.Namespace.Contains("AppStoreService.Core"))
                {
                    foreach (var map in classMap.DeclaredMemberMaps.Where(x => x != null))
                    {
                        if (map.MemberType == typeof(object) &&
                            (map.MemberName == "Id" || map.MemberName == "ObjectId"))
                        {
                            classMap.MapIdMember(map.MemberInfo);
                            map.SetIdGenerator(new ObjectIdGenerator());
                        }

                        if (map.MemberType.IsGenericParameter && map.MemberType.GetGenericTypeDefinition() ==
                            typeof(NullValueDictionary <,>))
                        {
                            classMap.MapExtraElementsMember(map.MemberInfo);
                        }
                    }
                }
            }
Beispiel #22
0
        private static void _ApplyClassMap <TImplementation>(BsonClassMap <TImplementation> cm) where TImplementation : class, IModel
        {
            cm.AutoMap();

            var idMember = typeof(TImplementation)
                           .GetMember("Id",
                                      System.Reflection.BindingFlags.Public
                                      | System.Reflection.BindingFlags.Instance
                                      | System.Reflection.BindingFlags.DeclaredOnly
                                      ).FirstOrDefault();

            if (idMember != null)
            {
                cm
                .MapIdMember(idMember)
                .SetElementName("_id")
                .SetSerializer(new StringSerializer(BsonType.ObjectId))
                .SetIdGenerator(StringObjectIdGenerator.Instance);
            }

            cm.SetIgnoreExtraElements(true);
        }
Beispiel #23
0
        //private static T BsonClassMap<core.Extensions.Data.Entity<T>> map => default(T);

        public override void Execute(WebApplicationBuilder builder, IServiceProvider serviceProvider = null)
        {
            base.Execute(builder, serviceProvider);

            var connections = options?.Connections;

            if (connections != null && connections.Any())
            {
                // Mappings
                var tKeys = new KeyValuePair <Type, IBsonSerializer>[] {
                    new KeyValuePair <Type, IBsonSerializer>(typeof(Entity <int>), new Int32Serializer(BsonType.Int32)),
                    new KeyValuePair <Type, IBsonSerializer>(typeof(Entity <long>), new Int64Serializer(BsonType.Int64)),
                    new KeyValuePair <Type, IBsonSerializer>(typeof(Entity <Guid>), new GuidSerializer(BsonType.String)),
                    new KeyValuePair <Type, IBsonSerializer>(typeof(Entity <string>), new StringSerializer(BsonType.String))
                };
                foreach (var tKey in tKeys)
                {
                    var cm = new BsonClassMap(tKey.Key);
                    cm.AutoMap();
                    cm.MapIdMember(tKey.Key.GetMember("Id").Single());
                    cm.IdMemberMap.SetSerializer(tKey.Value);
                    BsonClassMap.RegisterClassMap(cm);
                }

                var hcBuilder = builder.Services.AddHealthChecks();
                foreach (var conn in connections)
                {
                    hcBuilder.AddMongoDb(conn.ConnectionString, name: $"mongodb-{conn.Name}", tags: new[] { "db", "mongodb" });
                }

                builder.Services.Configure <Options>(_ =>
                {
                    _.Connections = connections;
                });

                builder.Services.TryAddTransient(typeof(IRepository <,>), typeof(Repository.Mongo <,>));
            }
        }
Beispiel #24
0
 private void ClassMapInitializer(BsonClassMap <MongoDbAdapter <T> > cm)
 {
     cm.AutoMap();
     cm.MapIdMember(c => c.Key);
 }
        // public methods
        /// <summary>
        /// Applies a modification to the class map.
        /// </summary>
        /// <param name="classMap">The class map.</param>
        public void Apply(BsonClassMap classMap)
        {
            foreach (var name in _names)
            {
				Type classType = classMap.ClassType;
	            var members = classType.GetMember2(name, _memberTypes, _bindingFlags);
				var member = members.SingleOrDefault();

				if (member != null)
                {
                    if (IsValidIdMember(classMap, member))
                    {
                        classMap.MapIdMember(member);
                        return;
                    }
                }
            }
        }
        // public methods
        /// <summary>
        /// Applies a modification to the class map.
        /// </summary>
        /// <param name="classMap">The class map.</param>
        public void Apply(BsonClassMap classMap)
        {
            foreach (var name in _names)
            {
                var member = classMap.ClassType.GetMember(name, _memberTypes, _bindingFlags).SingleOrDefault();

                if (member != null)
                {
                    classMap.MapIdMember(member);
                    return;
                }
            }
        }
Beispiel #27
0
 public static void Register(BsonClassMap <ShardWriteOperation> cm)
 {
     cm.AutoMap();
     cm.MapIdMember(lsm => lsm.Id);
     cm.SetIgnoreExtraElements(true);
 }
Beispiel #28
0
 public static void Register(BsonClassMap <WorkflowMetadata> cm)
 {
     cm.AutoMap();
     cm.MapIdMember(s => s.WorkflowId);
     cm.SetIgnoreExtraElements(true);
 }
Beispiel #29
0
 public static void Register(BsonClassMap <ShardMetadata> cm)
 {
     cm.AutoMap();
     cm.MapIdMember(lsm => lsm.ShardId);
     cm.SetIgnoreExtraElements(true);
 }