Mtf.Database 1.0.9

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

// Install Mtf.Database as a Cake Tool
#tool nuget:?package=Mtf.Database&version=1.0.9                

Mtf.Database BaseRepository Documentation

Overview

The Mtf.Database library provides an abstract BaseRepository class for performing CRUD (Create, Read, Update, Delete) operations in SQL databases. It supports both SQLite and SQL Server, with the ability to switch between them based on a type parameter. The repository uses Dapper for simplified data access and manages SQL scripts for database operations within transactions.

This documentation covers setup, constructors, properties, methods, and example usage for BaseRepository, providing flexibility and control over database interactions in .NET applications.

Installation

To use Mtf.Database, add the package to your project:

  1. Add Package: Run the following command in your project directory:

    dotnet add package Mtf.Database
    
  2. Include the Namespace: Add the Mtf.Database namespace at the beginning of your code:

    using Mtf.Database;
    

Class: BaseRepository<T>

The BaseRepository class provides common database operations, such as executing scripts and querying data. The class supports both SQLite and SQL Server, with automatic handling of transactions to ensure data consistency.

Constructors

The BaseRepository class provides two constructors to support different database engines:

  • BaseRepository()
    Uses SQLite as the default database engine if no type is specified.

  • BaseRepository(DatabaseType type)
    Allows selection of either SQLite or SQL Server as the database engine.

Properties

Property Type Description
DbProvider DbProviderType Choose your DbProvider (SQLite, SqlServer).
ConnectionString string Connection string for the database.
ScriptsToExecute List<string> List of SQL scripts to execute at initialization.
DatabaseScriptsAssembly Assembly The assembly of the database scripts.
DatabaseScriptsLocation string The location of the database scripts.

Enum: DatabaseType

Defines the database types supported by BaseRepository.

Enum Value Description
SQLite Uses SQLite as the database engine.
SQLServer Uses SQL Server as the database engine.

Methods

Transactional Execution
  • T ExecuteInTransaction(Func<IDbConnection, IDbTransaction, T> operation)
    Executes a transactional operation and returns a result.

  • void ExecuteInTransaction(Action<IDbConnection, IDbTransaction> operation)
    Executes a transactional operation with no return value.

Query and Execution
  • List<T> Query(string scriptName)
    Executes a query script and returns a list of results.

  • List<T> Query(string scriptName, object param)
    Executes a parameterized query script and returns a list of results.

  • T QuerySingleOrDefault(string scriptName, object param)
    Executes a single-result query with parameters and returns the result.

  • void Execute(string scriptName, object param)
    Executes a script with parameters without returning a value.

  • TResultType ExecuteScalar<TResultType>(string scriptName, object param)
    Executes a script and returns a scalar result of specified type.

CRUD Operations
  • T Get(int id)
    Retrieves a single record by ID.

  • List<T> GetAll()
    Retrieves all records for the entity.

  • List<T> GetWhere(object param)
    Retrieves records that match the specified parameters.

  • void Delete(int id)
    Deletes a single record by ID.

  • void DeleteWhere(object param)
    Deletes records matching specified parameters.

Example Usage

using Mtf.Database;
using Mtf.Database.Models;
using System;
using System.Collections.Generic;

public class ExampleUsage
{
    public static void Main()
    {
        // Set up connection string and scripts
        BaseRepository.DbProvider = DbProviderType.SQLite;
        BaseRepository.ConnectionString = "Data Source=MyAppDatabase.db;Version=3;";
        BaseRepository.DatabaseScriptsAssembly = Assembly.GetEntryAssembly();
        BaseRepository.DatabaseScriptsLocation = "MyApp.Database";
        BaseRepository.ScriptsToExecute = new List<string> { "CreateDatabase", "Migration1" };

        // Use BaseRepository with SQL Server
        var serverRepository = new ServerRepository<Server>();

        // Insert and retrieve data
        var server = serverRepository.Get(1);
        Console.WriteLine($"Server ID: {server.Id}");

        // Delete data
        serverRepository.Delete(1);
        Console.WriteLine("Server deleted.");
    }
}

Notes

  • Transactions: BaseRepository automatically wraps operations in transactions for data consistency.
  • Script Management: Define SQL scripts in ScriptsToExecute to automate setup tasks, such as creating tables.
  • Error Handling: Wrap methods in try-catch blocks to manage exceptions during database interactions.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
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
1.0.10 43 11/28/2024
1.0.9 70 11/26/2024
1.0.8 68 11/26/2024
1.0.7 77 11/22/2024
1.0.6 73 11/22/2024
1.0.5 81 11/9/2024