eQuantic.Core.Api.Client 1.6.18

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

// Install eQuantic.Core.Api.Client as a Cake Tool
#tool nuget:?package=eQuantic.Core.Api.Client&version=1.6.18                

eQuantic.Core.Api Library

The eQuantic Core API provides all the implementation needed to publish standard APIs.

To install eQuantic.Core.Api, run the following command in the Package Manager Console

Install-Package eQuantic.Core.Api

Example of implementation

The data entities

[Table("orders")]
public class OrderData : EntityDataBase
{
    [Key]
    public string Id { get; set; } = string.Empty;
    public DateTime Date { get; set; }
    
    public virtual ICollection<OrderItemData> Items { get; set; } = new HashSet<OrderItemData>();
}

[Table("orderItems")]
public class OrderItemData : EntityDataBase, IWithReferenceId<OrderItemData, int>
{
    [Key]
    public int Id { get; set; }
    public int OrderId { get; set; }
    
    [ForeignKey(nameof(OrderId))]
    public virtual OrderData? Order { get; set; }
    
    [Required]
    [MaxLength(200)]
    public string Name { get; set; } = string.Empty;
}

The models

public class Order
{
    public string Id { get; set; } = string.Empty;
    public DateTime Date { get; set; }
}

public class OrderItem
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public string Name { get; set; } = string.Empty;
}

The request models

public class OrderRequest
{
    public DateTime? Date { get; set; }
}

public class OrderItemRequest
{
    public string? Name { get; set; }
}

The mappers

public class OrderMapper : IMapper<OrderData, Order>, IMapper<OrderRequest, OrderData>
{
    public Order? Map(OrderData? source)
    {
        return Map(source, new Order());
    }

    public Order? Map(OrderData? source, Order? destination)
    {
        if (source == null)
        {
            return null;
        }

        if (destination == null)
        {
            return Map(source);
        }

        destination.Id = source.Id;
        destination.Date = source.Date;

        return destination;
    }

    public OrderData? Map(OrderRequest? source)
    {
        return Map(source, new OrderData());
    }

    public OrderData? Map(OrderRequest? source, OrderData? destination)
    {
        if (source == null)
        {
            return null;
        }

        if (destination == null)
        {
            return Map(source);
        }
        
        destination.Date = source.Date ?? DateTime.UtcNow;

        return destination;
    }
}

The services

public interface IOrderService : IApplicationService
{
    
}

[MapCrudEndpoints]
public class OrderService : IOrderService
{
    private readonly IMapperFactory _mapperFactory;
    private readonly ILogger<ExampleService> _logger;
    private readonly IAsyncQueryableRepository<IQueryableUnitOfWork, OrderData, int> _repository;
    
    public OrderService(
        IApplicationContext<int> applicationContext,
        IQueryableUnitOfWork unitOfWork, 
        IMapperFactory mapperFactory, 
        ILogger<OrderService> logger)
    {
        _mapperFactory = mapperFactory;
        _logger = logger;
        _repository = unitOfWork.GetAsyncQueryableRepository<IQueryableUnitOfWork, OrderData, int>();
    }
    
    public async Task<Order?> GetByIdAsync(int orderId, CancellationToken cancellationToken = default)
    {
        var item = await _repository.GetAsync(orderId, cancellationToken: cancellationToken);

        if (item == null)
        {
            var ex = new EntityNotFoundException<int>(orderId);
            _logger.LogError(ex, "{ServiceName} - GetById: Entity of {EntityName} not found", GetType().Name,
                nameof(OrderData));
            throw ex;
        }

        var mapper = _mapperFactory.GetMapper<OrderData, Order>()!;
        var result = mapper.Map(item);
        
        return result;
    }
}

The Program.cs

var builder = WebApplication.CreateBuilder(args);
var assembly = typeof(Program).Assembly;

builder.Services.AddDbContext<ExampleDbContext>(opt =>
    opt.UseInMemoryDatabase("ExampleDb"));
        
builder.Services.AddQueryableRepositories<ExampleUnitOfWork>(opt =>
{
    opt.FromAssembly(assembly)
        .AddLifetime(ServiceLifetime.Scoped);
});

builder.Services
    .AddMappers(opt => opt.FromAssembly(assembly))
    .AddTransient<IExampleService, ExampleService>()
    .AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
        options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    })
    .AddFilterModelBinder()
    .AddSortModelBinder();

builder.Services
    .AddEndpointsApiExplorer()
    .AddApiDocumentation(opt => opt.WithTitle("Example API"));

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseApiDocumentation();
}

app.UseHttpsRedirection();
app.UseRouting();
app.MapControllers();
app.MapGet("orders/{id}", GetById);

app.Run();
return;

async Task<Results<Ok<Order>, NotFound>> GetById(
    [FromRoute] int id, 
    [FromServices] IOrderService service, 
    CancellationToken cancellationToken)
{
    var item = await service.GetByIdAsync(id, cancellationToken);
    if (item == null)
        return TypedResults.NotFound();

    return TypedResults.Ok(item);
}

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 is compatible.  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.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on eQuantic.Core.Api.Client:

Package Downloads
eQuantic.Core.Api.Crud.Client

eQuantic Api CRUD Client Library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.7.10 110 11/9/2024
1.7.9 102 10/11/2024
1.7.8 262 10/4/2024
1.7.7 90 10/4/2024
1.7.6 97 10/3/2024
1.7.5 126 9/4/2024
1.7.4 127 9/1/2024
1.7.3 132 8/25/2024
1.7.2 385 7/29/2024
1.7.1 80 7/28/2024
1.7.0 64 7/28/2024
1.6.25 73 7/27/2024
1.6.24 105 7/22/2024
1.6.23 353 6/23/2024
1.6.22 101 6/23/2024
1.6.21 106 6/22/2024
1.6.20 107 6/21/2024
1.6.19 231 5/18/2024
1.6.18 268 5/4/2024
1.6.17 224 4/11/2024
1.6.16 111 4/11/2024
1.6.15 130 4/9/2024
1.6.14 132 4/4/2024
1.6.13 380 4/2/2024
1.6.12 109 3/21/2024
1.6.11 118 3/21/2024
1.6.10 220 3/18/2024
1.6.9 140 3/10/2024
1.6.8 154 2/18/2024
1.6.7 129 2/17/2024
1.6.6 143 2/14/2024
1.6.5 123 2/13/2024
1.6.4 271 2/12/2024
1.6.3 146 2/10/2024

Conventions for API Client Layer