public static void ConfigureMapping() { var conventionsProfile = new ConventionProfile(); conventionsProfile.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention()); conventionsProfile.SetIdMemberConvention(new NamedIdMemberConvention("Id")); BsonClassMap.RegisterConventions(conventionsProfile, t => typeof(ISagaEntity).IsAssignableFrom(t)); }
private static void InitializeSerialization() { var conventions = new ConventionProfile(); conventions.SetDefaultValueConvention(new EmptyGuidDefaultValueConvention()); conventions.SetSerializeDefaultValueConvention(new NeverSerializeDefaultValueConvention()); BsonClassMap.RegisterConventions(conventions, type => type.FullName.StartsWith("MongoDB.BsonUnitTests.Jira.CSharp310Tests")); }
protected override void ConfigureConventions(Nancy.Conventions.NancyConventions nancyConventions) { base.ConfigureConventions(nancyConventions); var profile = new ConventionProfile(); profile.SetElementNameConvention(new CamelCaseElementNameConvention()); MongoDB.Bson.Serialization.BsonClassMap.RegisterConventions(profile, _ => true); }
// constructors /// <summary> /// Initializes a new instance of the BsonMemberMap class. /// </summary> /// <param name="memberInfo">The member info.</param> /// <param name="conventions">The conventions to use with this member.</param> public BsonMemberMap(MemberInfo memberInfo, ConventionProfile conventions) { _memberInfo = memberInfo; _memberType = BsonClassMap.GetMemberInfoType(memberInfo); _defaultValue = GetDefaultValue(_memberType); _conventions = conventions; }
/// <summary> /// Initializes a new instance of the BsonMemberMap class. /// </summary> /// <param name="memberInfo">The member info.</param> /// <param name="conventions">The conventions to use with this member.</param> public BsonMemberMap( MemberInfo memberInfo, ConventionProfile conventions ) { this.memberInfo = memberInfo; this.memberType = BsonClassMap.GetMemberInfoType(memberInfo); this.conventions = conventions; }
public virtual void Init() { var profile = new ConventionProfile(); profile.SetMemberFinderConvention(new LoggingMemberFinderConvention()); BsonClassMap.RegisterClassMap(new ExceptionMap()); BsonClassMap.RegisterClassMap(new LocationInformationMap()); BsonClassMap.RegisterConventions(profile, t => true); }
private static void InitializeSerialization() { var conventions = new ConventionProfile(); conventions.SetDefaultValueConvention(new EmptyGuidDefaultValueConvention()); #pragma warning disable 618 // SetSerializeDefaultValueConvention and NeverSerializeDefaultValueConvention are obsolete conventions.SetSerializeDefaultValueConvention(new NeverSerializeDefaultValueConvention()); #pragma warning restore 618 BsonClassMap.RegisterConventions(conventions, type => type.FullName.StartsWith("MongoDB.BsonUnitTests.Jira.CSharp310Tests", StringComparison.Ordinal)); }
public MongoDbSagaPersister(string connectionString, string collectionName) { this.collectionName = collectionName; database = MongoDatabase.Create(connectionString); elementNameConventions = new SagaDataElementNameConvention(); var conventionProfile = new ConventionProfile() .SetElementNameConvention(elementNameConventions); BsonClassMap.RegisterConventions(conventionProfile, t => typeof(ISagaData).IsAssignableFrom(t)); }
public void TestMappingUsesBsonSerializationOptionsConventionDoesNotMatchWrongProperty() { var profile = new ConventionProfile() .SetSerializationOptionsConvention(new TypeRepresentationSerializationOptionsConvention(typeof(ObjectId), BsonType.JavaScriptWithScope)); BsonClassMap.RegisterConventions(profile, t => t == typeof(A)); var classMap = BsonClassMap.LookupClassMap(typeof(A)); var options = classMap.GetMemberMap("NoMatch").SerializationOptions; Assert.IsNull(options); }
// public methods /// <summary> /// Merges another convention profile into this one (only missing conventions are merged). /// </summary> /// <param name="other">The other convention profile.</param> public void Merge(ConventionProfile other) { if (DefaultValueConvention == null) { DefaultValueConvention = other.DefaultValueConvention; } if (ElementNameConvention == null) { ElementNameConvention = other.ElementNameConvention; } if (ExtraElementsMemberConvention == null) { ExtraElementsMemberConvention = other.ExtraElementsMemberConvention; } if (IdGeneratorConvention == null) { IdGeneratorConvention = other.IdGeneratorConvention; } if (IdMemberConvention == null) { IdMemberConvention = other.IdMemberConvention; } if (IgnoreExtraElementsConvention == null) { IgnoreExtraElementsConvention = other.IgnoreExtraElementsConvention; } #pragma warning disable 618 // SerializeDefaultValueConvention is obsolete if (IgnoreIfDefaultConvention == null && SerializeDefaultValueConvention == null) { if (other.SerializeDefaultValueConvention != null) { SerializeDefaultValueConvention = other.SerializeDefaultValueConvention; } else { IgnoreIfDefaultConvention = other.IgnoreIfDefaultConvention; } } #pragma warning restore 618 if (IgnoreIfNullConvention == null) { IgnoreIfNullConvention = other.IgnoreIfNullConvention; } if (MemberFinderConvention == null) { MemberFinderConvention = other.MemberFinderConvention; } if (SerializationOptionsConvention == null) { SerializationOptionsConvention = other.SerializationOptionsConvention; } }
private IQueryable GetQueryableCollection(string connectionString, Dictionary<string, Type> providerTypes, string collectionName) { var collectionType = CreateDynamicTypeForCollection(collectionName, providerTypes); var conventions = new ConventionProfile(); conventions.SetIdMemberConvention(new NamedIdMemberConvention(MongoMetadata.MappedObjectIdName)); conventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention()); BsonClassMap.RegisterConventions(conventions, t => t == collectionType); return InterceptingProvider.Intercept( new MongoQueryableResource(connectionString, collectionName, collectionType), new ResultExpressionVisitor()); }
public void TestMappingWithAMatchingSerializationOptionsConventionDoesNotOverrideAttribute() { var profile = new ConventionProfile() .SetSerializationOptionsConvention(new TypeRepresentationSerializationOptionsConvention(typeof(ObjectId), BsonType.JavaScriptWithScope)); BsonClassMap.RegisterConventions(profile, t => t == typeof(B)); var classMap = BsonClassMap.LookupClassMap(typeof(B)); var options = classMap.GetMemberMap("Match").SerializationOptions; Assert.IsInstanceOf<RepresentationSerializationOptions>(options); Assert.AreEqual(BsonType.ObjectId, ((RepresentationSerializationOptions)options).Representation); }
public LogEntryMongoDataService() { lock (this) { if (!BsonClassMap.IsClassMapRegistered(typeof(LogEntry))) { var noIdConventions = new ConventionProfile(); noIdConventions.SetIdMemberConvention(new NamedIdMemberConvention()); // no names BsonClassMap.RegisterConventions(noIdConventions, t => t == typeof(LogEntry)); BsonClassMap.RegisterClassMap<LogEntry>(cm => cm.AutoMap()); } } }
// public static methods /// <summary> /// Looks up the effective set of conventions that apply to a type. /// </summary> /// <param name="type">The type.</param> /// <returns>The conventions for that type.</returns> public static IConventionPack Lookup(Type type) { if (type == null) { throw new ArgumentNullException("type"); } lock (__lock) { var pack = new ConventionPack(); // append any attribute packs (usually just one) at the end so attributes are processed last var attributePacks = new List <IConventionPack>(); #pragma warning disable 618 //obsoleted by ConventionProfile ConventionProfile conventionProfile = null; #pragma warning restore 618 foreach (var container in __conventionPacks) { if (container.Filter(type)) { #pragma warning disable 618 //obsoleted by ConventionProfile if (container.Pack is ConventionProfile) { conventionProfile = container.Pack as ConventionProfile; } #pragma warning restore 618 if (container.Name == "__attributes__") { attributePacks.Add(container.Pack); } else { pack.Append(container.Pack); } } } if (conventionProfile != null) { // already includes the default attribute convention pack return(conventionProfile); } foreach (var attributePack in attributePacks) { pack.Append(attributePack); } return(pack); } }
public void TestConventionProfileStillUsesDefaults() { #pragma warning disable 618 var conventions = new ConventionProfile(); conventions.SetElementNameConvention(new CamelCaseElementNameConvention()); BsonClassMap.RegisterConventions(conventions, t => t == typeof(A)); #pragma warning restore 618 var classMap = new BsonClassMap<A>(); classMap.AutoMap(); var memberMap = classMap.GetMemberMap(x => x.S); Assert.IsNotNull(memberMap); }
public static void ConfigureMongoDB(IUnityContainer container) { var settings = container.Resolve<PrototypeSettings>(); container.RegisterInstance(new MongoViewDatabase(settings.MongoViewConnectionString).EnsureIndexes()); container.RegisterInstance(new MongoLogsDatabase(settings.MongoLogsConnectionString).EnsureIndexes()); container.RegisterInstance(new MongoEventsDatabase(settings.MongoEventsConnectionString)); // Register bson serializer conventions var myConventions = new ConventionProfile(); myConventions.SetIdMemberConvention(new NoDefaultPropertyIdConvention()); myConventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention()); BsonClassMap.RegisterConventions(myConventions, type => true); DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.UtcInstance; }
/// <summary> /// Merges another convention profile into this one (only missing conventions are merged). /// </summary> /// <param name="other">The other convention profile.</param> public void Merge( ConventionProfile other ) { if (IdGeneratorConvention == null) { IdGeneratorConvention = other.IdGeneratorConvention; } if (DefaultValueConvention == null) { DefaultValueConvention = other.DefaultValueConvention; } if (ElementNameConvention == null) { ElementNameConvention = other.ElementNameConvention; } if (ExtraElementsMemberConvention == null) { ExtraElementsMemberConvention = other.ExtraElementsMemberConvention; } if (IdMemberConvention == null) { IdMemberConvention = other.IdMemberConvention; } if (IgnoreExtraElementsConvention == null) { IgnoreExtraElementsConvention = other.IgnoreExtraElementsConvention; } if (IgnoreIfNullConvention == null) { IgnoreIfNullConvention = other.IgnoreIfNullConvention; } if (MemberFinderConvention == null) { MemberFinderConvention = other.MemberFinderConvention; } if (SerializeDefaultValueConvention == null) { SerializeDefaultValueConvention = other.SerializeDefaultValueConvention; } }
public void TestSave() { var server = MongoServer.Create("mongodb://localhost/?safe=true"); var database = server["onlinetests"]; var collection = database.GetCollection<Foo>("csharp77"); var conventions = new ConventionProfile() .SetIdMemberConvention(new NamedIdMemberConvention("_id")); BsonClassMap.RegisterConventions(conventions, t => t == typeof(Foo)); collection.RemoveAll(); for (int i = 0; i < 10; i++) { var foo = new Foo { _id = ObjectId.Empty, Name = string.Format("Foo-{0}", i), Summary = string.Format("Summary for Foo-{0}", i) }; collection.Save(foo, SafeMode.True); var count = collection.Count(); Assert.AreEqual(i + 1, count); } }
public void InitTestDatabase() { var conventions = new ConventionProfile(); conventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention()); BsonClassMap.RegisterConventions(conventions, t => t.FullName.StartsWith("Bikee.")); // Compose all maps. BsonMapRegistrator.Compose(); string connectionString = ConfigurationManager.ConnectionStrings[0].ConnectionString; this.MongoServer = string.IsNullOrEmpty(connectionString) ? MongoServer.Create() // connect to local host : MongoServer.Create(connectionString); string databaseName = ConfigurationManager.AppSettings["testDatabaseName"] ?? "bikee_test"; if (this.MongoServer.DatabaseExists(databaseName)) { this.MongoServer.DropDatabase(databaseName); } this.MongoDatabase = this.MongoServer.GetDatabase(databaseName); DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance; }
public void TestSave() { var server = Configuration.TestServer; var database = Configuration.TestDatabase; var collection = Configuration.GetTestCollection<Foo>(); var conventions = new ConventionProfile() .SetIdMemberConvention(new NamedIdMemberConvention("_id")); BsonClassMap.RegisterConventions(conventions, t => t == typeof(Foo)); collection.RemoveAll(); for (int i = 0; i < 10; i++) { var foo = new Foo { _id = ObjectId.Empty, Name = string.Format("Foo-{0}", i), Summary = string.Format("Summary for Foo-{0}", i) }; collection.Save(foo, SafeMode.True); var count = collection.Count(); Assert.AreEqual(i + 1, count); } }
/// <summary> /// Initializes the driver after it has been instantiated. /// </summary> /// <param name="cxInfo">the serialized connection properties.</param> /// <param name="context">The driver object</param> /// <param name="executionManager">The current Query Execution Manager for this query</param> public override void InitializeContext(IConnectionInfo cxInfo, object context, QueryExecutionManager executionManager) { base.InitializeContext(cxInfo, context, executionManager); //since the type is generated dynamically we can only access it by reflection ConnectionProperties props = propsSerializer.Deserialize(cxInfo.DriverData); PropertyInfo pinf = context.GetType().GetProperty("SqlTabWriter", BindingFlags.Instance | BindingFlags.Public); pinf.SetValue(context, executionManager.SqlTranslationWriter, null); if (props.InitializationQuery != null) { MethodInfo customInitialize = context.GetType().GetMethod("DoCustomInit", new[] { typeof(ConnectionProperties) }); customInitialize.Invoke(context, new object[] { props }); } MethodInfo init = context.GetType().GetMethod("InitCollections", BindingFlags.Instance | BindingFlags.Public); init.Invoke(context, new object[] { }); if(!mSerializersAlreadyRegistered && props.CustomSerializers != null) { List<Assembly> assemblies = props.AssemblyLocations.Select(LoadAssemblySafely).ToList(); foreach (var pair in props.CustomSerializers) { var type = assemblies.Select(a => a.GetType(pair.Key)).FirstOrDefault(x => x != null); var serializer = assemblies.Select(a => a.GetType(pair.Value)).FirstOrDefault(x => x != null); if (type == null || serializer == null) return; //throw new Exception(string.Format("Unable to initialize custom serializer {0} for type {1}", pair.Value, pair.Key)); BsonSerializer.RegisterSerializer(type, (IBsonSerializer)Activator.CreateInstance(serializer)); } mSerializersAlreadyRegistered = true; } if (props.AdditionalOptions.BlanketIgnoreExtraElements) { var conventions = new ConventionProfile(); conventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention()); BsonClassMap.RegisterConventions(conventions, t => true); } //set as default MongoDB.Bson.IO.JsonWriterSettings.Defaults.Indent = true; }
/// <summary> /// Registers the MongoDB Bson serialization conventions. /// </summary> /// <param name="autoGenerateID">A <see cref="Boolean"/> value which indicates whether /// the ID value should be automatically generated when a new document is inserting.</param> /// <param name="localDateTime">A <see cref="Boolean"/> value which indicates whether /// the local date/time should be used when serializing/deserializing <see cref="DateTime"/> values.</param> /// <param name="additionConventions">Additional conventions that needs to be registered.</param> public static void RegisterConventions(bool autoGenerateID, bool localDateTime, ConventionProfile additionConventions) { var convention = new ConventionProfile(); convention.SetIdMemberConvention(new NamedIdMemberConvention("id", "Id", "ID", "iD")); if (autoGenerateID) convention.SetIdGeneratorConvention(new GuidIDGeneratorConvention()); if (localDateTime) convention.SetSerializationOptionsConvention(new UseLocalDateTimeConvention()); if (additionConventions != null) convention.Merge(additionConventions); BsonClassMap.RegisterConventions(convention, type => true); }
/// <summary> /// Merges another convention profile into this one (only missing conventions are merged). /// </summary> /// <param name="other">The other convention profile.</param> public void Merge( ConventionProfile other ) { if (IdGeneratorConvention == null) { IdGeneratorConvention = other.IdGeneratorConvention; } if (DefaultValueConvention == null) { DefaultValueConvention = other.DefaultValueConvention; } if (ElementNameConvention == null) { ElementNameConvention = other.ElementNameConvention; } if (ExtraElementsMemberConvention == null) { ExtraElementsMemberConvention = other.ExtraElementsMemberConvention; } if (IdMemberConvention == null) { IdMemberConvention = other.IdMemberConvention; } if (IgnoreExtraElementsConvention == null) { IgnoreExtraElementsConvention = other.IgnoreExtraElementsConvention; } if (IgnoreIfNullConvention == null) { IgnoreIfNullConvention = other.IgnoreIfNullConvention; } if(MemberFinderConvention == null) { MemberFinderConvention = other.MemberFinderConvention; } if (SerializeDefaultValueConvention == null) { SerializeDefaultValueConvention = other.SerializeDefaultValueConvention; } }
/// <summary> /// Initializes the <see cref="JobStore"/> class. /// </summary> static JobStore() { var myConventions = new ConventionProfile(); myConventions.SetIdMemberConvention(new IdOrKeyConvention()); BsonClassMap.RegisterConventions( myConventions, t => true ); BsonSerializer.RegisterSerializer( typeof(JobKey), new JobKeySerializer() ); BsonSerializer.RegisterSerializer( typeof(TriggerKey), new TriggerKeySerializer() ); BsonSerializer.RegisterSerializer( typeof(JobDetailImpl), new JobDetailImplSerializer() ); BsonClassMap.RegisterClassMap<JobDetailImpl>(cm => { cm.AutoMap(); cm.SetDiscriminator("JobDetailImpl"); }); BsonSerializer.RegisterSerializer( typeof(JobDataMap), new JobDataMapSerializer() ); BsonSerializer.RegisterSerializer( typeof(DateTimeOffset), new DateTimeOffsetSerializer() ); BsonSerializer.RegisterGenericSerializerDefinition(typeof(Collection.ISet<>), typeof(SetSerializer<>)); BsonClassMap.RegisterClassMap<AbstractTrigger>(cm => { cm.AutoMap(); cm.SetIsRootClass(true); }); BsonClassMap.RegisterClassMap<CalendarIntervalTriggerImpl>(cm => { cm.AutoMap(); cm.MapField("complete"); cm.MapField("nextFireTimeUtc"); cm.MapField("previousFireTimeUtc"); cm.SetIgnoreExtraElements(true); }); BsonClassMap.RegisterClassMap<CronTriggerImpl>(cm => { cm.AutoMap(); cm.MapField("nextFireTimeUtc"); cm.MapField("previousFireTimeUtc"); cm.MapField(x => x.TimeZone).SetSerializer(new TimeZoneInfoSerializer()); cm.SetIgnoreExtraElements(true); }); BsonSerializer.RegisterSerializer(typeof(TimeOfDay), new TimeOfDaySerializer()); BsonClassMap.RegisterClassMap<DailyTimeIntervalTriggerImpl>(cm => { cm.AutoMap(); cm.MapField("complete"); cm.MapField("nextFireTimeUtc"); cm.MapField("previousFireTimeUtc"); cm.MapField(x => x.TimeZone).SetSerializer(new TimeZoneInfoSerializer()); cm.SetIgnoreExtraElements(true); }); BsonClassMap.RegisterClassMap<SimpleTriggerImpl>(cm => { cm.AutoMap(); cm.MapField("complete"); cm.MapField("nextFireTimeUtc"); cm.MapField("previousFireTimeUtc"); cm.SetIgnoreExtraElements(true); }); }
public void AfterPropertiesSet() { var defaultProfile = ConventionProfile.GetDefault(); _profile = new ConventionProfile(); _filter = new ConventionFilterHelper(IncludeFilters, ExcludeFilters); _profile.SetDefaultValueConvention(DefaultValueConvention ?? defaultProfile.DefaultValueConvention); _profile.SetElementNameConvention(ElementNameConvention ?? defaultProfile.ElementNameConvention); _profile.SetExtraElementsMemberConvention(ExtraElementsMemberConvention ?? defaultProfile.ExtraElementsMemberConvention); _profile.SetIdGeneratorConvention(IdGeneratorConvention ?? defaultProfile.IdGeneratorConvention); _profile.SetIdMemberConvention(IdMemberConvention ?? defaultProfile.IdMemberConvention); _profile.SetIgnoreExtraElementsConvention(IgnoreExtraElementsConvention ?? defaultProfile.IgnoreExtraElementsConvention); _profile.SetIgnoreIfDefaultConvention(IgnoreIfDefaultConvention ?? defaultProfile.IgnoreIfDefaultConvention); _profile.SetIgnoreIfNullConvention(IgnoreIfNullConvention ?? defaultProfile.IgnoreIfNullConvention); _profile.SetMemberFinderConvention(MemberFinderConvention ?? defaultProfile.MemberFinderConvention); _profile.SetSerializationOptionsConvention(SerializationOptionsConvention ?? defaultProfile.SerializationOptionsConvention); BsonClassMap.RegisterConventions(_profile, _filter.Filter); }