TeymurDevv.UnitOfWorkPlus 1.0.2

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

UnitOfWorkPlus - Usage Guide

📌 Introduction

UnitOfWorkPlus is a fully automated and dependency injection (DI) friendly implementation of the Unit of Work + Repository Pattern. It allows you to manage repositories dynamically without manually registering them.

🚀 Features

No manual repository registration required
Automatic detection of repositories
Direct repository access via _unitOfWork.ProductRepository
Works seamlessly with Dependency Injection (DI)
Supports dynamic access using _unitOfWork["Product"]


📌 1️⃣ Installation

  1. Add UnitOfWorkPlus package to your project:
    dotnet add package UnitOfWorkPlus
    

📌 2️⃣ Setup in Program.cs

Register UnitOfWork and DbContext in Dependency Injection (DI).

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using YourNamespace.Data; // Your ApplicationDbContext
using TeymurDevv.UnitOfWorkPlus;

var builder = WebApplication.CreateBuilder(args);

// Register UnitOfWork and DbContext
builder.Services.AddUnitOfWork<ApplicationDbContext>();

var app = builder.Build();
app.Run();

📌 3️⃣ Inject IUnitOfWork and Use in a Service

Use IUnitOfWork in services to access repositories dynamically.

public class ProductService
{
    private readonly IUnitOfWork _unitOfWork;

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

    public async Task AddProduct(string name, decimal price)
    {
        var product = new Product { Name = name, Price = price };
        await _unitOfWork.Repository<Product>().Create(product);
        await _unitOfWork.SaveChangesAsync();
    }
}

📌 4️⃣ Inject IUnitOfWork in a Controller

Use IUnitOfWork in an API controller to perform CRUD operations.

[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
    private readonly IUnitOfWork _unitOfWork;

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

    [HttpPost]
    public async Task<IActionResult> AddProduct([FromBody] Product product)
    {
        await _unitOfWork.Repository<Product>().Create(product);
        await _unitOfWork.SaveChangesAsync();
        return Ok("Product added successfully.");
    }

    [HttpGet]
    public async Task<ActionResult<List<Product>>> GetProducts()
    {
        return await _unitOfWork.Repository<Product>().GetAll();
    }
}

📌 5️⃣ Dynamic Repository Access

You can dynamically access repositories by entity name.

var productRepo = (IRepository<Product>)_unitOfWork["Product"];
await productRepo.Create(new Product { Name = "Smartphone", Price = 799.99M });
await _unitOfWork.SaveChangesAsync();

🎯 Summary

Automatic repository detection
No need for manual repository registration
Seamless integration with ASP.NET Core
Direct repository access via _unitOfWork.Repository<Product>()
Dynamic repository access via _unitOfWork["Product"]

🚀 Now your UnitOfWork system is fully automated and production-ready! 🔥

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 121 2/26/2025
1.0.3 112 2/25/2025
1.0.2 108 2/25/2025
1.0.1 111 2/25/2025
1.0.0 115 2/25/2025