FGV.Lib.Persistence.SqlServer 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FGV.Lib.Persistence.SqlServer --version 1.0.0
                    
NuGet\Install-Package FGV.Lib.Persistence.SqlServer -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="FGV.Lib.Persistence.SqlServer" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FGV.Lib.Persistence.SqlServer" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FGV.Lib.Persistence.SqlServer" />
                    
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 FGV.Lib.Persistence.SqlServer --version 1.0.0
                    
#r "nuget: FGV.Lib.Persistence.SqlServer, 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.
#:package FGV.Lib.Persistence.SqlServer@1.0.0
                    
#: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=FGV.Lib.Persistence.SqlServer&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FGV.Lib.Persistence.SqlServer&version=1.0.0
                    
Install as a Cake Tool

FGV.Lib.Persistence.SqlServer

A .NET library that provides a robust and flexible SQL Server persistence layer for your applications. This library simplifies database operations through a repository pattern and unit of work implementation.

Overview

FGV.Lib.Persistence.SqlServer is designed to streamline database interactions by providing:

  • Repository pattern implementation for database operations
  • Unit of Work pattern for transaction management
  • Entity Framework Core integration
  • Base entity classes for consistent data modeling
  • Fluent configuration support for entity mapping

Components

Data Context

DataContext serves as the EF Core DbContext implementation, managing database connections and entity configurations.

Repository Pattern

  • IDatabaseRepository<TEntity> - Interface defining common CRUD operations
  • DatabaseRepository<TEntity> - Implementation of the repository interface for SQL Server

Unit of Work

  • IUnitOfWork - Interface for transaction management
  • UnitOfWork - Implementation of the unit of work pattern

Entity Framework

  • BaseEntity - Abstract base class for all entities
  • BaseIdentityEntity - Base class for entities with identity columns

Installation

Package Manager Console

Install-Package FGV.Lib.Persistence.SqlServer

.NET CLI

dotnet add package FGV.Lib.Persistence.SqlServer

Configuration

Add the following to your Startup.cs or program configuration:

services.AddDbContext<DataContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped(typeof(IDatabaseRepository<>), typeof(DatabaseRepository<>));

Add your connection string to appsettings.json:

{
"ConnectionStrings": {
    "DefaultConnection": "Server=YourServer;Database=YourDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}

Usage Examples

Defining Entities

public class Customer : BaseIdentityEntity
{
    public string Name { get; set; }
    public string Email { get; set; }
    public DateTime RegisterDate { get; set; }
}

Entity Configuration

public class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
    public void Configure(EntityTypeBuilder<Customer> builder)
    {
        builder.ToTable("Customers");
        
        builder.Property(c => c.Name)
            .IsRequired()
            .HasMaxLength(100);
            
        builder.Property(c => c.Email)
            .IsRequired()
            .HasMaxLength(150);
    }
}

Basic Repository Operations

// Inject the repository in your service or controller
private readonly IDatabaseRepository<Customer> _customerRepository;
private readonly IUnitOfWork _unitOfWork;

public CustomerService(
    IDatabaseRepository<Customer> customerRepository,
    IUnitOfWork unitOfWork)
{
    _customerRepository = customerRepository;
    _unitOfWork = unitOfWork;
}

// Get all customers
public async Task<IEnumerable<Customer>> GetAllCustomersAsync()
{
    return await _customerRepository.GetAllAsync();
}

// Get customer by ID
public async Task<Customer> GetCustomerByIdAsync(int id)
{
    return await _customerRepository.GetByIdAsync(id);
}

// Add a new customer
public async Task AddCustomerAsync(Customer customer)
{
    await _customerRepository.AddAsync(customer);
    await _unitOfWork.CommitAsync();
}

// Update a customer
public async Task UpdateCustomerAsync(Customer customer)
{
    _customerRepository.Update(customer);
    await _unitOfWork.CommitAsync();
}

// Delete a customer
public async Task DeleteCustomerAsync(int id)
{
    var customer = await _customerRepository.GetByIdAsync(id);
    if (customer != null)
    {
        _customerRepository.Delete(customer);
        await _unitOfWork.CommitAsync();
    }
}

Using Specification Pattern

// Create a specification
public class ActiveCustomersSpecification : BaseSpecification<Customer>
{
    public ActiveCustomersSpecification()
    {
        AddCriteria(c => c.IsActive);
        AddOrderBy(c => c.Name);
        AddInclude(c => c.Orders);
    }
}

// Use the specification
public async Task<IEnumerable<Customer>> GetActiveCustomersAsync()
{
    var spec = new ActiveCustomersSpecification();
    return await _customerRepository.ListAsync(spec);
}

Transaction Management

public async Task ProcessOrderAsync(Order order, Payment payment)
{
    try
    {
        await _unitOfWork.BeginTransactionAsync();
        
        await _orderRepository.AddAsync(order);
        await _paymentRepository.AddAsync(payment);
        
        await _unitOfWork.CommitTransactionAsync();
    }
    catch (Exception)
    {
        await _unitOfWork.RollbackTransactionAsync();
        throw;
    }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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.  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 (1)

Showing the top 1 NuGet packages that depend on FGV.Lib.Persistence.SqlServer:

Package Downloads
FGV.Lib.Api

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 153 7/1/2025
1.0.0 709 5/29/2025