コード例 #1
0
ファイル: MongoModelBuilder.cs プロジェクト: zjc-china/abp
        public MongoDbContextModel Build(AbpMongoDbContext dbContext)
        {
            lock (SyncObj)
            {
                var entityModels = _entityModelBuilders
                                   .Select(x => x.Value)
                                   .Cast <IMongoEntityModel>()
                                   .ToImmutableDictionary(x => x.EntityType, x => x);

                var baseClasses = new List <Type>();

                foreach (var entityModel in entityModels.Values)
                {
                    var map = entityModel.As <IHasBsonClassMap>().GetMap();
                    if (!BsonClassMap.IsClassMapRegistered(map.ClassType))
                    {
                        BsonClassMap.RegisterClassMap(map);
                    }

                    baseClasses.AddRange(entityModel.EntityType.GetBaseClasses(includeObject: false));

                    CreateCollectionIfNotExists(dbContext, entityModel.CollectionName);
                }

                baseClasses = baseClasses.Distinct().ToList();

                foreach (var baseClass in baseClasses)
                {
                    if (!BsonClassMap.IsClassMapRegistered(baseClass))
                    {
                        var map = new BsonClassMap(baseClass);
                        map.ConfigureAbpConventions();
                        BsonClassMap.RegisterClassMap(map);
                    }
                }

                return(new MongoDbContextModel(entityModels));
            }
        }
コード例 #2
0
 public MongoEntityModelBuilder()
 {
     EntityType    = typeof(TEntity);
     _bsonClassMap = new BsonClassMap <TEntity>();
     _bsonClassMap.ConfigureAbpConventions();
 }
コード例 #3
0
ファイル: MongoModelBuilder.cs プロジェクト: younes21/abp
    public virtual MongoDbContextModel Build(AbpMongoDbContext dbContext)
    {
        lock (SyncObj)
        {
            var useAbpClockHandleDateTime = dbContext.LazyServiceProvider.LazyGetRequiredService <IOptions <AbpMongoDbOptions> >().Value.UseAbpClockHandleDateTime;

            var entityModels = _entityModelBuilders
                               .Select(x => x.Value)
                               .Cast <IMongoEntityModel>()
                               .ToImmutableDictionary(x => x.EntityType, x => x);

            var baseClasses = new List <Type>();

            foreach (var entityModel in entityModels.Values)
            {
                var map = entityModel.As <IHasBsonClassMap>().GetMap();

                if (useAbpClockHandleDateTime)
                {
                    var dateTimePropertyInfos = entityModel.EntityType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                                                .Where(property =>
                                                       (property.PropertyType == typeof(DateTime) ||
                                                        property.PropertyType == typeof(DateTime?)) &&
                                                       property.CanWrite
                                                       ).ToList();

                    dateTimePropertyInfos.ForEach(property =>
                    {
                        var disableDateTimeNormalization =
                            entityModel.EntityType.IsDefined(typeof(DisableDateTimeNormalizationAttribute), true) ||
                            ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault <DisableDateTimeNormalizationAttribute>(property) != null;

                        if (property.PropertyType == typeof(DateTime?))
                        {
                            map.MapProperty(property.Name).SetSerializer(new NullableSerializer <DateTime>().WithSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), disableDateTimeNormalization)));
                        }
                        else
                        {
                            map.MapProperty(property.Name).SetSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), disableDateTimeNormalization));
                        }
                    });
                }

                if (!BsonClassMap.IsClassMapRegistered(map.ClassType))
                {
                    BsonClassMap.RegisterClassMap(map);
                }

                baseClasses.AddRange(entityModel.EntityType.GetBaseClasses(includeObject: false));

                CreateCollectionIfNotExists(dbContext, entityModel.CollectionName);
            }

            baseClasses = baseClasses.Distinct().ToList();

            foreach (var baseClass in baseClasses)
            {
                if (!BsonClassMap.IsClassMapRegistered(baseClass))
                {
                    var map = new BsonClassMap(baseClass);
                    map.ConfigureAbpConventions();

                    if (useAbpClockHandleDateTime)
                    {
                        var dateTimePropertyInfos = baseClass.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                                                    .Where(property =>
                                                           (property.PropertyType == typeof(DateTime) ||
                                                            property.PropertyType == typeof(DateTime?)) &&
                                                           property.CanWrite
                                                           ).ToList();

                        dateTimePropertyInfos.ForEach(property =>
                        {
                            if (property.PropertyType == typeof(DateTime?))
                            {
                                map.MapProperty(property.Name).SetSerializer(new NullableSerializer <DateTime>().WithSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), false)));
                            }
                            else
                            {
                                map.MapProperty(property.Name).SetSerializer(new AbpMongoDbDateTimeSerializer(GetDateTimeKind(dbContext), false));
                            }
                        });
                    }

                    BsonClassMap.RegisterClassMap(map);
                }
            }

            return(new MongoDbContextModel(entityModels));
        }
    }