Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(EntityFramework): support Masa.Utils 0.4.0-preview.1 #21

Merged
merged 9 commits into from
Mar 24, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Caching.DistributedMemory" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Caching.Redis" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Caching.DistributedMemory" Version="0.4.0-preview.1" />
<PackageReference Include="Masa.Utils.Caching.Redis" Version="0.4.0-preview.1" />
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="0.4.0-preview.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.4.0-preview.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
</ItemGroup>
Expand Down
26 changes: 1 addition & 25 deletions src/Data/Masa.Contrib.Data.Contracts.EF/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
[中](README.zh-CN.md) | EN

## Contracts.EF

Example:

```C#
Install-Package Masa.Contrib.Data.UoW.EF
Install-Package Masa.Contrib.Data.Contracts.EF
```

```C#
builder.Services.AddEventBus(options => {
options.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSoftDelete().UseSqlServer("server=localhost;uid=sa;pwd=P@ssw0rd;database=identity"));
});
```

> When the entity inherits ISoftware and is deleted, change the delete state to the modified state, and cooperate with the custom Remove operation to achieve soft deletion
> Do not query the data marked as soft deleted when querying

> Frequently Asked Questions:

- Problem 1: After using UseSoftDelete, there is a problem that the submission cannot be saved

After using Uow, the transaction will be enabled by default after Add、 Modified、 and Deleted
and the transaction can be saved normally after the transaction is submitted
If the EventBus is used, the transaction will be automatically submitted
## Contracts.EF
26 changes: 1 addition & 25 deletions src/Data/Masa.Contrib.Data.Contracts.EF/README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
中 | [EN](README.md)

## Contracts.EF

用例:

```C#
Install-Package Masa.Contrib.Data.UoW.EF
Install-Package Masa.Contrib.Data.Contracts.EF
```

```C#
builder.Services.AddEventBus(options => {
options.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSoftDelete().UseSqlServer("server=localhost;uid=sa;pwd=P@ssw0rd;database=identity"));
});
```

> 当实体继承ISoftware,且被删除时,将删除状态改为修改状态,并配合自定义Remove操作,实现软删除
> 支持查询的时候不查询被标记软删除的数据

> 常见问题:

- 问题1:使用UseSoftDelete后出现提交保存不上的问题

使用Uow后,默认在进行Add、Modified、Deleted后会启用事务
需要提交事务之后才能正常保存
如果使用EventBus则会自动提交事务
## Contracts.EF

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions src/Data/Masa.Contrib.Data.Contracts.EF/_Imports.cs
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
global using Masa.BuildingBlocks.Data.Contracts;
global using Masa.BuildingBlocks.Data.UoW;
global using Masa.Contrib.Data.Contracts.EF.SoftDelete;
global using Masa.Utils.Data.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore.ChangeTracking;
global using Microsoft.EntityFrameworkCore.Metadata;
global using Microsoft.Extensions.DependencyInjection;
global using System.Linq.Expressions;
11 changes: 3 additions & 8 deletions src/Data/Masa.Contrib.Data.UoW.EF/DispatcherOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ public static IDispatcherOptions UseUoW<TDbContext>(

options.Services.AddSingleton<UoWProvider>();

options.Services.AddScoped<IUnitOfWork>(serviceProvider =>
options.Services.AddScoped<IUnitOfWork>(serviceProvider => new UnitOfWork<TDbContext>(serviceProvider)
{
var dbContext = serviceProvider.GetRequiredService<TDbContext>();
var logger = serviceProvider.GetService<ILogger<UnitOfWork<TDbContext>>>();
return new UnitOfWork<TDbContext>(dbContext, logger)
{
DisableRollbackOnFailure = disableRollbackOnFailure,
UseTransaction = useTransaction
};
DisableRollbackOnFailure = disableRollbackOnFailure,
UseTransaction = useTransaction
});
if (options.Services.All(service => service.ServiceType != typeof(MasaDbContextOptions<TDbContext>)))
options.Services.AddMasaDbContext<TDbContext>(optionsBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.4.0-preview.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Masa.Contrib.Data.UoW.EF/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Example:
```C#
Install-Package Masa.Contrib.Data.UoW.EF
Install-Package Masa.Contrib.Data.Contracts.EF
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether references to Contracts can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it can be deleted, I missed it here, processed

Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

```C#
builder.Services.AddEventBus(options => {
options.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSqlServer("server=localhost;uid=sa;pwd=P@ssw0rd;database=identity"));
});
```

```
4 changes: 2 additions & 2 deletions src/Data/Masa.Contrib.Data.UoW.EF/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
```C#
Install-Package Masa.Contrib.Data.UoW.EF
Install-Package Masa.Contrib.Data.Contracts.EF
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

```C#
builder.Services.AddEventBus(options => {
options.UseUoW<CustomDbContext>(dbOptions => dbOptions.UseSqlServer("server=localhost;uid=sa;pwd=P@ssw0rd;database=identity"));
});
```

```
31 changes: 16 additions & 15 deletions src/Data/Masa.Contrib.Data.UoW.EF/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ namespace Masa.Contrib.Data.UoW.EF;
public class UnitOfWork<TDbContext> : IUnitOfWork
where TDbContext : MasaDbContext
{
private readonly IServiceProvider _serviceProvider;

private DbContext? _context;

protected DbContext Context => _context ??= _serviceProvider.GetRequiredService<TDbContext>();

public DbTransaction Transaction
{
get
Expand All @@ -11,13 +17,13 @@ public DbTransaction Transaction
throw new NotSupportedException("Doesn't support transaction opening");

if (TransactionHasBegun)
return _context.Database.CurrentTransaction!.GetDbTransaction();
return Context.Database.CurrentTransaction!.GetDbTransaction();

return _context.Database.BeginTransaction().GetDbTransaction();
return Context.Database.BeginTransaction().GetDbTransaction();
}
}

public bool TransactionHasBegun => _context.Database.CurrentTransaction != null;
public bool TransactionHasBegun => Context.Database.CurrentTransaction != null;

public bool DisableRollbackOnFailure { get; set; }

Expand All @@ -27,19 +33,14 @@ public DbTransaction Transaction

public bool UseTransaction { get; set; } = true;

private readonly DbContext _context;

private readonly ILogger<UnitOfWork<TDbContext>>? _logger;

public UnitOfWork(TDbContext dbContext, ILogger<UnitOfWork<TDbContext>>? logger = null)
public UnitOfWork(IServiceProvider serviceProvider)
{
_context = dbContext;
_logger = logger;
_serviceProvider = serviceProvider;
}

public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
await _context.SaveChangesAsync(cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
EntityState = EntityState.UnChanged;
}

Expand All @@ -48,7 +49,7 @@ public async Task CommitAsync(CancellationToken cancellationToken = default)
if (!UseTransaction || !TransactionHasBegun)
throw new NotSupportedException("Transaction not opened");

await _context.Database.CommitTransactionAsync(cancellationToken);
await Context.Database.CommitTransactionAsync(cancellationToken);
CommitState = CommitState.Commited;
}

Expand All @@ -57,10 +58,10 @@ public async Task RollbackAsync(CancellationToken cancellationToken = default)
if (!UseTransaction || !TransactionHasBegun)
throw new NotSupportedException("Transactions are not opened and rollback is not supported");

await _context.Database.RollbackTransactionAsync(cancellationToken);
await Context.Database.RollbackTransactionAsync(cancellationToken);
}

public ValueTask DisposeAsync() => _context.DisposeAsync();
public ValueTask DisposeAsync() => Context.DisposeAsync();

public void Dispose() => _context.Dispose();
public void Dispose() => Context.Dispose();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ namespace Masa.Contrib.Ddd.Domain.Repository.EF;
public static class DispatcherOptionsExtensions
{
public static IDispatcherOptions UseRepository<TDbContext>(
this IDispatcherOptions options)
where TDbContext : DbContext
=> options.UseRepository<TDbContext>(AppDomain.CurrentDomain.GetAssemblies());

public static IDispatcherOptions UseRepository<TDbContext>(
this IDispatcherOptions options,
params Assembly[] assemblies)
this IDispatcherOptions options)
where TDbContext : DbContext
{
if (options.Services == null)
Expand All @@ -23,7 +17,7 @@ public static IDispatcherOptions UseRepository<TDbContext>(
if (options.Services.All(service => service.ServiceType != typeof(IUnitOfWork)))
throw new Exception("Please add UoW first.");

options.Services.TryAddRepository<TDbContext>(assemblies);
options.Services.TryAddRepository<TDbContext>(options.Assemblies);
return options;
}

Expand Down
1 change: 1 addition & 0 deletions src/Ddd/Masa.Contrib.Ddd.Domain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Install-Package Masa.Contrib.Dispatcher.Events
Install-Package Masa.Contrib.Dispatcher.IntegrationEvents.Dapr
Install-Package Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF
Install-Package Masa.Contrib.Data.UoW.EF
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. Add DomainEventBus
Expand Down
1 change: 1 addition & 0 deletions src/Ddd/Masa.Contrib.Ddd.Domain/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Install-Package Masa.Contrib.Dispatcher.Events
Install-Package Masa.Contrib.Dispatcher.IntegrationEvents.Dapr
Install-Package Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF
Install-Package Masa.Contrib.Data.UoW.EF
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer
```

1. 添加DomainEventBus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Models.Config" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Models.Config" Version="0.4.0-preview.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" Version="1.5.0" />
<PackageReference Include="Masa.Utils.Models.Config" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Exceptions" Version="0.3.5" />
<PackageReference Include="Masa.Utils.Models.Config" Version="0.4.0-preview.1" />
<PackageReference Include="Masa.Utils.Exceptions" Version="0.4.0-preview.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Example:
Install-Package Masa.Contrib.Dispatcher.IntegrationEvents.Dapr //Send cross-process messages
Install-Package Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF //Record cross-process message logs
Install-Package Masa.Contrib.Data.UoW.EF //Use UnitOfWork
Install-Package Masa.Utils.Data.EntityFrameworkCore.SqlServer // Use SqlServer
```

1. Add IIntegrationEventBus
Expand All @@ -22,7 +23,7 @@ builder.Services
});
```

> CustomerDbContext needs to inherit IntegrationEventLogContext
> CustomerDbContext needs to inherit MasaDbContext

2. Custom IntegrationEvent

Expand All @@ -38,7 +39,7 @@ public class DemoIntegrationEvent : IntegrationEvent
3. Custom CustomDbContext

```C#
public class CustomDbContext : IntegrationEventLogContext
public class CustomDbContext : MasaDbContext
{
public DbSet<User> Users { get; set; } = null!;

Expand Down
Loading