GithubHelp home page GithubHelp logo

open-net-libraries / open.database.extensions Goto Github PK

View Code? Open in Web Editor NEW
21.0 5.0 4.0 2.06 MB

Useful set of utilities and abstractions for simplifying modern data-access operations and ensuring DI compatibility.

Home Page: https://electricessence.github.io/Open.Database.Extensions/api/index.html

License: MIT License

C# 100.00%
csharp dotnet database extensions sqlclient asynchronous expressive ado async-await data-flow

open.database.extensions's Introduction

Open.Database.Extensions

NuGet

Useful set of utilities and abstractions for simplifying modern database operations and ensuring dependency injection compatibility.

Connection Factories

Connection factories facilitate creation and disposal of connections without the concern of a connection reference or need for awareness of a connection string. A SqlConnectionFactory is provided and can be overridden to provide more specific dependency injection configurations.

Expressive Commands

The provided expressive command classes allow for an expressive means to append parameters and execute the results without lengthy complicated setup.

Extensions are provided to create commands from connection factories.

8.0 Release Notes

  • 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.
  • Added Open.Database.Extensions.MSSqlClient for Microsoft.Data.SqlClient support.
  • .NET 8.0 added to targets to ensure potential compliation and performance improvements are available.
  • Improved nullable integrity.

Example

var result = connectionFactory
   .StoredProcedure("[procedure name]")
   .AddParam("a",1)
   .AddParam("b",true)
   .AddParam("c","hello")
   .ExecuteScalar();

Extensions

Instead of writing this:

var myResult = new List<T>();
using(var reader = await mySqlCommand.ExecuteReaderAsync(CommandBehavior.CloseConnection))
{
    while(await reader.ReadAsync())
        myResult.Add(transform(reader));
}

Is now simplified to this:

var myResult = await mySqlCommand.ToListAsync(transform);

Deferred Transformation

In 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.

Results<T>() and ResultsAsync<T>()

Queues 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.

Optionally 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).

Examples

If all the columns in the database map exactly to a field: (A column that has no associated field/property is ignored.)

var people = cmd.Results<Person>();

If the database fields don't map exactly:

var people = cmd.Results<Person>(
 (Field:"FirstName", Column:"first_name"),
 (Field:"LastName", Column:"last_name")));

or

var people = cmd.Results<Person>(
 ("FirstName", "first_name"),
 ("LastName", "last_name"));

or

var people = cmd.Results<Person>(new Dictionary<string,string>{
 {"FirstName", "first_name"},
 {"LastName", "last_name"});

Retrieve() and RetrieveAsync()

Queues all the data. Returns a QueryResult<Queue<object[]>> containing the requested data and column information. The .AsDequeueingMappedEnumerable() extension will iteratively convert the results to dictionaries for ease of access.

ResultsAsync<T>

ResultsAsync<T>() is fully asynchronous from end-to-end but returns an IEnumerable<T> 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.

Transactions

Example:

// Returns true if the transaction is successful.
public static bool TryTransaction()
=> ConnectionFactory.Using(connection =>
    // Open a connection and start a transaction.
    connection.ExecuteTransactionConditional(transaction => {

        // First procedure does some updates.
        var count = transaction
            .StoredProcedure("[Updated Procedure]")
            .ExecuteNonQuery();

        // Second procedure validates the results.
        // If it returns true, then the transaction is committed.
        // If it returns false, then the transaction is rolled back.
        return transaction
            .StoredProcedure("[Validation Procedure]")
            .AddParam("@ExpectedCount", count)
            .ExecuteScalar<bool>();
    }));

open.database.extensions's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar electricessence avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

open.database.extensions's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.