JMSL.Core.Dal 2.0.1

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package JMSL.Core.Dal --version 2.0.1
                    
NuGet\Install-Package JMSL.Core.Dal -Version 2.0.1
                    
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="JMSL.Core.Dal" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JMSL.Core.Dal" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="JMSL.Core.Dal" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JMSL.Core.Dal --version 2.0.1
                    
#r "nuget: JMSL.Core.Dal, 2.0.1"
                    
#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.
#:package JMSL.Core.Dal@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JMSL.Core.Dal&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=JMSL.Core.Dal&version=2.0.1
                    
Install as a Cake Tool

JMSL.Core.Dal

Lightweight SQL Server database access layer using the Unit of Work and Repository patterns on top of ADO.NET + Dapper.

Package ID: JMSL.Core.Dal Version: 2.0.1 Target Framework: .NET 8.0 License: MIT Maintained by: JMSL IT — Jaykay Marketing Services (Pvt) Limited


Dependencies

Package Version
Microsoft.Data.SqlClient 5.1.5

Dapper is not a direct dependency — install it separately in your consuming project if you want query helpers.


Core Types

Type Description
DalSession Opens a SQL connection and exposes a UnitOfWork. Always use inside a using block.
IUnitOfWork Exposes the active IDbConnection and transaction control (Begin, Commit, Rollback).
BaseSQLRepository Abstract base class for repositories. Provides Execute, WithConnection, and WithConnectionAsync helpers.

Patterns

Two injection patterns are supported. Choose one consistently per project.

The IUnitOfWork is resolved from outside (e.g., from a service layer) and injected via the constructor. The repository stores it as _unitOfWork and uses it for all calls.

public class ProductRepository : BaseSQLRepository, IProductRepository
{
    private const string SP_GET_ALL   = "dbo.GetAllProducts";
    private const string SP_GET_BY_ID = "dbo.GetProductById";
    private const string SP_INSERT    = "dbo.InsertProduct";

    public ProductRepository(IUnitOfWork unitOfWork) : base(unitOfWork) { }

    // Uses stored _unitOfWork — no unitOfWork param needed
    public IEnumerable<Product> GetAll() =>
        WithConnection(c => c.Query<Product>(SP_GET_ALL, commandType: CommandType.StoredProcedure));

    public async Task<Product?> GetByIdAsync(int id)
    {
        var parameters = new DynamicParameters();
        parameters.Add("@ProductId", id, DbType.Int32);

        return await WithConnectionAsync(c =>
            c.QuerySingleOrDefaultAsync<Product>(SP_GET_BY_ID, param: parameters,
                commandType: CommandType.StoredProcedure));
    }

    public void Insert(Product product)
    {
        var parameters = new DynamicParameters();
        parameters.Add("@Name",  product.Name,  DbType.String);
        parameters.Add("@Price", product.Price, DbType.Decimal);

        Execute(c => c.Execute(SP_INSERT, param: parameters, commandType: CommandType.StoredProcedure));
    }
}

Caller:

// Read — no transaction
using var session = new DalSession(connectionString);
var repo = new ProductRepository(session.UnitOfWork);
var products = repo.GetAll();

// Write — with transaction
using var session = new DalSession(connectionString, autoBeginTransaction: true);
try
{
    var repo = new ProductRepository(session.UnitOfWork);
    repo.Insert(product);
    session.UnitOfWork.Commit();
}
catch
{
    session.UnitOfWork.Rollback();
    throw;
}

Pattern B — Per-Call Injection

IUnitOfWork is passed explicitly to each repository method. Useful when migrating from older code or when a single repository instance is reused across multiple sessions.

public class ProductRepository : BaseSQLRepository
{
    private const string SP_GET_ALL = "dbo.GetAllProducts";
    private const string SP_INSERT  = "dbo.InsertProduct";

    public ProductRepository() : base() { }

    public IEnumerable<Product> GetAll(IUnitOfWork unitOfWork) =>
        WithConnection(c => c.Query<Product>(SP_GET_ALL, commandType: CommandType.StoredProcedure), unitOfWork);

    public void Insert(Product product, IUnitOfWork unitOfWork)
    {
        var parameters = new DynamicParameters();
        parameters.Add("@Name",  product.Name,  DbType.String);
        parameters.Add("@Price", product.Price, DbType.Decimal);

        Execute(c => c.Execute(SP_INSERT, param: parameters, commandType: CommandType.StoredProcedure), unitOfWork);
    }
}

Caller:

// Read — no transaction
using var session = new DalSession(connectionString);
var products = repo.GetAll(session.UnitOfWork);

// Write — with transaction
using var session = new DalSession(connectionString, autoBeginTransaction: true);
try
{
    repo.Insert(product, session.UnitOfWork);
    session.UnitOfWork.Commit();
}
catch
{
    session.UnitOfWork.Rollback();
    throw;
}

Transaction Behaviour

Scenario Approach
Single SP, no .NET transaction needed new DalSession(connectionString) — SP auto-commits internally
Multiple operations must be atomic new DalSession(connectionString, autoBeginTransaction: true), then Commit() or Rollback()
Transaction rolled back on exception UnitOfWork.Dispose() automatically rolls back any uncommitted transaction

IUnitOfWork does not expose the underlying IDbTransaction. Transaction enlistment is managed internally. Do not pass transaction: to Dapper calls — omit that parameter entirely.


Helper Methods on BaseSQLRepository

Method Returns Description
Execute(Action<IDbConnection>) void Run a command with no return value
WithConnection<T>(Func<IDbConnection, T>) T Run a query and return a result
WithConnectionAsync<T>(Func<IDbConnection, Task<T>>) Task<T> Async version of WithConnection
CalculateNoOfPages(int, int) int Utility: compute page count from total records and page size

All three connection methods have overloads for both constructor-injected and per-call IUnitOfWork.


© 2025 Jaykay Marketing Services (Pvt) Limited. All rights reserved.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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