Bizer.Services 0.7.0

Suggested Alternatives

Bizer.Extensions.ApplicationService.EntityFrameworkCore

Additional Details

The package is changed to extensions design

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

// Install Bizer.Services as a Cake Tool
#tool nuget:?package=Bizer.Services&version=0.7.0                

Bizer.Services

基于 EntityFrameworkCore 和 Mapster 快速构建 CURD 的业务逻辑服务

有以下基类: ServiceBase:空基类,只有基本的几个对象,例如日志 ILogger ServiceBase<TContext>:控制指定 TContext 来操作 EF ServiceBase<TContext, TEntity>:直接用 EF 操作指定实体 CrudServiceBase:提供 CRUD 的操作

示例:


public class TestDbContext : DbContext
{
    public TestDbContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<User> Users { get; set; }
}
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

[InjectService]
public interface IUserService:ICrudService<int,User>
{
}

//使用了 AddDbContext<TestDbContext> 时,需要指定 TContext 参数
public class UserService : CrudServiceBase<TestDbContext, int, User>, IUserService
{
    public UserService(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
}

//配置了 AddDbContext() 时,可以不指定 TContext
public class UserService : CrudServiceBase<int, User>, IUserService
{
    public UserService(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
}

在启动程序中:

services.AddBizer(options => options.Assemblies.Add(typeof(IUserService).Assembly))
    .AddMapper()
    .AddServiceInjection()
    .AddDbContext(options.UseSqlServer("xx"));//使用 BizerDbContext
    // 或者
    .AddDbContext<MyDbContext>(options.UseSqlServer("xx"));//使用自定义 DbContext

像平时那样使用你的接口即可

private readonly IUserService _userService;
public DemoClass(IUserService userService)
{
        _userService = userService;
}
var result = await _userService.CreateAsync(new() { Id = 1, Name = "admin" });

注意事项

在添加服务时,只添加 AddDbContext() 方法,默认使用 BizerDbContextDbContext 类型:

builder.AddBizer().AddDbContext();
  • 迁移时,需要使用命令指定 BizerDbContext:
PM> Add-Migration xxxxx
PM> Update-Database -Context BizerDbContext
  • 编写业务逻辑时,继承的 Crud 基类可以使用不带 TContext 泛型参数的
public class MyClass : CrudServiceBase<Guid, MyEntity>
{
    //...
}

如果使用了自定义 DbContext

builder.AddBizer().AddDbContext<MyDbContext>();
  • 建议派生自 BizerDbContext 基类,以确保可以继续使用自动发现迁移对象的功能
public class MyDbContext : BizerDbContext
{
    public MyDbContext(DbContextOptions options, IServiceProvider serviceProvider) : base(options, serviceProvider)
    {

    }
}
  • 编写业务逻辑时,继承的 Crud 基类需要显示地带上你自定义的 DbContext 泛型参数
public class MyClass : CrudServiceBase<MyDbContext, Guid, MyEntity>
{
    //...
}

扩展功能

  • 使用 Mapster 作为对象映射功能
builder.AddBizer().AddMapper();
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 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.

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.7.0 187 1/9/2024 0.7.0 is deprecated because it is no longer maintained.
0.6.1 162 12/26/2023
0.6.0 183 9/4/2023
0.5.0.1 164 8/29/2023
0.5.0 174 8/4/2023
0.4.0 169 7/28/2023
0.3.0 180 7/20/2023