FGV.Lib.Persistence.SqlServer
1.1.0
dotnet add package FGV.Lib.Persistence.SqlServer --version 1.1.0
NuGet\Install-Package FGV.Lib.Persistence.SqlServer -Version 1.1.0
<PackageReference Include="FGV.Lib.Persistence.SqlServer" Version="1.1.0" />
<PackageVersion Include="FGV.Lib.Persistence.SqlServer" Version="1.1.0" />
<PackageReference Include="FGV.Lib.Persistence.SqlServer" />
paket add FGV.Lib.Persistence.SqlServer --version 1.1.0
#r "nuget: FGV.Lib.Persistence.SqlServer, 1.1.0"
#:package FGV.Lib.Persistence.SqlServer@1.1.0
#addin nuget:?package=FGV.Lib.Persistence.SqlServer&version=1.1.0
#tool nuget:?package=FGV.Lib.Persistence.SqlServer&version=1.1.0
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 operationsDatabaseRepository<TEntity>
- Implementation of the repository interface for SQL Server
Unit of Work
IUnitOfWork
- Interface for transaction managementUnitOfWork
- Implementation of the unit of work pattern
Entity Framework
BaseEntity
- Abstract base class for all entitiesBaseIdentityEntity
- 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 | Versions 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. |
-
net7.0
- FluentValidation (>= 11.9.0)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- RepositoryHelpers (>= 2.8.0)
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.