Ridavei.Settings.DbAbstractions 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ridavei.Settings.DbAbstractions --version 1.0.0                
NuGet\Install-Package Ridavei.Settings.DbAbstractions -Version 1.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.
<PackageReference Include="Ridavei.Settings.DbAbstractions" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ridavei.Settings.DbAbstractions --version 1.0.0                
#r "nuget: Ridavei.Settings.DbAbstractions, 1.0.0"                
#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 Ridavei.Settings.DbAbstractions as a Cake Addin
#addin nuget:?package=Ridavei.Settings.DbAbstractions&version=1.0.0

// Install Ridavei.Settings.DbAbstractions as a Cake Tool
#tool nuget:?package=Ridavei.Settings.DbAbstractions&version=1.0.0                

Ridavei.Settings.DbAbstractions

Latest release

NuGet Badge Ridavei.Settings.DbAbstractions

Library that contains abstract classes for manager and settings retriever used to connect to the database.

Using IDbConnection as parameter it's checked if the connection is broken or closed. If it's' broken then Close method is called. If the connection is in the closed state then it will be opened and closed as needed.

The ADbSettings has constant table and column names that can be used in creating queries and the table in the database. The values are shown below.

public abstract class ADbSettings : ASettings
{
    /// <summary>
    /// Name of the database table for storing settings.
    /// </summary>
    protected const string TableName = "RidaveiSettings";

    /// <summary>
    /// Name of the dictionary column name.
    /// </summary>
    protected const string DictionaryColumnName = "DictionaryName";

    /// <summary>
    /// Name of the settings key column name.
    /// </summary>
    protected const string SettingsKeyColumnName = "SettingsKey";
    
    /// <summary>
    /// Name of the settings value column name.
    /// </summary>
    protected const string SettingsValueColumnName = "SettingsValue";

    //Rest of the class
}

Example of use

Creating classes to load data from SQL Server

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using Ridavei.Settings.Base;
using Ridavei.Settings.DbAbstractions;

namespace Example
{
    internal sealed class SqlServerManager : ADbManager
    {
        public SqlServerManager(string connectionString) : base(SqlClientFactory.Instance, connectionString) { }

        public SqlServerManager(IDbConnection connection) : base(connection) { }

        protected override ADbSettings CreateDbSettings(string dictionaryName, DbProviderFactory dbFactory, string connectionString)
        {
            return new SqlServerSettings(dictionaryName, dbFactory, connectionString);
        }

        protected override ADbSettings CreateDbSettings(string dictionaryName, IDbConnection connection)
        {
            return new SqlServerSettings(dictionaryName, connection);
        }

        protected override bool TryGetDbSettingsObject(string dictionaryName, DbProviderFactory dbFactory, string connectionString, out ASettings settings)
        {
            settings = CreateDbSettings(dictionaryName, dbFactory, connectionString);
            return true;
        }

        protected override bool TryGetDbSettingsObject(string dictionaryName, IDbConnection connection, out ASettings settings)
        {
            settings = CreateDbSettings(dictionaryName, connection);
            return true;
        }
    }

    internal sealed class SqlServerSettings : ADbSettings
    {
        public SqlServerSettings(string dictionaryName, DbProviderFactory dbFactory, string connectionString) : base(dictionaryName, dbFactory, connectionString) { }

        public SqlServerSettings(string dictionaryName, IDbConnection connection) : base(dictionaryName, connection) { }

        protected override int AddOrUpdateValueInDb(IDbConnection connection, string key, string value)
        {
            using (IDbCommand cmd = connection.CreateCommand())
            {
                cmd.CommandText = "QUERY";

                return cmd.ExecuteNonQuery();
            }
        }

        protected override IReadOnlyDictionary<string, string> GetAllValuesFromDb(IDbConnection connection)
        {
            Dictionary<string, string> res = new Dictionary<string, string>();
            using (IDbCommand cmd = connection.CreateCommand())
            {
                cmd.CommandText = "QUERY";

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                        res.Add(reader.GetString(0), reader.IsDBNull(1) ? string.Empty : reader.GetString(1));
                }
            }

            return res;
        }

        protected override bool TryGetValueFromDb(IDbConnection connection, string key, out string value)
        {
            value = string.Empty;
            using (IDbCommand cmd = connection.CreateCommand())
            {
                cmd.CommandText = "QUERY";

                var obj = cmd.ExecuteScalar();
                if (obj != null && obj != DBNull.Value)
                {
                    value = obj.ToString();
                    return true;
                }
                return false;
            }
        }
    }
}

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 netcoreapp3.1 is compatible. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Ridavei.Settings.DbAbstractions:

Package Downloads
Ridavei.Settings.SqlServer

Builder extension to store settings keys and values in SQL Server database.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 322 4/18/2023
1.0.0 218 11/29/2022