private void SetModifiedBySingle <T>(T entity) where T : IEntity { ThrowIfModifiedByIsEmpty <T>(); Cache <T> .ModifiedByProp?.SetValue( entity, BsonSerializer.Deserialize(ModifiedBy.ToBson(), Cache <T> .ModifiedByProp.PropertyType)); }
private void SetModifiedBySingle <T>(T entity) where T : IEntity { ThrowIfModifiedByIsEmpty <T>(); Cache <T> .ModifiedByProp?.SetValue( entity, BsonSerializer.Deserialize(ModifiedBy.ToBson(), Cache <T> .ModifiedByProp.PropertyType)); //note: we can't use an IModifiedBy interface because the above line needs a concrete type // to be able to correctly deserialize a user supplied derived/sub class of ModifiedOn. }
private void SetModifiedByMultiple <T>(IEnumerable <T> entities) where T : IEntity { if (Cache <T> .ModifiedByProp is null) { return; } ThrowIfModifiedByIsEmpty <T>(); var val = BsonSerializer.Deserialize(ModifiedBy.ToBson(), Cache <T> .ModifiedByProp.PropertyType); foreach (var e in entities) { Cache <T> .ModifiedByProp.SetValue(e, val); } }
/// <summary> /// Instantiates a DBContext /// </summary> /// <param name="modifiedBy">An optional ModifiedBy instance. /// When supplied, all save/update operations performed via this DBContext instance will set the value on entities that has a property of type ModifiedBy. /// You can even inherit from the ModifiedBy class and add your own properties to it. /// Only one ModifiedBy property is allowed on a single entity type.</param> public DBContext(ModifiedBy modifiedBy = null) => ModifiedBy = modifiedBy;
/// <summary> /// Gets a transaction context/scope for a given entity type's database /// </summary> /// <typeparam name="T">The entity type to determine the database from for the transaction</typeparam> /// <param name="options">Client session options (not required)</param> public static Transaction Transaction <T>(ClientSessionOptions options = null, ModifiedBy modifiedBy = null) where T : IEntity { return(new Transaction(DatabaseName <T>(), options, modifiedBy)); }
/// <summary> /// Gets a transaction context/scope for a given database or the default database if not specified. /// </summary> /// <param name="database">The name of the database which this transaction is for (not required)</param> /// <param name="options">Client session options (not required)</param> public static Transaction Transaction(string database = default, ClientSessionOptions options = null, ModifiedBy modifiedBy = null) { return(new Transaction(database, options, modifiedBy)); }
/// <summary> /// Instantiates and begins a transaction. /// </summary> /// <param name="database">The name of the database to use for this transaction. default db is used if not specified</param> /// <param name="options">Client session options for this transaction</param> /// <param name="modifiedBy">An optional ModifiedBy instance. /// When supplied, all save/update operations performed via this DBContext instance will set the value on entities that has a property of type ModifiedBy. /// You can inherit from the ModifiedBy class and add your own properties to it. /// Only one ModifiedBy property is allowed on a single entity type.</param> public Transaction(string database = default, ClientSessionOptions options = null, ModifiedBy modifiedBy = null) { Session = DB.Database(database).Client.StartSession(options); Session.StartTransaction(); ModifiedBy = modifiedBy; }