Bizer.Services
0.6.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Bizer.Services --version 0.6.0
NuGet\Install-Package Bizer.Services -Version 0.6.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.6.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bizer.Services --version 0.6.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Bizer.Services, 0.6.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.6.0 // Install Bizer.Services as a Cake Tool #tool nuget:?package=Bizer.Services&version=0.6.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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()
方法,默认使用 BizerDbContext
的 DbContext
类型:
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 | Versions 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.
-
net6.0
- Bizer (>= 0.6.0)
- LazyCache.AspNetCore (>= 2.4.0)
- Mapster.DependencyInjection (>= 1.0.0)
- Mapster.EFCore (>= 5.1.0)
- Microsoft.EntityFrameworkCore (>= 6.0.0)
-
net7.0
- Bizer (>= 0.6.0)
- LazyCache.AspNetCore (>= 2.4.0)
- Mapster.DependencyInjection (>= 1.0.0)
- Mapster.EFCore (>= 5.1.0)
- Microsoft.EntityFrameworkCore (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.