Exemple #1
0
        internal static void Delete(this DbConnection connection, object data, string tableName,
                                    IFieldInfoCollection fields, DbTransaction transaction = null,
                                    ICommandBuilder commandBuilder = null, IExceptionProcessor exceptionProcessor = null)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (data is null)
            {
                throw new ArgumentNullException("data");
            }

            commandBuilder = commandBuilder ?? DefaultCommandBuilder;

            lock (data)
            {
                if (data is IPersistanceTracking)
                {
                    ((IPersistanceTracking)data).OnDeleting();
                }

                using (var command = connection.CreateCommand())
                {
                    commandBuilder.BuildDelete(command, data, tableName, fields);

                    try
                    {
                        ExecuteNonQuery(connection, command, transaction);
                    }
                    catch (Exception e)
                    {
                        if (exceptionProcessor is null)
                        {
                            throw;
                        }
                        else
                        {
                            throw exceptionProcessor.ProcessException(e, connection, command.CommandText, transaction);
                        }
                    }
                }

                if (data is IPersistanceTracking)
                {
                    ((IPersistanceTracking)data).OnDeleted();
                }
            }
        }