GenericEntity 0.1.0

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

// Install GenericEntity as a Cake Tool
#tool nuget:?package=GenericEntity&version=0.1.0                

Generic Entity

A library to manage EF Core entity easier without adding DbSet<T> of every single entity into database context

NuGet

Features

  • No need to write each entity field on DbContext
  • Support multiple database
  • Cleaner query invoke

Setup

First, create interface that represent entity of database, then implement IEntity<br>

public interface ISampleEntity : IEntity { }

Second, create interface that represent db context that implement IAppDbContext<TEntity>, where T is interface of entity that created before

public interface ISampleDbContext : IAppDbContext<ISampleEntity> { }

Third, create entity class that impelement interface entity above

public class Blog : ISampleEntity
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

Forth, create db context class that inherit from AppDbContext<TContext> and implement interface db context that created before, where TContext is db context itself

public class SampleDbContext : AppDbContext<SampleDbContext>, ISampleDbContext
{
    public SampleDbContext() : base() { }
    public SampleDbContext(DbContextOptions<SampleDbContext> options) : base(options) { }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyEntityRegistration();
    }

    DbSet<UEntity> IAppDbContext<ISampleEntity>.Entity<UEntity>()
    {
        return Set<UEntity>();
    }
}

Note: You need to override OnModelCreating and Entity method like above to make this library work

ApplyEntityRegistration() will make sure each class that implement interface entity (first step) is migrated into database, and Set<T> is to return field entity on db context.

Lastly (optional), register interface db context and db context into service container

services.AddScoped<ISampleDbContext, SampleDbContext>();

Usage

Instead of inject db context instance (when using DI), you can inject interface db context (second step) into constructor

public class BlogController : Controller
{
    private readonly ISampleDbContext _dbContext;

    public BlogController(ISampleDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    ...
}

And then invoke method Entity<TEntity> when query to some entity

public class BlogController : Controller
{
    ...

    [HttpGet("{id}")]
    public async Task<IActionResult> ReadDetail(string id, CancellationToken ct)
    {
        var blog = await _dbContext.Entity<Blog>()
            .Where(x => x.Id == id)
            .FirstOrDefaultAsync(ct);

        return new OkObjectResult(blog);
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
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
0.2.0 497 1/25/2022
0.1.0 328 8/22/2021