ThrinCheck.GenericHub 1.0.1

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

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

Generic Hub

Generic Hub is a versatile and comprehensive .NET package designed to streamline your data management needs. Whether you're building a small application or a large-scale enterprise system, Generic Hub provides essential features for generic repository operations, pagination, and standardized response handling.

Features

Generic Repository

  • Simplify your data access layer with a generic repository pattern.
  • Perform CRUD (Create, Read, Update, Delete) operations on any entity effortlessly.
  • Supports both DbContext and IdentityDbContext for seamless integration with your existing data contexts.

Generic Pagination

  • Easily paginate through large datasets with flexible pagination options.
  • Enhance performance and user experience by efficiently fetching and displaying data.

Generic Response

  • Standardize your API responses with a consistent structure for success, error, and validation messages.
  • Improve interoperability and client-side integration with predictable response formats.

Getting Started

Installation

Install the Generic Hub package via NuGet Package Manager Console:

Install-Package ThrinCheck.GenericHub

Usage Instructions

  1. Configuring the Repository: Begin by creating your repository class. Make sure it is generic and inherits from the GenericRepository class. Provide your DbContext as the type parameter. Additionally, create an interface for your repository:
public class RepositoryFactory<T> : GenericRepository<T, ApplicationDbContext>, IYourRepository<T> where T : class
{
    private ApplicationDbContext _context;

    public RepositoryFactory(ApplicationDbContext context) : base(context)
    {
        _context = context;
    }
}

In this example, RepositoryFactory<T> is a generic repository class specialized to handle entities of type T. It inherits functionalities from GenericRepository<T, ApplicationDbContext>.

  1. Creating the Interface: Alongside your repository class, define an interface to abstract the repository's methods and provide a contract for its implementation. This interface should extend the IGenericRepository<T> interface:
public interface IRepositoryFactory<T> : IGenericRepository<T, ApplicationDbContext> where T : class
{
    // Define additional methods specific to the Contact repository if needed
}

By creating this interface, you adhere to the principle of abstraction, allowing your repository class to be interchangeable and promoting code maintainability and flexibility.

  1. Registering Services: In your Program class, register the repository and its corresponding interface with the dependency injection container:
builder.Services.AddScoped(typeof(IRepositoryFactory<>), typeof(RepositoryFactory<>));
  1. Perform Operations: Use repository methods for CRUD operations, pagination, and handling responses:
var entity = await repository.GetByIdAysnc(id);
var entities = await repository.GetAllAsync();
await repository.InsertAsync(newEntity);
await repository.UpdateAsync(entityToUpdate);
await repository.DeleteAsync(id);
  1. Using the Repository in Controller: In your controller, inject the repository interface and use it to perform CRUD operations:
[Route("api/[controller]")]
[ApiController]
public class ContactController : ControllerBase
{
    private readonly IRepositoryFactory<Contact> _repository;

    public ContactController(IRepositoryFactory<Contact> repository)
    {
        _repository = repository;
    }

    [HttpPost]
    public async Task<IActionResult> AddContactAsync(AddContact addContact)
    {
        // Create a new contact instance
        Contact contact = new()
        {
            Address = addContact.Address,
            Email = addContact.Email,
            City = addContact.City,
            Country = addContact.Country,
            Name = addContact.Name,
        };

        // Insert the contact into the repository
        var isSuccess = await _repository.InsertAsync(contact);

        // Return appropriate response based on the operation result
        if (isSuccess)
        {
            return Ok(contact); // Assuming you want to return the added contact
        }
        else
        {
            return BadRequest();
        }
    }

    [HttpGet]
    public async Task<IActionResult> GetContacts()
    {
        // Retrieve all contacts from the repository
        var allContacts = await _repository.GetAllAsync();
        return Ok(allContacts);
    }
}

In this controller example, the ContactController receives an instance of IRepositoryFactory<Contact> via constructor injection. It then uses this repository to perform operations such as adding a contact (AddContactAsync) and retrieving all contacts (GetContacts).

  1. Pagination Usage: To paginate the data, use the GenericPagination class as follows:
int pageNumber = 1;
int pageSize = 10;
var pagedData = GenericPagination<Contact>.PaginateItem(allContacts.ToList(), pageNumber, pageSize);
 [HttpGet]
    public async Task<IActionResult> GetContacts()
    {
        // Retrieve all contacts from the repository
        var allContacts = await _repository.GetAllAsync();

        // Pagination
        int pageNumber = 1;
        int pageSize = 10;
        var pagedData = GenericPagination<Contact>.PaginateItem(allContacts.ToList(), pageNumber, pageSize);

        // Return paginated data
        return Ok(pagedData);
    }

Pagination Sample Response

{
  "totalPages": 1,
  "pageSize": 10,
  "pageNumber": 1,
  "totalItems": 3,
  "hasPrevious": false,
  "hasNext": false,
  "items": [
    {
      "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
      "name": "John Doe",
      "email": "Johndoe@gmail.com",
      "address": "Texas",
      "city": "Texas",
      "country": "USA"
    },
    {
      "id": "4bf67ae2-0814-4dc9-917d-c7c38b4dcdfd",
        "name": "Adam Sam",
      "email": "Adam@gmail.com",
      "address": "New York",
      "city": "New York",
      "country": "USA"
    },
    {
      "id": "b8df1886-1f0d-4349-a3b2-c9a8c005033d",
      "name": "Alexa Bob",
      "email": "Alexa@gmail.com",
      "address": "Lagos",
      "city": "Lagos",
      "country": "Nigeria"
    }
  ]
}

This will paginate the list of contacts (allContacts) based on the specified page number and page size.

  1. Generic Response Usage: To return a generic response, utilize the GenericResponse class as shown below:
[HttpGet]
public async Task<IActionResult> GetContacts()
{
    // Retrieve all contacts from the repository
    var allContacts = await _repositoryFactory.GetAllAsync();

    // Pagination
    int pageNumber = 1;
    int pageSize = 10;
    var pagedData = GenericPagination<Contact>.PaginateItem(allContacts.ToList(), pageNumber);
    
    // Create a success response with the paginated data
    var response = GenericResponse<Repo.GenericPagination<Contact>>.SuccessResponse(pagedData, "Successfully Fetched all Contact");

    // Return the response
    return Ok(response);
}

Generic Response Sample Response

{
  "success": true,
  "message": "Successfully Fetched all Contact",
  "data": {
    "totalPages": 1,
    "pageSize": 10,
    "pageNumber": 1,
    "totalItems": 3,
    "hasPrevious": false,
    "hasNext": false,
    "items": [
      {
      "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
      "name": "John Doe",
      "email": "Johndoe@gmail.com",
      "address": "Texas",
      "city": "Texas",
      "country": "USA"
    },
    {
      "id": "4bf67ae2-0814-4dc9-917d-c7c38b4dcdfd",
        "name": "Adam Sam",
      "email": "Adam@gmail.com",
      "address": "New York",
      "city": "New York",
      "country": "USA"
    },
    {
      "id": "b8df1886-1f0d-4349-a3b2-c9a8c005033d",
      "name": "Alexa Bob",
      "email": "Alexa@gmail.com",
      "address": "Lagos",
      "city": "Lagos",
      "country": "Nigeria"
    }
    ]
  }
}

Contributions and Support

Generic Hub is open-source and welcomes contributions from the community. If you encounter any issues or have suggestions for improvement, please feel free to submit a pull request or open an issue on GitHub.

For support and questions, please reach out to us via email or join our community on LinkedIn.

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 was computed.  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.0.3 114 10/7/2024
1.0.2 100 10/6/2024
1.0.1 134 3/26/2024