SDCores.Repository 1.0.1

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

// Install SDCores.Repository as a Cake Tool
#tool nuget:?package=SDCores.Repository&version=1.0.1

SDCores.Repository in .Net core

Repository Base

public interface IRepository<T> where T : class
{
    Task<T> FindById(object id);
    Task<T> FindAsync(params object[] keyValues);

    IQueryable<T> FindAll(bool? noTracking = false);

    IQueryable<T> FindAll(Expression<Func<T, bool>> predicate, bool? noTracking = false);

    void Add(T entity);

    void AddMultiple(List<T> entities);

    void Update(T entity);

    void UpdateMultiple(List<T> entities);

    void Remove(T entity);

    void Remove(object id);

    void RemoveMultiple(List<T> entities);

    bool All(Expression<Func<T, bool>> predicate);

    Task<bool> AllAsync(Expression<Func<T, bool>> predicate);

    bool Any();

    bool Any(Expression<Func<T, bool>> predicate);

    Task<bool> AnyAsync();

    Task<bool> AnyAsync(Expression<Func<T, bool>> predicate);

    T FirstOrDefault(bool? noTracking = false);

    T FirstOrDefault(Expression<Func<T, bool>> predicate, bool? noTracking = false);

    Task<T> FirstOrDefaultAsync(bool? noTracking = false);

    Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate, bool? noTracking = false);

    int Count();

    int Count(Expression<Func<T, bool>> predicate);

    Task<int> CountAsync();
    
    Task<int> CountAsync(Expression<Func<T, bool>> predicate);

    T LastOrDefault(bool? noTracking = false);
    
    T LastOrDefault(Expression<Func<T, bool>> predicate, bool? noTracking = false);

    Task<T> LastOrDefaultAsync(bool? noTracking = false);
    
    Task<T> LastOrDefaultAsync(Expression<Func<T, bool>> predicate, bool? noTracking = false);

    decimal Sum(Expression<Func<T, decimal>> selector);
    
    decimal? Sum(Expression<Func<T, decimal?>> selector);
    
    decimal Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal>> selector);
    
    decimal? Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal?>> selector);

    Task<decimal> SumAsync(Expression<Func<T, decimal>> selector);
    
    Task<decimal?> SumAsync(Expression<Func<T, decimal?>> selector);
    
    Task<decimal> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal>> selector);
    
    Task<decimal?> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal?>> selector);

    int Sum(Expression<Func<T, int>> selector);
    
    int? Sum(Expression<Func<T, int?>> selector);
    
    int Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector);
    
    int? Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, int?>> selector);

    Task<int> SumAsync(Expression<Func<T, int>> selector);
    
    Task<int?> SumAsync(Expression<Func<T, int?>> selector);
    
    Task<int> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector);
    
    Task<int?> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, int?>> selector);

    long Sum(Expression<Func<T, long>> selector);
    
    long? Sum(Expression<Func<T, long?>> selector);
    
    long Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, long>> selector);
    
    long? Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, long?>> selector);

    Task<long> SumAsync(Expression<Func<T, long>> selector);
    
    Task<long?> SumAsync(Expression<Func<T, long?>> selector);
    
    Task<long> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, long>> selector);
    
    Task<long?> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, long?>> selector);

    float Sum(Expression<Func<T, float>> selector);
    
    float? Sum(Expression<Func<T, float?>> selector);
    
    float Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, float>> selector);
    
    float? Sum(Expression<Func<T, bool>> predicate, Expression<Func<T, float?>> selector);

    Task<float> SumAsync(Expression<Func<T, float>> selector);
    
    Task<float?> SumAsync(Expression<Func<T, float?>> selector);

    Task<float> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, float>> selector);

    Task<float?> SumAsync(Expression<Func<T, bool>> predicate, Expression<Func<T, float?>> selector);

    T Max();
    
    TResult Max<TResult>(Expression<Func<T, TResult>> selector);

    Task<T> MaxAsync();
    
    Task<TResult> MaxAsync<TResult>(Expression<Func<T, TResult>> selector);

    T Min();
    
    TResult Min<TResult>(Expression<Func<T, TResult>> selector);

    Task<T> MinAsync();
    
    Task<TResult> MinAsync<TResult>(Expression<Func<T, TResult>> selector);

    IQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> selector);
    
    IQueryable<TResult> Select<TResult>(Expression<Func<T, int, TResult>> selector);

    IQueryable<TResult> Select<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector);
    
    IQueryable<TResult> Select<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, int, TResult>> selector);

    TResult SelectFirst<TResult>(Expression<Func<T, TResult>> selector);
    
    TResult SelectFirst<TResult>(Expression<Func<T, int, TResult>> selector);

    TResult SelectFirst<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector);
    
    TResult SelectFirst<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, int, TResult>> selector);

    Task<List<TResult>> SelectAsync<TResult>(Expression<Func<T, TResult>> selector);
    
    Task<List<TResult>> SelectAsync<TResult>(Expression<Func<T, int, TResult>> selector);

    Task<List<TResult>> SelectAsync<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector);
    
    Task<List<TResult>> SelectAsync<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, int, TResult>> selector);

    Task<TResult> SelectFirstAsync<TResult>(Expression<Func<T, TResult>> selector);
    
    Task<TResult> SelectFirstAsync<TResult>(Expression<Func<T, int, TResult>> selector);

    Task<TResult> SelectFirstAsync<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector);
    
    Task<TResult> SelectFirstAsync<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, int, TResult>> selector);

    IQueryable<TDestination> MapTo<TDestination>() where TDestination : new();
    
    TDestination MapOne<TDestination>() where TDestination : new();

    Task<List<TDestination>> MapToAsync<TDestination>() where TDestination : new();
    
    Task<TDestination> MapOneAsync<TDestination>() where TDestination : new();
    ...
}

Dynamic Repository DBContext

public class Repository<T, DBContext> : IRepository<T> where T : class where DBContext : DbContext
{
    private readonly DBContext _context;

    public Repository(DBContext context)
    {
        _context = context;
    }
    ... 
}

And then, I create Accessor class

...
using Microsoft.EntityFrameworkCore.Storage;
using SDCores;

namespace API._Repositories
{
    [DependencyInjection(ServiceLifetime.Scoped)]
    public interface IRepositoryAccessor
    {
        IRepository<ModelA> ModelA{get;}
        ...
        Task<bool> Save();
        Task<IDbContextTransaction> BeginTransactionAsync();
    }
}
...
using Microsoft.EntityFrameworkCore.Storage;
using SDCores;

namespace API._Repositories
{
    public class RepositoryAccessor : IRepositoryAccessor
    {
        private DBContext _dbContext;
        public RepositoryAccessor(DBContext dbContext)
        {
            _dbContext = dbContext;
            ModelA = new Repository<ModelA, DBContext>(_dbContext);
            ...
        }

        public IRepository<ModelA> ModelA { get; set; }
        ...

        public async Task<bool> Save()
        {
            return await _dbContext.SaveChangesAsync() > 0;
        }

        public async Task<IDbContextTransaction> BeginTransactionAsync()
        {
            return await _dbContext.Database.BeginTransactionAsync();
        }
    }
}

And in Service, you use Accessor as follows

public interface IServiceA
{
    void Do();
}

----------------------------------------------------------------------------------------------------
using SDCores;

public class ServiceA : IServiceA
{
    private readonly IRepositoryAccessor _repoAccessor;

    public ServiceA(IRepositoryAccessor repoAccessor) {
        _repoAccessor = repoAccessor;
    }

    void Do()
    {
        var demo = _repoAccessor.ModelA.FirstOrDefaultAsync(x => ...);
        ...
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  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 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
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.1 4 5/3/2024
1.0.0 158 1/5/2024