Esempio n. 1
0
        /// <inheritdoc />
        public SaveWorkState Create(SaveBundle saveBundle, Predicate <EntityInfo> includePredicate)
        {
            var entitiesMetadata = new Dictionary <string, EntityMetadata>();
            var saveMap          = new Dictionary <Type, List <EntityInfo> >();

            foreach (dynamic entityData in saveBundle.Entities)
            {
                var entityAspect = (EntityAspect)_jsonSerializer.Deserialize(new JTokenReader(entityData.entityAspect), typeof(EntityAspect));
                if (!entitiesMetadata.TryGetValue(entityAspect.EntityTypeName, out var metadata))
                {
                    metadata = GetMetadata(entityAspect.EntityTypeName);
                    entitiesMetadata.Add(entityAspect.EntityTypeName, metadata);
                }

                if (!saveMap.TryGetValue(metadata.Type, out var entityInfos))
                {
                    entityInfos = new List <EntityInfo>();
                    saveMap.Add(metadata.Type, entityInfos);
                }

                var entityInfo = CreateEntityInfo(entityData, entityAspect, metadata);
                if (includePredicate(entityInfo))
                {
                    entityInfos.Add(entityInfo);
                }
            }

            return(new SaveWorkState(saveMap));
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the changes made by the client, with an additional root model validation performed by the provided or default <see cref="IModelSaveValidator"/>.
        /// </summary>
        /// <typeparam name="TModel">The root model type.</typeparam>
        /// <param name="saveBundle">The changes to save.</param>
        /// <param name="configureAction">The save configuration action.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The save result.</returns>
        public Task <SaveResult> SaveChangesAsync <TModel>(SaveBundle saveBundle, Action <AsyncSaveChangesOptionsConfigurator <TModel> > configureAction, CancellationToken cancellationToken = default)
        {
            var configurator = new AsyncSaveChangesOptionsConfigurator <TModel>(_modelSaveValidatorProvider.Get(typeof(TModel)));

            configureAction?.Invoke(configurator);

            return(SaveChangesAsync(saveBundle, configurator.SaveChangesOptions, cancellationToken));
        }
Esempio n. 3
0
        /// <summary>
        /// Saves the changes made by the client.
        /// </summary>
        /// <param name="saveBundle">The changes to save.</param>
        /// <param name="configureAction">The save configuration action.</param>
        /// <returns>The save result.</returns>
        public SaveResult SaveChanges(SaveBundle saveBundle, Action <SaveChangesOptionsConfigurator> configureAction = null)
        {
            var configurator = new SaveChangesOptionsConfigurator();

            configureAction?.Invoke(configurator);

            return(SaveChanges(saveBundle, configurator.SaveChangesOptions));
        }
Esempio n. 4
0
        /// <summary>
        /// Saves the changes made by the client, with an additional root model validation performed by the provided or default <see cref="IModelSaveValidator"/>.
        /// </summary>
        /// <typeparam name="TModel">The root model type.</typeparam>
        /// <param name="saveBundle">The changes to save.</param>
        /// <param name="configureAction">The save configuration action.</param>
        /// <returns>The save result.</returns>
        public SaveResult SaveChanges <TModel>(SaveBundle saveBundle, Action <SaveChangesOptionsConfigurator <TModel> > configureAction = null)
        {
            var configurator = new SaveChangesOptionsConfigurator <TModel>(_modelSaveValidatorProvider.Get(typeof(TModel)));

            configureAction?.Invoke(configurator);

            return(SaveChanges(saveBundle, configurator.SaveChangesOptions));
        }
Esempio n. 5
0
        /// <summary>
        /// Saves the changes made by the client.
        /// </summary>
        /// <param name="saveBundle">The changes to save.</param>
        /// <param name="configureAction">The save configuration action.</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The save result.</returns>
        public Task <SaveResult> SaveChangesAsync(SaveBundle saveBundle, Action <AsyncSaveChangesOptionsConfigurator> configureAction, CancellationToken cancellationToken = default)
        {
            var configurator = new AsyncSaveChangesOptionsConfigurator();

            configureAction?.Invoke(configurator);

            return(SaveChangesAsync(saveBundle, configurator.SaveChangesOptions, cancellationToken));
        }
Esempio n. 6
0
        /// <summary>
        /// Saves the changes made by the client.
        /// </summary>
        /// <param name="saveBundle">The changes to save.</param>
        /// <param name="saveChangesOptions">The saving options.</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The save result.</returns>
        protected async Task <SaveResult> SaveChangesAsync(SaveBundle saveBundle, AsyncSaveChangesOptions saveChangesOptions, CancellationToken cancellationToken)
        {
            var saveWorkState = _saveWorkStateFactory.Create(saveBundle,
                                                             info => AfterCreateEntityInfo(info, saveBundle.SaveOptions) &&
                                                             (saveChangesOptions.AfterCreateEntityInfoAction?.Invoke(info, saveBundle.SaveOptions) ?? true));

            try
            {
                var context = new SaveChangesContext(saveWorkState.SaveMap, saveBundle.SaveOptions);
                return(await SaveChangesCoreAsync(saveWorkState, context, saveChangesOptions, cancellationToken).ConfigureAwait(false));
            }
            catch (Exception e)
            {
                if (!HandleSaveException(e, saveWorkState))
                {
                    throw;
                }
            }

            return(saveWorkState.ToSaveResult());
        }
Esempio n. 7
0
        /// <summary>
        /// Saves the changes made by the client.
        /// </summary>
        /// <param name="saveBundle">The changes to save.</param>
        /// <param name="saveChangesOptions">The saving options.</param>
        /// <returns>The save result.</returns>
        protected SaveResult SaveChanges(SaveBundle saveBundle, SaveChangesOptions saveChangesOptions)
        {
            var saveWorkState = _saveWorkStateFactory.Create(saveBundle,
                                                             info => AfterCreateEntityInfo(info, saveBundle.SaveOptions) &&
                                                             (saveChangesOptions.AfterCreateEntityInfoAction?.Invoke(info, saveBundle.SaveOptions) ?? true));

            try
            {
                var context = new SaveChangesContext(saveWorkState.SaveMap, saveBundle.SaveOptions);
                return(SaveChangesCore(saveWorkState, context, saveChangesOptions));
            }
            catch (Exception e)
            {
                if (!HandleSaveException(e, saveWorkState))
                {
                    throw;
                }
            }

            return(saveWorkState.ToSaveResult());
        }
Esempio n. 8
0
 /// <summary>
 /// Saves the changes made by the client.
 /// </summary>
 /// <param name="saveBundle">The changes to save.</param>
 /// <param name="cancellationToken">The cancellation token</param>
 /// <returns>The save result.</returns>
 public Task <SaveResult> SaveChangesAsync(SaveBundle saveBundle, CancellationToken cancellationToken = default)
 {
     return(SaveChangesAsync(saveBundle, AsyncSaveChangesOptions.Default, cancellationToken));
 }