/// <summary> /// Gets the changes. /// </summary> /// <param name="entity">The entity.</param> /// <param name="fields">The fields.</param> /// <param name="forwardRelationships">The forward relationships.</param> /// <param name="reverseRelationships">The reverse relationships.</param> /// <param name="getFields">if set to <c>true</c> [get fields].</param> /// <param name="getForwardRelationships">if set to <c>true</c> [get forward relationships].</param> /// <param name="getReverseRelationships">if set to <c>true</c> [get reverse relationships].</param> /// <exception cref="ArgumentNullException"> /// <paramref name="entity"/> cannot be null. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="entity"/> must also implement IEntityInternal. /// </exception> internal static void GetChanges(this IEntity entity, out IEntityFieldValues fields, out IDictionary <long, IChangeTracker <IMutableIdKey> > forwardRelationships, out IDictionary <long, IChangeTracker <IMutableIdKey> > reverseRelationships, bool getFields = true, bool getForwardRelationships = true, bool getReverseRelationships = true) { if (entity == null) { throw new ArgumentException("entity"); } IEntityModificationToken token; IEntityInternal entityInternal; entityInternal = entity as IEntityInternal; if (entityInternal != null) { token = entityInternal.ModificationToken; } else { throw new ArgumentException("Must implement IEntityIternal", "entity"); } Entity.GetChanges(token, out fields, out forwardRelationships, out reverseRelationships, getFields, getForwardRelationships, getReverseRelationships); }
/// <summary> /// Initializes a new instance of the <see cref="EntityChanges" /> class. /// </summary> /// <param name="fields">The fields.</param> /// <param name="fieldsChanged"> /// if set to <c>true</c> [fields changed]. /// </param> /// <param name="forwardRelationships">The forward relationships.</param> /// <param name="forwardRelationshipsChanged"> /// if set to <c>true</c> [forward relationships changed]. /// </param> /// <param name="reverseRelationships">The reverse relationships.</param> /// <param name="reverseRelationshipsChanged"> /// if set to <c>true</c> [reverse relationships changed]. /// </param> /// <exception cref="System.ArgumentNullException"> /// fields /// or /// forwardRelationships /// or /// reverseRelationships /// </exception> public EntityChanges(IEntityFieldValues fields, bool fieldsChanged, IDictionary <long, IChangeTracker <IMutableIdKey> > forwardRelationships, bool forwardRelationshipsChanged, IDictionary <long, IChangeTracker <IMutableIdKey> > reverseRelationships, bool reverseRelationshipsChanged) { Fields = fields; ForwardRelationships = forwardRelationships; ReverseRelationships = reverseRelationships; FieldsChanged = fieldsChanged; ForwardRelationshipsChanged = forwardRelationshipsChanged; ReverseRelationshipsChanged = reverseRelationshipsChanged; }
/// <summary> /// Called when an instance of a type containing an auto-number field is created. /// </summary> void IEntityFieldCreate.OnCreate(IEntity entity) { if (entity == null) { return; } long userId; RequestContext.TryGetUserId(out userId); ///// // This is already running in a transaction ///// using (DatabaseContext ctx = DatabaseContext.GetContext( )) { ///// // Command takes an Update Lock under SERIALIZABLE isolation to ensure concurrency. ///// using (DatabaseContextInfo.SetContextInfo("Create autonumber instance")) using (IDbCommand command = ctx.CreateCommand("spCreateAutoNumberInstance", CommandType.StoredProcedure)) { ctx.AddParameter(command, "@tenantId", DbType.Int64, RequestContext.TenantId); ctx.AddParameter(command, "@entityId", DbType.Int64, entity.Id); ctx.AddParameter(command, "@fieldId", DbType.Int64, _field.Id); ctx.AddParameter(command, "@context", DbType.AnsiString, DatabaseContextInfo.GetMessageChain(userId)); object newId = command.ExecuteScalar( ); if (newId != null && newId != DBNull.Value) { ///// // Update the read-only cache with the new value. ///// IEntityFieldValues readonlyFields = EntityFieldCache.Instance.GetOrCreate(entity.Id); ///// // Set the value. ///// readonlyFields[_field.Id] = ( int )newId; } } } }
/// <summary> /// Fill the EntityFieldCache with preloaded data. /// </summary> /// <param name="entityData"></param> private static void AddFieldsToCache(EntityData entityData) { long entityId = entityData.Id.Id; // Get field cache for entity IEntityFieldValues cachedFieldValues = EntityFieldCache.Instance.GetOrCreate(entityId); // Fields foreach (FieldData fieldEntry in entityData.Fields) { long fieldId = fieldEntry.FieldId.Id; // Only update field cache if the field is not present // This is to avoid populating the cache with stale data if (!cachedFieldValues.ContainsField(fieldId)) { TypedValue typedValue = fieldEntry.Value; object fieldValue = typedValue?.Value; cachedFieldValues[fieldId] = fieldValue; } } }