Qrtix.RepositoryPattern.Abstractions 7.0.2

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

// Install Qrtix.RepositoryPattern.Abstractions as a Cake Tool
#tool nuget:?package=Qrtix.RepositoryPattern.Abstractions&version=7.0.2                

Repository Pattern Abstractions

NuGet Version NuGet Downloads GitHub Repo stars

A library for implementing generic repositories and unit of work with Entity Framework Core. This implementation uses a single instance of the DbContext for all repositories to avoid concurrency issues.

Consult the online documentation for more details.

[!Tip] While the Qrtix.RepositoryPattern.Abstractions provides the necessary abstractions for implementing generic repositories and unit of work, it's recommended to use one of the specialized libraries that build upon these abstractions for specific data access technologies. For Entity Framework Core, consider using the Qrtix.RepositoryPattern.EntityFrameworkCore library, which enhances compatibility and simplifies integration with EF Core features.

Table of Contents

Installation

Using the NuGet package manager console within Visual Studio run the following command:

Install-Package Ortix.RepositoryPattern.Abstractions

Or using the .NET Core CLI from a terminal window:

dotnet add package Qrtix.RepositoryPattern.Abstractions

Creating the DbContext

Create your DbContext inheriting from Microsoft.EntityFrameworkCore.DbContext:

public class MyDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }  

    protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
		base.OnModelCreating(modelBuilder);
	}
}

Configure your DbContext in the Program class:

builder.Services.AddDbContext<DbContext, SuinQShopModuleDbContext>(opt =>
    {
        opt.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString"));
    });

Creating the Repository Implementation

Create your repository implementation inheriting from IRepository<TEntity>:

public class ProductRepository : IRepository<TEntity> where TEntity : class
{
	private readonly DbSet<TEntity> _dbSet;
	
    
    public IQueryable<TEntity> Data => _dbSet;


	public async Task<IQueryable<TEntity>> GetManyAsync(Expression<Func<TEntity, bool>>? filters = null,
		bool disableTracking = true,
		IEnumerable<Expression<Func<TEntity, object>>>? includes = null,
		Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
		CancellationToken cancellationToken = default)
	{
		// implementation
	}

	public IQueryable<TEntity> GetMany(Expression<Func<TEntity, bool>>? filters = null,
		bool disableTracking = false,
		IEnumerable<Expression<Func<TEntity, object>>>? includes = null,
		Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null)
		{
            // implementation
        }

	// others implementation 
}

Creating the Unit Of Work implementation

Create your unit of work implementation inheriting from IUnitOfWork:

class UnitOfWork : IUnitOfWork
{
	private readonly DbContext _context;
	private IDbContextTransaction? _transaction;
	private readonly Dictionary<string, object> _repositories;

	public UnitOfWork(DbContext context)
	{
		_context = context;
		_repositories = new Dictionary<string, object>();
	}

	public void Dispose()
	{
		// implementation
	}


	public async ValueTask DisposeAsync()
	{
		// implementation
	}

	public IRepository<TEntity> Repository<TEntity>() where TEntity : class
	{
		// implementation
	}

	// others implementations

Injecting the RepositoryPattern's Services

Add the RepositoryPattern services to the Service Collection:

builder.Services.AddRepositoryPattern(options => {
    options.UseRepositoryImplementation(typeof(Repository<>);
    options.UseUnitOfWorkImplementation<UnitOfWork>();
});

The default scope for injected services is scoped. If you want to change it, refer to the next example:

builder.Services.AddRepositoryPattern(options => {
    options.UseRepositoryImplementation(typeof(Repository<>)
		.UseLifeTime(ServiceLifetime.Singletor);
        
    options.UseUnitOfWorkImplementation<UnitOfWork>()
        .UseLifetime(ServiceLifetime.Singleton);
});

Repository Pattern Implementations

While the RepositoryPattern.Abstractions provides the necessary abstractions for implementing generic repositories and unit of work, it's recommended to use one of the specialized libraries that build upon these abstractions for specific data access technologies.

For Entity Framework Core, consider using the RepositoryPattern.EntityFrameworkCore library, which enhances compatibility and simplifies integration with EF Core features. For more information consult the Readme file or the online documentation.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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 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 (1)

Showing the top 1 NuGet packages that depend on Qrtix.RepositoryPattern.Abstractions:

Package Downloads
Qrtix.RepositoryPattern.EntityFrameworkCore

This library provides a convenient way to implement the repository pattern with Entity Framework Core in your .NET applications. By using this library, you can decouple your data access logic from your business logic, resulting in a more maintainable and testable codebase.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.2 143 8/26/2024
7.0.1 134 7/18/2024
6.0.1 144 5/19/2024
6.0.0 130 5/18/2024
5.0.1 122 5/6/2024
5.0.0 181 5/6/2024
4.0.0 138 5/3/2024
2.0.2 148 4/26/2024
2.0.1 105 4/26/2024
2.0.0 174 4/25/2024
1.1.0 112 4/25/2024
1.0.1 118 4/23/2024
1.0.0 134 4/22/2024

f
- Fix bug in the `UnitOfWork` implementation checking.