TxCommand.Sql.Abstractions 1.0.0

.NET 5.0 .NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
Install-Package TxCommand.Sql.Abstractions -Version 1.0.0
dotnet add package TxCommand.Sql.Abstractions --version 1.0.0
<PackageReference Include="TxCommand.Sql.Abstractions" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TxCommand.Sql.Abstractions --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TxCommand.Sql.Abstractions, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install TxCommand.Sql.Abstractions as a Cake Addin
#addin nuget:?package=TxCommand.Sql.Abstractions&version=1.0.0

// Install TxCommand.Sql.Abstractions as a Cake Tool
#tool nuget:?package=TxCommand.Sql.Abstractions&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Actions codecov CodeFactor Nuget Nuget

TxCommand

TxCommand is a simple commanding package which provides commanding interfaces that can be executed within a transaction. TxCommand is built in a way where it can be extended to support multiple platforms and drivers.

Package Version Downloads
TxCommand Nuget Nuget
TxCommand.Abstractions Nuget Nuget

Get Started

For this example, we'll be using the Sql variant of TxCommand, TxCommand.Sql. To get started, install the TxCommand.Sql package to your project - this can be done either with the NuGet Package Manager or the NuGet CLI.

> Install-Package TxCommand.Sql

After installing the TxCommand, the package can be easily configured in your DI setup. For example:

public void ConfigureServices(IServiceCollection services)
{
    // TxCommand.Sql depends on an IDbConnection, so here we configure
    // an instance of MySqlConnection.
    services.AddTransient(_ => new MySqlConnection("<connection string>"));

    // Configure TxCommand and the Sql package.
    services.AddTxCommand()
        .AddSql();
}

Once the DI is configured, you're good to go. The next step is to setup a command. Below is an example of a command that is used to insert a Car record into a database.

using System;
using System.Data;
using TxCommand.Abstractions;

...

// This is the command which is used to create a car record. It implemented the
// ITxCommand interface, which has an optional type parameter used as a result.
public class CreateCarCommand : ITxCommand<int>
{
    public string Reg { get; set; }

    public CreateCarCommand(string reg)
    {
        Reg = reg;
    }

    // This is the main entrypoint to the command.
    public async Task<int> ExecuteAsync(IDbConnection connection, IDbTransaction transaction)
    {
        const string query = "INSERT INTO `Cars` (`Reg`) VALUES (@Reg); SELECT LAST_INSERT_ID();";

        return await connection.ExecuteScalarAsync<int>(query, new {Reg}, transaction);
    }

    // Validate is used to validate that the data passed to the command
    // is valid, before execution.
    public void Validate()
    {
        if (string.IsNullOrEmpty(Reg))
        {
            throw new ArgumentException("Reg cannot be empty", nameof(Reg));
        }
    }
}

Now we got the command sorted, the final step is to execute it. To execute the command we use the ISession interface. A Session is used to execute a set of command within a single transaction. An instance of ISession can be instantiated using the ISessionFactory dependency.

using TxCommand.Abstractions;

...

public class CarFactory
{
    private readonly ISessionFactory _sessionFactory;

    // ISessionFactory can be injected into another service, using the DI container.
    public CarFactory(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;
    }

    public async Task<int> CreateAsync(string reg)
    {
        // A session should be disposed to commit the transaction. Alternatively,
        // session.CommitAsync() can be called - or even session.RollbackAsync();
        using (var session = _sessionFactory.Create())
        {
            // Create a new instance of the command.
            var command = new CreateCarCommand(reg);

            // Then call execute! The session will first call command.Validate(),
            // then it will be executed and return the result of the command.
            return await session.ExecuteAsync(command);
        }
    }
}

Contributing

Not much here, but feel free to raise and issues or open a Pull Request if you think of an enhancement or spot a bug!

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TxCommand.Sql.Abstractions:

Package Downloads
TxCommand.Sql

Provides SQL implementations for TxCommand, supporting MySQL and SQL Server.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.1 854 12/9/2021
1.1.1 121 12/9/2021
1.1.0 120 12/9/2021
1.0.0 296 9/1/2021