TeymurDevv.UnitOfWorkPlus 1.0.0

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

TeymurDevv.UnitOfWorkPlus

📌 Introduction

TeymurDevv.UnitOfWorkPlus is a lightweight, generic Unit of Work and Repository pattern for Entity Framework Core. This package simplifies database operations by providing a structured Unit of Work and Generic Repository implementation, allowing you to easily manage transactions and repositories.


🚀 Features

Generic Repository with built-in support for filtering, pagination, and tracking options.
Unit of Work pattern to manage database transactions efficiently.
Automatic Repository Registration for clean dependency injection.
Works with Any DbContext – supports all Entity Framework Core providers.
NuGet-Compatible for easy integration into any .NET project.


📦 Installation

Install via NuGet:

 dotnet add package TeymurDevv.UnitOfWorkPlus

OR manually add it to your .csproj file:

<PackageReference Include="TeymurDevv.UnitOfWorkPlus" Version="1.0.0" />

⚙️ Configuration

1️⃣ Register Services in Program.cs (or Startup.cs)

In .NET 6+, modify Program.cs:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TeymurDevv.UnitOfWorkPlus;

var builder = WebApplication.CreateBuilder(args);

// Add DbContext
builder.Services.AddDbContext<MyDbContext>(options =>
    options.UseSqlServer("YourConnectionString"));

// Register UnitOfWork
builder.Services.AddUnitOfWork<MyDbContext>();

var app = builder.Build();

For .NET 5 and earlier, add this in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
    services.AddUnitOfWork<MyDbContext>();
}

📌 Usage

2️⃣ Inject IUnitOfWork and Use Repositories

Once registered, you can inject IUnitOfWork in your services and access repositories dynamically.

public class CategoryService
{
    private readonly IUnitOfWork _unitOfWork;

    public CategoryService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public async Task<List<Category>> GetCategories()
    {
        return await _unitOfWork.Repository<Category>().GetAll();
    }

    public async Task AddCategory(Category category)
    {
        await _unitOfWork.Repository<Category>().Create(category);
        await _unitOfWork.SaveChangesAsync();
    }
}

🎯 Unit of Work API

IUnitOfWork Methods

Method Description
BeginTransactionAsync(CancellationToken cancellationToken = default) Begins a new database transaction asynchronously.
CommitTransactionAsync(CancellationToken cancellationToken = default) Commits the current database transaction asynchronously.
RollbackTransactionAsync(CancellationToken cancellationToken = default) Rolls back the current database transaction asynchronously.
SaveChangesAsync(CancellationToken cancellationToken = default) Saves all changes to the database asynchronously.

📌 Repository API

IRepository<T> Methods

Method Description
GetEntity(Expression<Func<T, bool>> predicate = null, bool AsnoTracking = false, int skip = 0, int take = 0, params Func<IQueryable<T>, IQueryable<T>>[] includes) Retrieves a single entity with optional filters, includes, and tracking settings.
GetAll(Expression<Func<T, bool>> predicate = null, bool AsnoTracking = false, int skip = 0, int take = 0, params Func<IQueryable<T>, IQueryable<T>>[] includes) Retrieves a list of entities with optional filters, pagination, and includes.
Create(T entity) Adds a new entity to the database.
Update(T entity) Updates an existing entity.
Delete(T entity) Deletes an entity from the database.
IsExists(Expression<Func<T, bool>> predicate = null) Checks if an entity exists based on a condition.
GetQuery(Expression<Func<T, bool>> predicate = null, bool AsnoTracking = false, params Func<IQueryable<T>, IQueryable<T>>[] includes) Returns an IQueryable<T> allowing for custom queries.

📌 Example Queries

Retrieve a Single Entity with Filters

var category = await _unitOfWork.Repository<Category>()
    .GetEntity(c => c.Name == "Technology");

Retrieve All with Includes & Pagination

var categories = await _unitOfWork.Repository<Category>()
    .GetAll(predicate: c => c.IsActive, take: 10, includes: q => q.Include(c => c.Products));

Update an Entity

var category = await _unitOfWork.Repository<Category>().GetEntity(c => c.Id == 1);
category.Name = "Science";
await _unitOfWork.Repository<Category>().Update(category);
await _unitOfWork.SaveChangesAsync();

Delete an Entity

var category = await _unitOfWork.Repository<Category>().GetEntity(c => c.Id == 1);
await _unitOfWork.Repository<Category>().Delete(category);
await _unitOfWork.SaveChangesAsync();

📌 Contributing

We welcome contributions! If you'd like to improve this library, feel free to fork the repository and submit a pull request.


📜 License

This project is licensed under the MIT License.


🛠 Support & Contact

For issues and feature requests, please create an issue on GitHub.

Happy Coding! 🚀

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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
1.0.4 132 2/26/2025
1.0.3 122 2/25/2025
1.0.2 119 2/25/2025
1.0.1 124 2/25/2025
1.0.0 125 2/25/2025