Exemplo n.º 1
0
        /// <summary>
        /// Adds the specified item to the data store.
        /// </summary>
        /// <param name="item">The data item to add.</param>
        /// <param name="identity">Optional, the item's identity.</param>
        /// <param name="token">A token with which the task may be cancelled.</param>
        /// <returns>The identity value which the item has, after it was added.</returns>
        /// <typeparam name="T">The item type.</typeparam>
        public async Task <object> AddAsync <T>(T item, object identity = null, CancellationToken token = default(CancellationToken)) where T : class
        {
            if (identity != null)
            {
                return(await wrapped.AddAsync <T>(item, identity, token).ConfigureAwait(false));
            }

            if (item is IEntity entity)
            {
                identityGenerator.UpdateWithIdentity(entity);
                return(await wrapped.AddAsync <T>((T)entity, entity.IdentityValue, token)
                       .ConfigureAwait(false));
            }

            return(await wrapped.AddAsync <T>(item, identity, token)
                   .ConfigureAwait(false));
        }
Exemplo n.º 2
0
        async Task <IIdentity <TEntity> > AddInternalAsync <TEntity>(TEntity entity, CancellationToken token = default(CancellationToken)) where TEntity : class, IEntity
        {
            var currentIdentity      = entity.GetIdentity();
            var currentIdentityValue = (currentIdentity != null)? currentIdentity.Value : null;

            var newIdentityValue = await persister.AddAsync(entity, currentIdentityValue, token)
                                   .ConfigureAwait(false);

            return((newIdentityValue is null) ? null : (IIdentity <TEntity>)idFactory.Create(typeof(TEntity), newIdentityValue));
        }