GenericRepository.EFCore 1.0.6

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

// Install GenericRepository.EFCore as a Cake Tool
#tool nuget:?package=GenericRepository.EFCore&version=1.0.6                

GenericRepository

  • This is a generic repository of basic CRUD operation
  • In This read entities with expression filters, sorting and include other dependent entities
  • It also return a queryable entity on which you can apply other linq operation
  • In this version added GetByName() method and GetItemsAsync() change into GetAllAsync() with no predicate for detail go through examples

How to Use

Method : 1

public interface IUnitOfWork : IDisposable
{
    // map repository name as DbSet in DbContext for convineant
    IRepository<Category> Categories { get; }
    IRepository<Product> Products { get; }

    Task<int> SaveChangesAsync();
}


public class UnitOfWork : IUnitOfWork
{
    private readonly AppDbContext _contex;

    public UnitOfWork(AppDbContext contex)
    {
        _contex = contex;

        Categories = new Repository<Category, AppDbContext>(_contex);
        Products = new Repository<Product, AppDbContext>(_contex);
    }


    public IRepository<Category> Categories { get; private set; }
    public IRepository<Product> Products { get; private set; }



    public void Dispose()
        => _contex.Dispose();

    public async Task<int> SaveChangesAsync()
        => await _contex.SaveChangesAsync();
}

In Program.cs

// &lt; for '<' and &gt; for '>'
services.AddTransient<IUnitOfWork, UnitOfWork>();

Method : 2

public interface IProductRepository : IRepository<Product>
{
    bool Status();
}


public class ProductRepository(TestDbContext context)
    : Repository<Product, TestDbContext>(context), IProductRepository
{
    public bool Status()
        => true;
}
public class UnitOfWork : IUnitOfWork
{
    private readonly AppDbContext _contex;

    public UnitOfWork(AppDbContext contex)
    {
        _contex = contex;

        Categories = new Repository<Category, AppDbContext>(_contex);
        Products = new ProductRepository(_contex);
    }


    public IRepository<Category> Categories { get; private set; }
    public IRepository<Product> Products { get; private set; }



    public void Dispose()
        => _contex.Dispose();

    public async Task<int> SaveChangesAsync()
        => await _contex.SaveChangesAsync();
}

In Endpoint OR Controller

app.MapGet("/api/products", async (IUnitOfWork unitOfWork) =>
{
    var products = await unitOfWork.Products.GetAllAsync();
    return Resutls.Ok(products);
});

app.MapGet("/api/products/{id:int}", async (int id, IUnitOfWork unitOfWork) =>
{
    var products = await unitOfWork.Products.GetByIdAsync(id);
    return Resutls.Ok(products);
});

app.MapGet("/api/products/{id:int}", async (int id, IUnitOfWork unitOfWork) =>
{
    var products = await unitOfWork.Products.FirstOrDefaultAsync(x=>x.Id == id);
    return Resutls.Ok(products);
});

app.MapGet("/api/products/{name}", async (string name, IUnitOfWork unitOfWork) =>
{
    var products = await unitOfWork.Products.GetByNameAsync(name);
    return Resutls.Ok(products);
});

app.MapGet("/api/products/{name}", async (string name, IUnitOfWork unitOfWork) =>
{
    var products = await unitOfWork.Products.FirstOrDefaultAsync(x=>x.Name == name);
    return Resutls.Ok(products);
});


app.MapPost("/api/product/create", async (IUnitOfWork unitOfWork, Product product) =>
{
    await unitOfWork.Products.AddAsync(product);
    await unitOfWork.SaveChangesAsync()

    return Results.Ok(new { Product = product, Status = "Success" });
});

app.MapPost("/api/product/{id:int}", async (int id, IUnitOfWork unitOfWork) =>
{
    var product = await unitOfWork.Products.GetEntity()
                                    .Include(x=>x.Category)
                                    .Where(x=>x.Id == id)
                                    .FirstOrDefaultAsync();

    var products = await unitOfWork.Products.GetEntity()
                                    .Include(x=>x.Category)
                                    .Where(x=>x.Id == id)
                                    .ToListAsync();

    return Results.Ok(new { Product = product, Status = "Success" });
});
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. 
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
2.0.2 104 9/7/2024
2.0.1 95 9/7/2024
2.0.0 128 3/28/2024
1.0.6 106 3/26/2024
1.0.5 98 3/26/2024
1.0.2 130 3/19/2024
1.0.1 106 3/18/2024
1.0.0 112 3/18/2024