SDCores.Repository 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SDCores.Repository --version 1.0.0                
NuGet\Install-Package SDCores.Repository -Version 1.0.0                
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.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SDCores.Repository --version 1.0.0                
#r "nuget: SDCores.Repository, 1.0.0"                
#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.0

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

SDCores.Repository in .Net core

Repository Base

public interface IRepository<T> where T : class
{
    Task<T> FindById(object id);

    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);

    ...
}

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 74 5/3/2024
1.0.0 187 1/5/2024