/// <summary> /// Creates a BsonWriter to a BsonDocument. /// </summary> /// <param name="document">A BsonDocument.</param> /// <param name="settings">The settings.</param> /// <returns>A BsonWriter.</returns> public static BsonWriter Create( BsonDocument document, BsonDocumentWriterSettings settings ) { return(new BsonDocumentWriter(document, settings)); }
// constructors /// <summary> /// Initializes a new instance of the BsonDocumentWriter class. /// </summary> /// <param name="topLevelDocument">The document to write to (normally starts out as an empty document).</param> /// <param name="settings">The settings.</param> public BsonDocumentWriter(BsonDocument topLevelDocument, BsonDocumentWriterSettings settings) : base(settings) { _topLevelDocument = topLevelDocument; _documentWriterSettings = settings; // already frozen by base class _context = null; _state = BsonWriterState.Initial; }
// constructors /// <summary> /// Initializes a new instance of the BsonDocumentWriter class. /// </summary> /// <param name="topLevelDocument">The document to write to (normally starts out as an empty document).</param> /// <param name="settings">The settings.</param> public BsonDocumentWriter(BsonDocument topLevelDocument, BsonDocumentWriterSettings settings) : base(settings) { this.topLevelDocument = topLevelDocument; this.settings = settings; // already frozen by base class context = null; state = BsonWriterState.Initial; }
/// <summary> /// /// </summary> /// <param name="nominalType"></param> /// <param name="value"></param> /// <param name="options"></param> /// <returns></returns> public static BsonDocument Serialize(Type nominalType, object value, IBsonSerializationOptions options) { var document = new BsonDocument(); var writerSettings = new BsonDocumentWriterSettings(); var writer = new BsonDocumentWriter(document, writerSettings); BsonSerializer.Serialize(writer, nominalType, value, options); return document; }
/// <summary> /// Initializes a new instance of the BsonDocumentWriter class. /// </summary> /// <param name="topLevelDocument">The document to write to (normally starts out as an empty document).</param> /// <param name="settings">The settings.</param> public BsonDocumentWriter( BsonDocument topLevelDocument, BsonDocumentWriterSettings settings ) { this.topLevelDocument = topLevelDocument; this.settings = settings.Freeze(); context = null; state = BsonWriterState.Initial; }
// protected methods /// <summary> /// Creates a clone of the settings. /// </summary> /// <returns>A clone of the settings.</returns> protected override BsonWriterSettings CloneImplementation() { var clone = new BsonDocumentWriterSettings { GuidRepresentation = GuidRepresentation, MaxSerializationDepth = MaxSerializationDepth }; return clone; }
// protected methods /// <summary> /// Creates a clone of the settings. /// </summary> /// <returns>A clone of the settings.</returns> protected override BsonWriterSettings CloneImplementation() { var clone = new BsonDocumentWriterSettings { GuidRepresentation = GuidRepresentation, MaxSerializationDepth = MaxSerializationDepth }; return(clone); }
// constructors /// <summary> /// Initializes a new instance of the BsonDocumentWriter class. /// </summary> /// <param name="topLevelDocument">The document to write to (normally starts out as an empty document).</param> /// <param name="settings">The settings.</param> public BsonDocumentWriter(BsonDocument topLevelDocument, BsonDocumentWriterSettings settings) : base(settings) { if (topLevelDocument == null) { throw new ArgumentNullException("topLevelDocument"); } _topLevelDocument = topLevelDocument; _context = null; State = BsonWriterState.Initial; }
/// <summary> /// Initializes a new instance of the BsonDocumentWriter class. /// </summary> /// <param name="document">The document to write to (normally starts out as an empty document).</param> /// <param name="settings">The settings.</param> public BsonDocumentWriter(BsonDocument document, BsonDocumentWriterSettings settings) : base(settings) { if (document == null) { throw new ArgumentNullException("document"); } _document = document; _documentWriterSettings = settings; // already frozen by base class _context = null; State = BsonWriterState.Initial; }
// protected methods /// <summary> /// Creates a clone of the settings. /// </summary> /// <returns>A clone of the settings.</returns> protected override BsonWriterSettings CloneImplementation() { var clone = new BsonDocumentWriterSettings { MaxSerializationDepth = MaxSerializationDepth }; #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { clone.GuidRepresentation = GuidRepresentation; } #pragma warning restore 618 return(clone); }
/// <summary> /// Creates a BsonWriter to a BsonDocument. /// </summary> /// <param name="document">A BsonDocument.</param> /// <param name="settings">The settings.</param> /// <returns>A BsonWriter.</returns> public static BsonWriter Create(BsonDocument document, BsonDocumentWriterSettings settings) { return new BsonDocumentWriter(document, settings); }
/// <summary> /// /// </summary> /// <param name="storeData"></param> /// <returns></returns> private static BsonDocument ConvertStoreDataToBsonDocument(SessionStateStoreData storeData) { var items = storeData.Items; var document = new BsonDocument(); var documentWriterSettings = new BsonDocumentWriterSettings(); var documentWriter = new BsonDocumentWriter(document, documentWriterSettings); documentWriter.WriteStartDocument(); foreach (string key in items.Keys) { var value = items[key]; documentWriter.WriteName(key); BsonSerializer.Serialize(documentWriter, value.GetType(), value); } documentWriter.WriteEndDocument(); return document; }
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values) { if (context == null) { throw TraceException("SetPropertyValues", new ArgumentNullException("context")); } if (values == null) { throw TraceException("SetPropertyValues", new ArgumentNullException("values")); } var userName = (string) context["UserName"]; var isAuthenticated = (bool) context["IsAuthenticated"]; if (string.IsNullOrWhiteSpace(userName) || values.Count == 0) { return; } var updateValues = (from SettingsPropertyValue value in values let allowAnonymous = value.Property.Attributes["AllowAnonymous"].Equals(true) where (value.IsDirty || !value.UsingDefaultValue) && (isAuthenticated || allowAnonymous) select value).ToList(); // If there are no values to update, then we're done here. if (updateValues.Count == 0) { return; } // If the user doesn't exist, and it's anonymous, create it. var user = GetMongoUser(userName); if (user == null) { if (!isAuthenticated) { user = new MongoMembershipUser { UserName = userName, IsAnonymous = true, CreationDate = DateTime.Now, }; try { var users = GetUserCollection(); users.Insert(user); } catch (MongoSafeModeException e) { var message = ProviderResources.CouldNotCreateUser; throw TraceException("SetPropertyValues", new ProviderException(message, e)); } } else { var message = ProviderResources.CouldNotFindUser; throw TraceException("SetPropertyValues", new ProviderException(message)); } } // Create the properties BSON document. var properties = new BsonDocument(); var propertiesWriterSettings = new BsonDocumentWriterSettings(); var propertiesWriter = new BsonDocumentWriter(properties, propertiesWriterSettings); propertiesWriter.WriteStartDocument(); foreach (var value in updateValues) { propertiesWriter.WriteName(value.Name); switch (value.Property.SerializeAs) { case SettingsSerializeAs.String: case SettingsSerializeAs.Xml: BsonSerializer.Serialize(propertiesWriter, typeof (string), value.SerializedValue); break; case SettingsSerializeAs.Binary: BsonSerializer.Serialize(propertiesWriter, typeof (byte[]), value.SerializedValue); break; case SettingsSerializeAs.ProviderSpecific: BsonSerializer.Serialize(propertiesWriter, value.Property.PropertyType, value.PropertyValue); break; default: throw TraceException("SetPropertyValues", new ArgumentOutOfRangeException()); } } propertiesWriter.WriteEndDocument(); // Create the profile BSON document. var profile = SerializationHelper.Serialize(typeof (MongoProfile), new MongoProfile { Properties = properties, LastActivityDate = DateTime.Now, LastUpdateDate = DateTime.Now }); try { var query = Query.EQ("UserName", userName); var update = Update.Set("Profile", profile); var users = GetUserCollection(); users.Update(query, update); } catch (MongoSafeModeException e) { var message = ProviderResources.CouldNotUpdateProfile; throw TraceException("SetPropertyValues", new ProviderException(message, e)); } }