Pustalorc.MySqlConnectorWrapper 5.0.3

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

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

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 is compatible. 
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

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.3 262 3/11/2023
5.0.2 229 2/9/2023
5.0.1 312 12/6/2022
5.0.0 398 7/27/2022
4.1.2 422 6/5/2022
4.1.1 420 6/5/2022
4.1.0 450 3/7/2022
4.0.0 652 2/11/2022
3.0.2 334 12/17/2021
3.0.1 292 10/12/2021
3.0.0 314 9/24/2021
2.3.0 518 10/5/2020
2.2.1 486 7/9/2020
2.2.0 481 7/7/2020

Updated all libraries to latest versions