/// <summary>
        /// Applies the specified member map convention.
        /// </summary>
        /// <param name="memberMap">The member map convention.</param>
        public void Apply(BsonMemberMap memberMap)
        {
            IBsonSerializationOptions options = null;

            switch (memberMap.MemberInfo.MemberType)
            {
            case MemberTypes.Property:
                PropertyInfo propertyInfo = (PropertyInfo)memberMap.MemberInfo;
                if (propertyInfo.PropertyType == typeof(DateTime) ||
                    propertyInfo.PropertyType == typeof(DateTime?))
                {
                    options = new DateTimeSerializationOptions(DateTimeKind.Local);
                }
                break;

            case MemberTypes.Field:
                FieldInfo fieldInfo = (FieldInfo)memberMap.MemberInfo;
                if (fieldInfo.FieldType == typeof(DateTime) ||
                    fieldInfo.FieldType == typeof(DateTime?))
                {
                    options = new DateTimeSerializationOptions(DateTimeKind.Local);
                }
                break;

            default:
                break;
            }
            memberMap.SetSerializationOptions(options);
        }
Exemplo n.º 2
0
        public static void Initialize()
        {
            var dateTimeSerializationOptions = new DateTimeSerializationOptions { Kind = DateTimeKind.Utc };

            BsonClassMap.RegisterClassMap<Entity>(classMap =>
                                                      {
                                                          classMap.AutoMap();
                                                          classMap.SetIdMember(classMap.GetMemberMap(entity => entity.Id));
                                                      });

            BsonClassMap.RegisterClassMap<Game>(classMap =>
                                                    {
                                                        classMap.AutoMap();

                                                        classMap.GetMemberMap(game => game.InTime)
                                                            .SetSerializationOptions(dateTimeSerializationOptions);
                                                        classMap.GetMemberMap(game => game.OutTime)
                                                            .SetSerializationOptions(dateTimeSerializationOptions);
                                                        classMap.UnmapProperty(game => game.HoursPlayed);
                                                    });

            BsonClassMap.RegisterClassMap<Membership>(classMap =>
                                                          {
                                                              classMap.AutoMap();
                                                              classMap.UnmapProperty(membership => membership.ExpiresOn);
                                                              classMap.UnmapProperty(membership => membership.RemainingHours);
                                                              classMap.UnmapProperty(membership => membership.RemainingHours);
                                                              classMap.UnmapProperty(membership => membership.ExpiresOn);
                                                              classMap.MapProperty(membership => membership.MembershipId);
                                                              classMap.GetMemberMap(membership => membership.IssuedOn)
                                                                  .SetSerializationOptions(dateTimeSerializationOptions);

                                                          });

            BsonClassMap.RegisterClassMap<MembershipReCharge>(classMap =>
                                                                {
                                                                    classMap.AutoMap();
                                                                    classMap.GetMemberMap(membershipRefil => membershipRefil.RechargedOn)
                                                                        .SetSerializationOptions(dateTimeSerializationOptions);
                                                                });

            BsonClassMap.RegisterClassMap<MonthlyExpense>(classMap =>
                {
                    classMap.AutoMap();
                    classMap.UnmapMember(m => m.TotalExpenses);
                });
        }
 public MongoPerformanceMonitor(string connectionString, string databaseName)
 {
     if (!localDateTimeRegistered)
     {
         DateTimeSerializationOptions options = DateTimeSerializationOptions.LocalInstance;
         var serializer = new DateTimeSerializer(options);
         BsonSerializer.RegisterSerializer(typeof(DateTime), serializer);
         localDateTimeRegistered = true;
     }
     this.connectionString = connectionString;
     this.databaseName     = databaseName;
     this.insertOptions    = new MongoInsertOptions {
         WriteConcern = WriteConcern.Acknowledged
     };
     this.updateOptions = new MongoUpdateOptions {
         Flags = UpdateFlags.Multi, WriteConcern = WriteConcern.Acknowledged
     };
 }
 /// <summary>
 /// Applies the specified member map convention.
 /// </summary>
 /// <param name="memberMap">The member map convention.</param>
 public void Apply(BsonMemberMap memberMap)
 {
     IBsonSerializationOptions options = null;
     switch (memberMap.MemberInfo.MemberType)
     {
         case MemberTypes.Property:
             PropertyInfo propertyInfo = (PropertyInfo)memberMap.MemberInfo;
             if (propertyInfo.PropertyType == typeof(DateTime) ||
                 propertyInfo.PropertyType == typeof(DateTime?))
                 options = new DateTimeSerializationOptions(DateTimeKind.Local);
             break;
         case MemberTypes.Field:
             FieldInfo fieldInfo = (FieldInfo)memberMap.MemberInfo;
             if (fieldInfo.FieldType == typeof(DateTime) ||
                 fieldInfo.FieldType == typeof(DateTime?))
                 options = new DateTimeSerializationOptions(DateTimeKind.Local);
             break;
         default:
             break;
     }
     memberMap.SetSerializationOptions(options);
 }
Exemplo n.º 5
0
        public void Register()
        {
            if (_isRegistered)
            {
                return;
            }

            _isRegistered = true;

            var pack = new ConventionPack();

            pack.Add(new StringObjectIdConvention());
            ConventionRegistry.Register("DiscriminatorConventions", pack, x => true);

            DateTimeSerializationOptions dateTimeOptions = DateTimeSerializationOptions.LocalInstance;
            var dateTimeSerializer = new DateTimeSerializer(dateTimeOptions);

            BsonSerializer.RegisterSerializer(typeof(DateTime), dateTimeSerializer);

            RegisterUserObject();
            RegisterUserElement();
        }
 private DateTimeSerializer(
     DateTimeSerializationOptions options
 )
 {
     this.options = options;
 }
 /// <summary>
 /// Initializes a new instance of the DateTimeSerializer class.
 /// </summary>
 /// <param name="defaultSerializationOptions">The default serialization options.</param>
 public DateTimeSerializer(DateTimeSerializationOptions defaultSerializationOptions)
     : base(defaultSerializationOptions)
 {
 }