Giter VIP home page Giter VIP logo

dynamicconsumenuget's Introduction

Dynamic Consume

Purpose

This package was prepared to eliminate the code confusion experienced when consuming the API in a .Net Core application.

Installation

You can add the package to your project via the Nuget package manager or cli

dotnet add package DynamicConsume --version 2.0.1

Usage

After installing the package, you must first register for the service in the Program.cs file.

builder.Services.AddDynamicConsume("api base url");

Afterwards, you can use it in the controller you will use by requesting it from the DI container.

The methods and parameters

Generic class methods

Task<List<T>> GetListAsync(string endpoint)
Task<T> GetByIdAsync(string endpoint, int id)
Task<List<T>> GetListByIdAsync(string endpoint, int id)
Task<int> PostAsync(string endpoint, object modelClass)
Task<int> PutAsync(string endpoint, object modelClass)
Task<int> DeleteAsync(string endpoint, int id)

Non generic class methods

Task<string> GetStringAsync(string endpoint)
Task<int> GetIntAsync(string endpoint)

Example Usage:

public class MyDataModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class MyService
{
    private readonly Consume<MyDataModel> _consume;

    public MyService(Consume<MyDataModel> consume)
    {
        _consume = consume;
    }

    public async Task<List<MyDataModel>> GetAllData()
    {
        return await _consume.GetListAsync("/data");
    }

    public async Task<MyDataModel> GetDataById(int id)
    {
        return await _consume.GetByIdAsync("/data", id);
    }

    public async Task<int> CreateData(MyDataModel model)
    {
        return await _consume.PostAsync("/data", model);
    }

    public async Task<int> UpdateData(MyDataModel model)
    {
        return await _consume.PutAsync("/data", model);
    }

    public async Task<int> DeleteData(int id)
    {
        return await _consume.DeleteAsync("/data", id);
    }
}

License

This project is licensed under the MIT License. See the LICENSE file for more information.

dynamicconsumenuget's People

Contributors

yahyaygmr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.