Pustalorc.MySqlConnectorWrapper 5.0.0

.NET 6.0 .NET Standard 2.0 .NET Framework 4.8
NuGet\Install-Package Pustalorc.MySqlConnectorWrapper -Version 5.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.
dotnet add package Pustalorc.MySqlConnectorWrapper --version 5.0.0
<PackageReference Include="Pustalorc.MySqlConnectorWrapper" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pustalorc.MySqlConnectorWrapper --version 5.0.0
#r "nuget: Pustalorc.MySqlConnectorWrapper, 5.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 Pustalorc.MySqlConnectorWrapper as a Cake Addin
#addin nuget:?package=Pustalorc.MySqlConnectorWrapper&version=5.0.0

// Install Pustalorc.MySqlConnectorWrapper as a Cake Tool
#tool nuget:?package=Pustalorc.MySqlConnectorWrapper&version=5.0.0

MySql Database Wrapper NuGet

Library that wraps Connector/NET (aka: MySql.Data) and MySqlConnector in order to reduce duplicate code when interacting with the connectors.

Installation & Usage

Before you begin, please make sure that your current solution has installed the nuget package of this library.
You can find this package here.

Note that this package also utilizes the following package: DbConnectionWrapper

Configuration Setup

Firstly, you need a configuration for the connector to use. Without a configuration, the connector will not know what server to connect to, which database to use, which user to use, etc.

You will want to create a class and have it inherit the interface IConnectorConfiguration
You are free to include extra information in this class to use, such as table names.
For example:

public class DatabaseConfig : IMySqlConfiguration
{
    public string MySqlServerAddress => "127.0.0.1";
    public ushort MySqlServerPort => 3306;
    public string DatabaseUsername => "myUsername";
    public string DatabasePassword => "myPassword";
    public string DatabaseName => "myDatabase";
    public string ConnectionString => "";
}

Note: These are { get; } only properties, but you can make them { get; set; } if you wish to be able to modify them mid run-time, or if you wish to serialize them into a configuration file.
ConnectionString is also available and used on the connection string builder classes to build a full connection string with the provided details, and make the process easier for the user.

Wrapper setup

Once you have a configuration and you think you are ready to move on, you can then use the connector wrapping.
There are 2 default implementations of the wrapper, one for MySql.Data and one for MySqlConnector.

If either one of these doesn't fit your use case, you can always create your own by inheriting from MySqlConnectionWrapper and overriding things as necessary.
Please note, if you want to adopt the wrapping to other database systems, you can, and should, use this other project: DbConnectionWrapper.
This other project will not work for connetors that do not use the DbConnection class from ADO.NET.

Since both wrappers function the same and are abstract to the point where they are virtually identical in method calls, I'll be using the MySqlConnector wrapper:

public class MyDatabaseLayerClass
{
    private MySqlConnectorWrapper<DatabaseConfig> Database { get; }

    public MyDatabaseLayerClass(DatabaseConfig config)
    {
        Database = new MySqlConnectorWrapper<DatabaseConfig>(config);
        var exception = Database.TestConnection();

        if (exception is not null)
        {
            Log.Exception(exception);
            throw exception;
        }

        CheckCreateSchema();
    }

    private void CheckCreateSchema()
    {
        var output = Database.ExecuteQuery($"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar);

        if (output.Result is not null) return;

        Database.ExecuteQuery(new Query($"CREATE TABLE `{Configuration.TableName}` (`Id` INT NOT NULL, PRIMARY KEY (`Id`));"));
    }
}

As shown in the example above, you can execute queries in 2 ways, either by just providing the details of the query (the query string, the type of query, etc.) or constructing a Query class yourself with the same details.

The Query class holds basic information about a query. This includes the following:

  • A query string - This is the command that the underlying SQL server will execute.
  • The type of the query - This tells the wrapper if it should run the query as a non query, as a scalar query or as a reader.
  • A list of Parameters for the query - These are used in binding data to the query. The type for the parameters is an abstract class, so make sure to use the parameter types exposed by your connector.
  • A callback - This is ran after the query has fully executed, and passes the output, as well as the current DbConnection and DbTransaction for further query execution. The callback is useful when using asynchronous calls in a run and forget style.

And that's about it, you can run queries from here, or full transactions with multiple queries (for example creating a table and filling it with data, or doing a heavy operation on a DB where rolling back is needed in case of an error), or even add a caching layer to your database. The limit will be yours in here.

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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 90 7/27/2022
4.1.2 101 6/5/2022
4.1.1 87 6/5/2022
4.1.0 113 3/7/2022
4.0.0 113 2/11/2022
3.0.2 148 12/17/2021
3.0.1 156 10/12/2021
3.0.0 162 9/24/2021
2.3.0 345 10/5/2020
2.2.1 345 7/9/2020
2.2.0 311 7/7/2020