{"id":25097024,"url":"https://github.com/reecerussell/tx-command","last_synced_at":"2025-10-30T20:17:43.029Z","repository":{"id":44552858,"uuid":"346110979","full_name":"reecerussell/tx-command","owner":"reecerussell","description":"A simple commanding library with support for executing commands within a database transaction.","archived":false,"fork":false,"pushed_at":"2023-03-05T19:39:21.000Z","size":113,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-23T19:57:11.182Z","etag":null,"topics":["cqrs","nuget","rdbms","sql"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reecerussell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-09T18:52:14.000Z","updated_at":"2024-04-20T16:38:39.000Z","dependencies_parsed_at":"2022-09-04T00:30:57.528Z","dependency_job_id":null,"html_url":"https://github.com/reecerussell/tx-command","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/reecerussell/tx-command","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecerussell%2Ftx-command","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecerussell%2Ftx-command/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecerussell%2Ftx-command/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecerussell%2Ftx-command/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reecerussell","download_url":"https://codeload.github.com/reecerussell/tx-command/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecerussell%2Ftx-command/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281874338,"owners_count":26576420,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-30T02:00:06.501Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cqrs","nuget","rdbms","sql"],"created_at":"2025-02-07T17:19:46.771Z","updated_at":"2025-10-30T20:17:42.996Z","avatar_url":"https://github.com/reecerussell.png","language":"C#","readme":"![Actions](https://github.com/reecerussell/tx-command/actions/workflows/publish.yaml/badge.svg)\n[![codecov](https://codecov.io/gh/reecerussell/tx-command/branch/master/graph/badge.svg?token=2o5osNgjr8)](https://codecov.io/gh/reecerussell/tx-command)\n[![CodeFactor](https://www.codefactor.io/repository/github/reecerussell/tx-command/badge)](https://www.codefactor.io/repository/github/reecerussell/tx-command)\n![Nuget](https://img.shields.io/nuget/v/TxCommand)\n[![Nuget](https://img.shields.io/nuget/dt/TxCommand)](https://www.nuget.org/packages/TxCommand/)\n\n# TxCommand\n\nTxCommand 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.\n\n| Package                | Version                                                         | Downloads                                                                                                                  |\n| ---------------------- | --------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |\n| TxCommand              | ![Nuget](https://img.shields.io/nuget/v/TxCommand)              | [![Nuget](https://img.shields.io/nuget/dt/TxCommand)](https://www.nuget.org/packages/TxCommand/)                           |\n| TxCommand.Abstractions | ![Nuget](https://img.shields.io/nuget/v/TxCommand.Abstractions) | [![Nuget](https://img.shields.io/nuget/dt/TxCommand.Abstractions)](https://www.nuget.org/packages/TxCommand.Abstractions/) |\n| TxCommand.Sql | ![Nuget](https://img.shields.io/nuget/v/TxCommand.Sql) | [![Nuget](https://img.shields.io/nuget/dt/TxCommand.Sql)](https://www.nuget.org/packages/TxCommand.Sql/) |\n| TxCommand.Sql.Abstractions | ![Nuget](https://img.shields.io/nuget/v/TxCommand.Sql.Abstractions) | [![Nuget](https://img.shields.io/nuget/dt/TxCommand.Sql.Abstractions)](https://www.nuget.org/packages/TxCommand.Sql.Abstractions/) |\n| TxCommand.Mongo | ![Nuget](https://img.shields.io/nuget/v/TxCommand.Mongo) | [![Nuget](https://img.shields.io/nuget/dt/TxCommand.Mongo)](https://www.nuget.org/packages/TxCommand.Mongo/) |\n| TxCommand.Mongo.Abstractions | ![Nuget](https://img.shields.io/nuget/v/TxCommand.Mongo.Abstractions) | [![Nuget](https://img.shields.io/nuget/dt/TxCommand.Mongo.Abstractions)](https://www.nuget.org/packages/TxCommand.Mongo.Abstractions/) |\n\n## Get Started\n\nFor 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.\n\n```\n\u003e Install-Package TxCommand.Sql\n```\n\nAfter installing the TxCommand, the package can be easily configured in your DI setup. For example:\n\n```csharp\npublic void ConfigureServices(IServiceCollection services)\n{\n    // TxCommand.Sql depends on an IDbConnection, so here we configure\n    // an instance of MySqlConnection.\n    services.AddTransient(_ =\u003e new MySqlConnection(\"\u003cconnection string\u003e\"));\n\n    // Configure TxCommand and the Sql package.\n    services.AddTxCommand(o =\u003e o.AddSql());\n}\n```\n\nOnce 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.\n\n```csharp\nusing System;\nusing System.Data;\nusing TxCommand.Abstractions;\n\n...\n\n// This is the command which is used to create a car record. It implemented the\n// ITxCommand interface, which has an optional type parameter used as a result.\npublic class CreateCarCommand : ITxCommand\u003cint\u003e\n{\n    public string Reg { get; set; }\n\n    public CreateCarCommand(string reg)\n    {\n        Reg = reg;\n    }\n\n    // This is the main entrypoint to the command.\n    public async Task\u003cint\u003e ExecuteAsync(IDbConnection connection, IDbTransaction transaction)\n    {\n        const string query = \"INSERT INTO `Cars` (`Reg`) VALUES (@Reg); SELECT LAST_INSERT_ID();\";\n\n        return await connection.ExecuteScalarAsync\u003cint\u003e(query, new {Reg}, transaction);\n    }\n\n    // Validate is used to validate that the data passed to the command\n    // is valid, before execution.\n    public void Validate()\n    {\n        if (string.IsNullOrEmpty(Reg))\n        {\n            throw new ArgumentException(\"Reg cannot be empty\", nameof(Reg));\n        }\n    }\n}\n```\n\nNow 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.\n\n```csharp\nusing TxCommand.Abstractions;\n\n...\n\npublic class CarFactory\n{\n    private readonly ISessionFactory _sessionFactory;\n\n    // ISessionFactory can be injected into another service, using the DI container.\n    public CarFactory(ISessionFactory sessionFactory)\n    {\n        _sessionFactory = sessionFactory;\n    }\n\n    public async Task\u003cint\u003e CreateAsync(string reg)\n    {\n        // A session should be disposed to commit the transaction. Alternatively,\n        // session.CommitAsync() can be called - or even session.RollbackAsync();\n        using (var session = _sessionFactory.Create())\n        {\n            // Create a new instance of the command.\n            var command = new CreateCarCommand(reg);\n\n            // Then call execute! The session will first call command.Validate(),\n            // then it will be executed and return the result of the command.\n            return await session.ExecuteAsync(command);\n        }\n    }\n}\n\n```\n\n## Contributing\n\nNot much here, but feel free to raise an issue or open a Pull Request if you think of an enhancement or spot a bug!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freecerussell%2Ftx-command","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freecerussell%2Ftx-command","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freecerussell%2Ftx-command/lists"}