SDCores 1.0.2

Suggested Alternatives

SDCores 1.2.3

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

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

SDCores in .Net core

1. AuthenticationConfig

Enables JWT-bearer authentication using the default scheme.

JWT bearer authentication performs authentication by extracting and validating a JWT token from the Authorization request header.

Using package : Microsoft.AspNetCore.Authentication.JwtBearer.

2. DependencyInjectionConfiguration

Why should use DependencyInjectionConfiguration ?

Usually we need:

public interface IServiceA
{
    void Do();
}

----------------------------------------------------------------------------------------------------

public class ServiceA : IServiceA
{
    void Do()
    {
        // implementation
    }
}

----------------------------------------------------------------------------------------------------

public class TestController : ControllerBase
{
    private readonly IServiceA _serviceA;

    public TestController(IServiceA serviceA)
    {
        _serviceA = serviceA;
    }
    ...
}

and then in Program.cs

using ...
var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddScoped<IServiceA, ServiceA>();
...

And if you have forgot to register your service(s) to the DI engine. This can be so annoying sometimes considering that you have already coded your service interface and its implementation and even used it by constructor injection in your controller or another service

System.InvalidOperationException: Unable to resolve service for type ...
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, ...

That's why you should use DependencyInjectionConfiguration

[DependencyInjectionAttribute(ServiceLifetime.Transient)]
public interface IServiceA
{
    void Do();
}

And then just add this line to your ConfigureService method of your Program.cs like this

builder.Services.AddDependencyInjectionConfiguration(typeof(Program));

In every service interface by using the DependencyInjectionAttribute annotation and passing your desired service lifetime, your service will be automatically registered in the dotnet core DI engine.

3. SwaggerGenConfig

Define one or more documents to be created by the Swagger generator.

Add one or more "securityDefinitions", describing how your API is protected, to the generated Swagger.

Adds a global security requirement.

Using package : Swashbuckle.AspNetCore.SwaggerGen.

4. AsposeUtility

Using Aspose.Cells version 23.8.0

Start Aspose.Cells with License: https://docs.aspose.com/cells/net/licensing/

Document for .Net: https://docs.aspose.com/cells/net/

5. ExcelUtility

An extension from Aspose.cells.

CheckExcel: check input templete format with sample template.

DownloadExcel: Download an Excel data file (.xlsx) using the SetDataSource function to dynamically dump data.

6. ExceptionHandlingMiddleware

A custom middleware that catches exception HttpStatusCode and write error message.

7. FilesUtility

Contains some functions to handle file upload, delete file, save file, get files tree folder, read file text.

8. OperationResult

A custom class Result.

9. PaginationUtility

A widget that returns pagination data.

10. SettingsConfigUtility

Get a valued stored in the appsettings.

public interface IServiceA
{
    void Do();
}

----------------------------------------------------------------------------------------------------

public class ServiceA : IServiceA
{
    private readonly IConfiguration _cofiguration;

    public ServiceA(IConfiguration cofiguration)
    {
        _cofiguration = cofiguration;
    }

    void Do()
    {
        var demo = _configuration.GetSection("AppSettings:Demo").Value;
        var mailSettingServer _configuration.GetSection("MailSettingServer").Get<MailSettingServer>();
        ...
    }
}

You don't need to declare them (IConfiguration) in each Service or Controller anymore, use the following:

public interface IServiceA
{
    void Do();
}

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

public class ServiceA :IServiceA
{

    void Do()
    {
        var demo = SettingsConfigUtility.GetCurrentSettings("AppSettings:Factory");
        var mailSettingServer = SettingsConfigUtility<MailSettingServer>.GetCurrentSettings("MailSettingServer");
        ...
    }
}

11. 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 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. 
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.2.6 113 3/15/2024
1.2.5 243 10/30/2023
1.2.4 124 10/20/2023
1.2.3 132 10/10/2023
1.2.2 159 9/15/2023
1.2.1 143 9/15/2023
1.2.0 124 9/14/2023
1.0.2 148 9/6/2023
1.0.1 150 8/18/2023
1.0.0 174 8/17/2023