PolleriaJs.EntityFramework.Infrastructure.Core 1.0.2

dotnet add package PolleriaJs.EntityFramework.Infrastructure.Core --version 1.0.2
NuGet\Install-Package PolleriaJs.EntityFramework.Infrastructure.Core -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="PolleriaJs.EntityFramework.Infrastructure.Core" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PolleriaJs.EntityFramework.Infrastructure.Core --version 1.0.2
#r "nuget: PolleriaJs.EntityFramework.Infrastructure.Core, 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.
// Install PolleriaJs.EntityFramework.Infrastructure.Core as a Cake Addin
#addin nuget:?package=PolleriaJs.EntityFramework.Infrastructure.Core&version=1.0.2

// Install PolleriaJs.EntityFramework.Infrastructure.Core as a Cake Tool
#tool nuget:?package=PolleriaJs.EntityFramework.Infrastructure.Core&version=1.0.2

Entity Framework Core Repository and Unit of Work

Overview

This library provides a robust implementation of the Repository and Unit of Work patterns using Entity Framework Core. It abstracts database operations and provides a clean and testable way to interact with the database.

Features

  • Generic repository implementation (EntityRepository<TEntity>)
  • Unit of Work for managing transactions (UnitOfWork)
  • Support for LINQ queries and raw SQL
  • Easy-to-use API for CRUD operations

Installation

To use this library, ensure you have Entity Framework Core installed in your project. You can install it via NuGet package manager or the .NET CLI:

dotnet add package Microsoft.EntityFrameworkCore

Usage

Setting Up the DbContext

First, ensure you have a DbContext implementation in your project.

public class MyDbContext : DbContext
{
    // Define your DbSets
}

Repository Usage

The EntityRepository<TEntity> can be used as follows:

// Instantiate your DbContext
var dbContext = new MyDbContext();

// Create a repository for an entity
var repository = new EntityRepository<MyEntity>(dbContext);

// Adding an entity
repository.Add(new MyEntity { /* Set properties */ });

// Querying entities
var entities = repository.AsQueryable().Where(/* some conditions */);

// Save changes
dbContext.SaveChanges();

Unit of Work Usage

The UnitOfWork pattern is used to manage transactions:

var unitOfWork = new UnitOfWork(new MyDbContext());

// Start a transaction
unitOfWork.BeginTransaction();

try
{
    // Perform database operations
    var repository = unitOfWork.Repository<MyEntity>();
    repository.Add(new MyEntity { /* Set properties */ });

    // Commit the transaction
    unitOfWork.Commit();
}
catch
{
    // Rollback in case of an exception
    unitOfWork.RollBack();
}

Additional Notes

  • Ensure proper disposal of DbContext and UnitOfWork instances to release database connections.
  • Customize the repository and unit of work classes to fit your specific needs.

About the Author

Selvin Medina

Repository

Contributing

Contributions are welcome. Please follow the coding conventions and add tests for new features.

License

MIT.

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
1.0.2 221 11/18/2023
1.0.1 93 11/18/2023
1.0.0 92 11/18/2023

First Version