The Generic API Abstraction Layer is a C# library that provides generic CRUD (Create, Read, Update, Delete) operations for working with objects in an API. It offers a class called GenericCrudProvider<TObj, TKey>
that implements the ICrudProvider<TObj, TKey>
interface.
The Generic API Abstraction Layer has the following dependencies:
GenericApiAbstractionLayer.Extensions
GenericApiAbstractionLayer.Interfaces
Microsoft.Extensions.Logging
Newtonsoft.Json
Make sure to install and reference these dependencies in your project.
To use the Generic API Abstraction Layer in your project, follow these steps:
- Add the
GenericApiAbstractionLayer
namespace to your project. - Install the required dependencies listed in the Dependencies section.
To use the Generic API Abstraction Layer, you need to follow these steps:
- Create an instance of the
HttpClient
class to make API requests. - Instantiate the
GenericCrudProvider<TObj, TKey>
class, providing anILogger
instance and theHttpClient
instance. - Use the methods provided by the
GenericCrudProvider<TObj, TKey>
class to perform CRUD operations on your objects.
The GenericCrudProvider<TObj, TKey>
class provides the following methods:
Task<IEnumerable<TObj>?> GetAsync(bool includeRelationships = false)
: Gets a collection of objects asynchronously. Returns a collection of objects if successful; otherwise,null
. You can specify theincludeRelationships
parameter to determine whether to include relationships in the result.Task<TObj?> GetAsync(TKey id, bool includeRelationships = false)
: Gets an object by its ID asynchronously. Returns the object if found; otherwise,null
. You can specify theincludeRelationships
parameter to determine whether to include relationships in the result.Task<TObj?> PostAsync(TObj obj)
: Creates a new object asynchronously. Returns the created object if successful; otherwise,null
.Task<TObj?> PutAsync(TKey id, TObj obj)
: Updates an object asynchronously. Returns the updated object if successful; otherwise,null
.Task<TObj?> DeleteAsync(TKey id)
: Deletes an object asynchronously. Returns the deleted object if successful; otherwise,null
.
Here's an example of how to use the Generic API Abstraction Layer:
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System.Threading.Tasks;
using GenericApiAbstractionLayer;
// Create an instance of HttpClient
var httpClient = new HttpClient();
// Create an instance of ILogger
var logger = new Logger<GenericCrudProvider<MyObject, int>>();
// Instantiate the GenericCrudProvider
var crudProvider = new GenericCrudProvider<MyObject, int>(logger, httpClient);
// Perform CRUD operations
var objects = await crudProvider.GetAsync();
var newObject = await crudProvider.PostAsync(new MyObject());
var updatedObject = await crudProvider.PutAsync(id, new MyObject());
var deletedObject = await crudProvider.DeleteAsync(id);