예제 #1
0
        private void InitialiseClassMap()
        {
            MappingLock.EnterUpgradeableReadLock();
            try
            {
                //For reasons unknown to me, you can't just call "BsonClassMap.LookupClassMap" as that "freezes" the class map
                //Instead, you must do the lookup and initial creation yourself.
                var classMaps = BsonClassMap.GetRegisteredClassMaps();
                ClassMap = classMaps.Where(cm => cm.ClassType == EntityType).FirstOrDefault();

                if (ClassMap == null)
                {
                    MappingLock.EnterWriteLock();
                    try
                    {
                        ClassMap = new BsonClassMap(EntityType);
                        ClassMap.AutoMap();
                        BsonClassMap.RegisterClassMap(ClassMap);

                        foreach (var processor in DefaultMappingPack.Instance.Processors)
                        {
                            processor.ApplyMapping(EntityType, ClassMap);
                        }
                    }
                    finally
                    {
                        MappingLock.ExitWriteLock();
                    }
                }
            }
            finally
            {
                MappingLock.ExitUpgradeableReadLock();
            }
        }
        public static IEntityDefinition RegisterType(Type entityType)
        {
            if (!IsValidTypeToMap(entityType))
            {
                throw new ArgumentException("Type is not a valid type to map", nameof(entityType));
            }

            MappingLock.EnterUpgradeableReadLock();
            try
            {
                if (EntityDefinitions.ContainsKey(entityType))
                {
                    throw new ArgumentException("Type is already registered", nameof(entityType));
                }

                if (BsonClassMap.IsClassMapRegistered(entityType))
                {
                    throw new ArgumentException($"Type is already registered as a {nameof(BsonClassMap)}");
                }

                MappingLock.EnterWriteLock();
                try
                {
                    //Now we have the write lock, do one super last minute check
                    if (EntityDefinitions.TryGetValue(entityType, out var definition))
                    {
                        //We will treat success of this check as if we have registered it just now
                        return(definition);
                    }

                    var classMap = new BsonClassMap(entityType);
                    definition = new EntityDefinition
                    {
                        EntityType = entityType
                    };

                    EntityDefinitions.TryAdd(entityType, definition);
                    BsonClassMap.RegisterClassMap(classMap);

                    foreach (var processor in MappingProcessors)
                    {
                        processor.ApplyMapping(definition, classMap);
                    }

                    return(definition);
                }
                finally
                {
                    MappingLock.ExitWriteLock();
                }
            }
            finally
            {
                MappingLock.ExitUpgradeableReadLock();
            }
        }
        public static bool TryRegisterType(Type entityType, out IEntityDefinition definition)
        {
            if (!IsValidTypeToMap(entityType))
            {
                definition = null;
                return(false);
            }

            MappingLock.EnterUpgradeableReadLock();
            try
            {
                if (EntityDefinitions.ContainsKey(entityType) || BsonClassMap.IsClassMapRegistered(entityType))
                {
                    definition = null;
                    return(false);
                }

                MappingLock.EnterWriteLock();
                try
                {
                    //Now we have the write lock, do one super last minute check
                    if (EntityDefinitions.TryGetValue(entityType, out definition))
                    {
                        //We will treat success of this check as if we have registered it just now
                        return(true);
                    }

                    var classMap = new BsonClassMap(entityType);
                    definition = new EntityDefinition
                    {
                        EntityType = entityType
                    };

                    EntityDefinitions.TryAdd(entityType, definition);
                    BsonClassMap.RegisterClassMap(classMap);

                    foreach (var processor in MappingProcessors)
                    {
                        processor.ApplyMapping(definition, classMap);
                    }

                    return(true);
                }
                finally
                {
                    MappingLock.ExitWriteLock();
                }
            }
            finally
            {
                MappingLock.ExitUpgradeableReadLock();
            }
        }
예제 #4
0
        public static IEntityDefinition RegisterType(Type entityType)
        {
            if (IsRegistered(entityType))
            {
                throw new ArgumentException("Type is already registered", nameof(entityType));
            }

            var definition = new EntityDefinition
            {
                EntityType = entityType
            };

            MappingLock.EnterUpgradeableReadLock();
            try
            {
                //For reasons unknown to me, you can't just call "BsonClassMap.LookupClassMap" as that "freezes" the class map
                //Instead, you must do the lookup and initial creation yourself.
                var classMap = BsonClassMap.GetRegisteredClassMaps()
                               .Where(cm => cm.ClassType == entityType).FirstOrDefault();

                if (classMap == null)
                {
                    MappingLock.EnterWriteLock();

                    try
                    {
                        classMap = new BsonClassMap(entityType);

                        BsonClassMap.RegisterClassMap(classMap);
                        classMap.AutoMap();

                        foreach (var processor in MappingProcessors)
                        {
                            processor.ApplyMapping(definition, classMap);
                        }
                    }
                    finally
                    {
                        MappingLock.ExitWriteLock();
                    }
                }
            }
            finally
            {
                MappingLock.ExitUpgradeableReadLock();
            }

            return(SetEntityDefinition(definition));
        }
 public static IEntityDefinition SetEntityDefinition(IEntityDefinition definition)
 {
     MappingLock.EnterWriteLock();
     try
     {
         return(EntityDefinitions.AddOrUpdate(definition.EntityType, definition, (entityType, existingValue) =>
         {
             return definition;
         }));
     }
     finally
     {
         MappingLock.ExitWriteLock();
     }
 }
예제 #6
0
        public static IEntityDefinition RegisterType(Type entityType)
        {
            if (IsRegistered(entityType))
            {
                throw new ArgumentException("Type is already registered", nameof(entityType));
            }

            var definition = new EntityDefinition
            {
                EntityType = entityType
            };

            MappingLock.EnterUpgradeableReadLock();
            try
            {
                if (!BsonClassMap.IsClassMapRegistered(entityType))
                {
                    MappingLock.EnterWriteLock();

                    try
                    {
                        var classMap = new BsonClassMap(entityType);
                        BsonClassMap.RegisterClassMap(classMap);

                        foreach (var processor in MappingProcessors)
                        {
                            processor.ApplyMapping(definition, classMap);
                        }
                    }
                    finally
                    {
                        MappingLock.ExitWriteLock();
                    }
                }
            }
            finally
            {
                MappingLock.ExitUpgradeableReadLock();
            }

            return(SetEntityDefinition(definition));
        }