Пример #1
0
        /// <summary>
        /// Inserts the specified entity.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(T entity)
        {
            var args = new EntityEventArgs <T>(entity, this);

            OnBeforeInsert(this, args);
            if (args.Cancel)
            {
                return(0);
            }

            LogSqlCommand(InsertDefinition, entity);
            entity.RowId = Context.Connection.Query <long>(InsertDefinition, entity).FirstOrDefault();
            OnAfterInsert(this, args);
            return(1);
        }
Пример #2
0
        /// <summary>
        /// Provides and asynchronous counterpart to the Update method
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public async Task <int> UpdateAsync(T entity)
        {
            var args = new EntityEventArgs <T>(entity, this);

            OnBeforeUpdate(this, args);
            if (args.Cancel)
            {
                return(0);
            }

            LogSqlCommand(UpdateDefinition, entity);
            var affected = await Context.Connection.ExecuteAsync(UpdateDefinition, entity);

            OnAfterUpdate(this, args);
            return(affected);
        }
Пример #3
0
        /// <summary>
        /// Deletes the specified entity. RowId must be set.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException"></exception>
        public int Delete(T entity)
        {
            if (entity.RowId == default(long))
            {
                throw new ArgumentException(nameof(entity.RowId));
            }

            var args = new EntityEventArgs <T>(entity, this);

            OnBeforeDelete(this, args);
            if (args.Cancel)
            {
                return(0);
            }

            LogSqlCommand(DeleteDefinition, entity);
            var affected = Context.Connection.Execute(DeleteDefinition, entity);

            entity.RowId = default(long);
            OnAfterDelete(this, args);
            return(affected);
        }
Пример #4
0
        /// <summary>
        /// Provides and asynchronous counterpart to the Insert method
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public async Task <int> InsertAsync(T entity)
        {
            var args = new EntityEventArgs <T>(entity, this);

            OnBeforeInsert(this, args);
            if (args.Cancel)
            {
                return(0);
            }

            LogSqlCommand(InsertDefinition, entity);
            var result = await Context.Connection.QueryAsync <long>(InsertDefinition, entity);

            if (result.Any())
            {
                entity.RowId = result.First();
                OnAfterInsert(this, args);
                return(1);
            }

            return(0);
        }
Пример #5
0
        /// <inheritdoc />
        public async Task <int> DeleteAsync(T entity)
        {
            if (entity.RowId == default)
            {
                throw new ArgumentException(nameof(entity.RowId));
            }

            var args = new EntityEventArgs <T>(entity, this);

            OnBeforeDelete(this, args);
            if (args.Cancel)
            {
                return(0);
            }

            Context.LogSqlCommand(DeleteDefinition, entity);
            var affected = await Context.Connection.ExecuteAsync(DeleteDefinition, entity).ConfigureAwait(false);

            entity.RowId = default;
            OnAfterDelete(this, args);

            return(affected);
        }