DataExplorer.EfCore.Unstable 1.0.1

dotnet add package DataExplorer.EfCore.Unstable --version 1.0.1                
NuGet\Install-Package DataExplorer.EfCore.Unstable -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="DataExplorer.EfCore.Unstable" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DataExplorer.EfCore.Unstable --version 1.0.1                
#r "nuget: DataExplorer.EfCore.Unstable, 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 DataExplorer.EfCore.Unstable as a Cake Addin
#addin nuget:?package=DataExplorer.EfCore.Unstable&version=1.0.1

// Install DataExplorer.EfCore.Unstable as a Cake Tool
#tool nuget:?package=DataExplorer.EfCore.Unstable&version=1.0.1                

DataExplorer.EfCore

Build Status

Library featuring a combination of Unit of Work, Repository, Specification and Data Service patterns.

Installation

Base DataExplorer package is required.

To register the library services with the DI container use the DataExplorerConfiguration extension method provided by the library:

builder.AddDataExplorer(options => 
{
    options.AddEfCore(assembliesToScan);
});

Description

Library provides definition of a base entity (with generic Id and with long Id) which should be inherited by any entity defined within application's domain.

public class CustomEntity : Entity<long>
{
    public bool CustomField { get; set; }
}

Library supports soft deletion via a IsDisabled property on entities, you can mark an entity as a soft deleted entity using IDisableableEntity interface. Snowflake IDs are supported using an IdGenerator, to mark an entity to use snowflake IDs inherit from SnowflakeEntity or implement ISnowflakeEntity interface and override the Id property. Ids can be generated by a provided IdGenerator which you can add as a singleton service via AddIdGen()

To avoid LINQ boilerplate a specification pattern is used to encapsulate query logic and should be used like so:

public CustomSpecification : ISpecification<CustomEntity>
{
    public CustomSpecification(bool customField)
    {
        Where(x => x.CustomField == customField);
    }
}

Please use IntelliSense to check for available methods but pretty much everything should be supported by now.

Every data service automatically grabs it's corresponding entity repository from the Unit of Work and exposes it via properties, you can however do anything and everything through exposed DbContext, UnitOfWork, Mapper and other properties.

Getting a repository is done through methods on IUnitOfWork:

unitOfWork.GetRepository<IRepository<CustomEntity>>();

Please use IntelliSense to find other helpful get repo methods. You can't (without reflection magic) create custom repository implementations, their constructor is internal, you can only get generic repos with the methods exposed via IUnitOfWork, any custom, additional logic, should be performed in the service layer.:

Given below context definitions (always abstract your own context) - either manually register your context interface as a service or use AddDbContext methods on DataExplorerEfCoreConfiguration.

public interface ICustomDbContext : IEfDbContext
{
}

public class CustomDbContext : EfDbContext, ICustomDbContext
{
}

You can define a crud data service like so to add some custom logic:

public interface ICustomDataService : ICrudDataService<CustomEntity, ICustomDbContext>
{
    Task<Result> GetFirstEntityWithCustomFieldTrueAndPerformLogicAsync();
}

public class CustomDataService : CrudDataService<CustomEntity, ICustomDbContext>, ICustomDataService
{
    public async Task<Result> GetFirstEntityWithCustomFieldTrueAndPerformLogicAsync()
    {
        var entityRes = await GetSingleBySpecAsync(new CustomSpecification(true));

        if (!entityRes.IsDefined(out var entity))
            return entityRes;

        /// perform custom logic on entity or whatnot

        return Result.FromSuccess();
    }
}

You can also simply inject a generic data service to perform simple operations like so:


public class CustomController : ControllerBase
{
    private readonly IReadOnlyDataService<CustomEntity,ICustomDbContext> _dataService;

    public CustomController(IReadOnlyDataService<CustomEntity,ICustomDbContext> dataService)
        => _dataService = dataService;

    [HttpGet]
    public async Task<IActionResult> GetAllByCustomFieldAsync(bool customField)
    {
        var subRes = await _dataService.GetBySpecAsync(new CustomSpecification(customField)); // or GetBySpecAsync<SomeDto> to automatically map the entity using AutoMapper
        if (!subRes.IsDefined(out var result))
            return BadRequest();

        return Ok(result);
    }
}

All service methods should return a Result struct which determines whether the operation succeeded or not and returns additional objects if necessary and shouldn't throw exceptions unless absolutely necessary.

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 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.0.1 306 1/9/2023
1.0.0 317 1/9/2023