{"id":24399659,"url":"https://github.com/open-net-libraries/open.database.extensions","last_synced_at":"2025-04-11T19:00:44.569Z","repository":{"id":43327756,"uuid":"117200522","full_name":"Open-NET-Libraries/Open.Database.Extensions","owner":"Open-NET-Libraries","description":"Useful set of utilities and abstractions for simplifying modern data-access operations and ensuring DI compatibility. ","archived":false,"fork":false,"pushed_at":"2024-01-17T07:55:52.000Z","size":2108,"stargazers_count":21,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-05-16T04:32:32.776Z","etag":null,"topics":["ado","async-await","asynchronous","channels","command","connection","connection-factory","csharp","data-flow","database","datareader","dotnet","expressive","extensions","sql-client","sqlclient"],"latest_commit_sha":null,"homepage":"https://electricessence.github.io/Open.Database.Extensions/api/index.html","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/Open-NET-Libraries.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-12T06:11:46.000Z","updated_at":"2024-06-14T06:40:56.389Z","dependencies_parsed_at":"2024-01-17T07:46:44.528Z","dependency_job_id":"73293c24-09b3-44ef-94de-480e57ba038d","html_url":"https://github.com/Open-NET-Libraries/Open.Database.Extensions","commit_stats":{"total_commits":237,"total_committers":4,"mean_commits":59.25,"dds":"0.14767932489451474","last_synced_commit":"4d5466e15e50079647b3148a00b737550a81f19e"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Open-NET-Libraries%2FOpen.Database.Extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Open-NET-Libraries%2FOpen.Database.Extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Open-NET-Libraries%2FOpen.Database.Extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Open-NET-Libraries%2FOpen.Database.Extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Open-NET-Libraries","download_url":"https://codeload.github.com/Open-NET-Libraries/Open.Database.Extensions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234715687,"owners_count":18875903,"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","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":["ado","async-await","asynchronous","channels","command","connection","connection-factory","csharp","data-flow","database","datareader","dotnet","expressive","extensions","sql-client","sqlclient"],"created_at":"2025-01-19T23:50:50.304Z","updated_at":"2025-04-11T19:00:44.550Z","avatar_url":"https://github.com/Open-NET-Libraries.png","language":"C#","readme":"# Open.Database.Extensions\n\n[![NuGet](https://img.shields.io/nuget/v/Open.Database.Extensions.Core.svg?style=flat)](https://www.nuget.org/packages/Open.Database.Extensions.Core/)\n\nUseful set of utilities and abstractions for simplifying modern database operations and ensuring dependency injection compatibility.\n\n## Principles\n\n- Minimize connection open time.\n- Deferred/lazy transformation.\n- Optimize for specific use cases.\n- Minimize boilerplate code.\n\n## Features\n- Provides a fluent interface for database operations.\n- Supports dependency injection for connection factories.\n- Support both synchronous and asynchronous operations.\n- Provides expressive commands for executing SQL queries and stored procedures.\n\n### Connection Factories\n\nConnection factories facilitate creation and disposal of connections without the concern of a connection reference or need for awareness of a connection string.\nA `SqlConnectionFactory` is provided and can be overridden to provide more specific dependency injection configurations.\n\n### Expressive Commands\n\nThe provided expressive command classes allow for an expressive means to append parameters and execute the results without lengthy complicated setup.\n\nExtensions are provided to create commands from connection factories.\n\n### Example\n\n```cs\nvar result = connectionFactory\n   .StoredProcedure(\"[procedure name]\")\n   .AddParam(\"a\",1)\n   .AddParam(\"b\",true)\n   .AddParam(\"c\",\"hello\")\n   .ExecuteScalar();\n```\n\n## Extensions\n\nInstead of writing this:\n\n```cs\nvar myResult = new List\u003cT\u003e();\nusing(var reader = await mySqlCommand.ExecuteReaderAsync(CommandBehavior.CloseConnection))\n{\n    while(await reader.ReadAsync())\n        myResult.Add(transform(reader));\n}\n```\n\nIs now simplified to this:\n\n```cs\nvar myResult = await mySqlCommand.ToListAsync(transform);\n```\n\n## Deferred Transformation\n\nIn order to keep connection open time to a minimum, some methods cache data before closing the connection and then subsequently applying the transformations as needed.\n\n### `Results\u003cT\u003e()` and `ResultsAsync\u003cT\u003e()`\n\nQueues all the data.  Then using the provided type `T` entity, the data is coerced by which properties intersect with the ones available to the `IDataReader`.\n\nOptionally a field to column override map can be passed as a parameter.  If a column is set as `null` then that field is ignored (not applied to the model).\n\n### Examples\n\nIf all the columns in the database map exactly to a field: (A column that has no associated field/property is ignored.)\n\n```cs\nvar people = cmd.Results\u003cPerson\u003e();\n```\n\nIf the database fields don't map exactly:\n\n```cs\nvar people = cmd.Results\u003cPerson\u003e(\n (Field:\"FirstName\", Column:\"first_name\"),\n (Field:\"LastName\", Column:\"last_name\")));\n```\n\nor\n\n```cs\nvar people = cmd.Results\u003cPerson\u003e(\n (\"FirstName\", \"first_name\"),\n (\"LastName\", \"last_name\"));\n```\n\nor\n\n```cs\nvar people = cmd.Results\u003cPerson\u003e(new Dictionary\u003cstring,string\u003e{\n {\"FirstName\", \"first_name\"},\n {\"LastName\", \"last_name\"});\n```\n\n### `Retrieve()` and `RetrieveAsync()`\n\nQueues all the data.  Returns a `QueryResult\u003cQueue\u003cobject[]\u003e\u003e` containing the requested data and column information.  The `.AsDequeueingMappedEnumerable()` extension will iteratively convert the results to dictionaries for ease of access.\n\n### `ResultsAsync\u003cT\u003e`\n\n`ResultsAsync\u003cT\u003e()` is fully asynchronous from end-to-end but returns an `IEnumerable\u003cT\u003e` that although has fully buffered the all the data into memory, has deferred the transformation until enumerated.  This way, the asynchronous data pipeline is fully complete before synchronously transforming the data.\n\n## Transactions\n\nExample:\n\n```cs\n// Returns true if the transaction is successful.\npublic static bool TryTransaction()\n=\u003e ConnectionFactory.Using(connection =\u003e\n    // Open a connection and start a transaction.\n    connection.ExecuteTransactionConditional(transaction =\u003e {\n\n        // First procedure does some updates.\n        var count = transaction\n            .StoredProcedure(\"[Updated Procedure]\")\n            .ExecuteNonQuery();\n\n        // Second procedure validates the results.\n        // If it returns true, then the transaction is committed.\n        // If it returns false, then the transaction is rolled back.\n        return transaction\n            .StoredProcedure(\"[Validation Procedure]\")\n            .AddParam(\"@ExpectedCount\", count)\n            .ExecuteScalar\u003cbool\u003e();\n    }));\n```\n\n## 8.0 Release Notes\n\n- All `.ConfigureAwait(true)` are now `.ConfigureAwait(false)` as they should be.  The caller will need to `.ConfigureAwait(true)` if they need to resume on the calling context.\n- Added `Open.Database.Extensions.MSSqlClient` for `Microsoft.Data.SqlClient` support.\n- .NET 8.0 added to targets to ensure potential compliation and performance improvements are available.\n- Improved nullable integrity.\n\n## 9.0 Release Notes\n\n- Open.Database.Extensions meta-package is discontinued.\n- .NET 9.0 added to targets to ensure potential compilation and performance improvements are available.\n- Impelmented some .NET 8 and 9 specific features.\n- Significant cleanup and simplifcation where possible.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-net-libraries%2Fopen.database.extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopen-net-libraries%2Fopen.database.extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-net-libraries%2Fopen.database.extensions/lists"}