TxCommand.Mongo.Abstractions 1.0.0

dotnet add package TxCommand.Mongo.Abstractions --version 1.0.0                
NuGet\Install-Package TxCommand.Mongo.Abstractions -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TxCommand.Mongo.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.Mongo.Abstractions --version 1.0.0                
#r "nuget: TxCommand.Mongo.Abstractions, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TxCommand.Mongo.Abstractions as a Cake Addin
#addin nuget:?package=TxCommand.Mongo.Abstractions&version=1.0.0

// Install TxCommand.Mongo.Abstractions as a Cake Tool
#tool nuget:?package=TxCommand.Mongo.Abstractions&version=1.0.0                

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
TxCommand.Sql Nuget Nuget
TxCommand.Sql.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 an issue or open a Pull Request if you think of an enhancement or spot a bug!

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

NuGet packages (1)

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

Package Downloads
TxCommand.Mongo

An implementation of TxCommand to provide transactional commanding interfaces for MongoDB.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,409 9/18/2021