Skip to content

feat(eventBus): EventBus extension supports UseMiddleware and Optimized use of UseSoftDelete method #16

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

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ Install-Package Masa.Contrib.Data.Contracts.EF

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

```
Expand Down
6 changes: 1 addition & 5 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ Install-Package Masa.Contrib.Data.Contracts.EF

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Caching.DistributedMemory" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Caching.Redis" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Caller.Core" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Caching.DistributedMemory" Version="0.3.2" />
<PackageReference Include="Masa.Utils.Caching.Redis" Version="0.3.2" />
<PackageReference Include="Masa.Utils.Caller.Core" Version="0.3.2" />
<PackageReference Include="Masa.Utils.Caller.HttpClient" Version="0.3.2" />
<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.1" />
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.3.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
</ItemGroup>
Expand Down
6 changes: 1 addition & 5 deletions src/Data/Masa.Contrib.Data.Contracts.EF/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ Install-Package Masa.Contrib.Data.Contracts.EF

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

Expand Down
6 changes: 1 addition & 5 deletions src/Data/Masa.Contrib.Data.Contracts.EF/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ Install-Package Masa.Contrib.Data.Contracts.EF

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ namespace Masa.Contrib.Data.Contracts.EF;

public static class ServiceCollectionExtensions
{
[Obsolete("Please use optionsBuilder.UseSoftDelete()")]
public static MasaDbContextOptionsBuilder UseSoftDelete(
this MasaDbContextOptionsBuilder optionsBuilder, IServiceCollection services)
this MasaDbContextOptionsBuilder masaDbContextOptionsBuilder,
IServiceCollection services) => masaDbContextOptionsBuilder.UseSoftDelete();

public static MasaDbContextOptionsBuilder UseSoftDelete(
this MasaDbContextOptionsBuilder masaDbContextOptionsBuilder)
{
if (services.Any(s => s.ImplementationType == typeof(ContractsFilter))) return optionsBuilder;
services.AddSingleton<ContractsFilter>();
if (masaDbContextOptionsBuilder.Services.Any(s => s.ImplementationType == typeof(ContractsFilter))) return masaDbContextOptionsBuilder;
masaDbContextOptionsBuilder.Services.AddSingleton<ContractsFilter>();

if (services.All(service => service.ServiceType != typeof(IUnitOfWork)))
if (masaDbContextOptionsBuilder.Services.All(service => service.ServiceType != typeof(IUnitOfWork)))
throw new Exception("Please add UoW first.");

optionsBuilder.UseQueryFilterProvider<QueryFilterProvider>()
masaDbContextOptionsBuilder.UseQueryFilterProvider<QueryFilterProvider>()
.UseSaveChangesFilter<SoftDeleteSaveChangesFilter>();

return optionsBuilder;
return masaDbContextOptionsBuilder;
}

private class ContractsFilter
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.1" />
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.3.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
</ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions src/Data/Masa.Contrib.Data.UoW.EF/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ Install-Package Masa.Contrib.Data.Contracts.EF

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

5 changes: 1 addition & 4 deletions src/Data/Masa.Contrib.Data.UoW.EF/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ Install-Package Masa.Contrib.Data.Contracts.EF

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

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ public static IDispatcherOptions UseEventBus(

public static IDispatcherOptions UseEventBus(
this IDispatcherOptions options,
Action<EventBusBuilder> eventBusBuilder)
=> options.UseEventBus(eventBusBuilder, ServiceLifetime.Scoped);

public static IDispatcherOptions UseEventBus(
this IDispatcherOptions options,
ServiceLifetime lifetime)
=> options.UseEventBus(null, lifetime);

public static IDispatcherOptions UseEventBus(
this IDispatcherOptions options,
Action<EventBusBuilder>? eventBusBuilder,
ServiceLifetime lifetime)
{
ArgumentNullException.ThrowIfNull(options.Services,nameof(options.Services));
ArgumentNullException.ThrowIfNull(options.Services, nameof(options.Services));

eventBusBuilder?.Invoke(new EventBusBuilder(options.Services));
options.Services.AddEventBus(options.Assemblies, lifetime);
return options;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Masa.Contrib.Dispatcher.Events;

public class EventBusBuilder
{
public IServiceCollection Services { get; }

public EventBusBuilder(IServiceCollection services) => Services = services;
}
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.1" />
<PackageReference Include="Masa.Utils.Models.Config" Version="0.3.2" />
<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
@@ -0,0 +1,17 @@
namespace Masa.Contrib.Dispatcher.Events;

public static class UseMiddlewareExtensions
{
public static EventBusBuilder UseMiddleware(
this EventBusBuilder eventBusBuilder,
Type middleware,
ServiceLifetime middlewareLifetime = ServiceLifetime.Scoped)
{
if (!typeof(IMiddleware<>).IsGenericInterfaceAssignableFrom(middleware))
throw new ArgumentException($"{middleware.Name} doesn't implement IMiddleware<>");

var descriptor = new ServiceDescriptor(typeof(IMiddleware<>), middleware, middlewareLifetime);
eventBusBuilder.Services.Add(descriptor);
return eventBusBuilder;
}
}
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.1" />
<PackageReference Include="Masa.Utils.Exceptions" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Models.Config" Version="0.3.2" />
<PackageReference Include="Masa.Utils.Exceptions" Version="0.3.2" />
<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 @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Exceptions" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Data.EntityFrameworkCore" Version="0.3.2" />
<PackageReference Include="Masa.Utils.Exceptions" Version="0.3.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Utils.Caching.Core" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Extensions.DependencyInjection" Version="0.3.1" />
<PackageReference Include="Masa.Utils.Caching.Core" Version="0.3.2" />
<PackageReference Include="Masa.Utils.Extensions.DependencyInjection" Version="0.3.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>

Expand Down
7 changes: 3 additions & 4 deletions test/Masa.Contrib.Data.Contracts.EF.Tests/SoftDeleteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void UseNotUseUoW()
services.AddMasaDbContext<CustomDbContext>(option =>
{
option.UseSqlite(_connection);
Assert.ThrowsException<Exception>(() => option.UseSoftDelete(services), "Please add UoW first.");
Assert.ThrowsException<Exception>(option.UseSoftDelete, "Please add UoW first.");
});
}

Expand All @@ -38,8 +38,7 @@ public void TestUseSoftDelete()
services.AddScoped(serviceProvider => uoW.Object);
services.AddMasaDbContext<CustomDbContext>(option =>
{
option.UseSqlite(_connection);
option.UseSoftDelete(services);
option.UseSoftDelete().UseSqlite(_connection);
});

var serviceProvider = services.BuildServiceProvider();
Expand Down Expand Up @@ -73,7 +72,7 @@ public void TestUseMultiSoftDelete()
services.AddMasaDbContext<CustomDbContext>(option =>
{
option.UseSqlite(_connection);
option.UseSoftDelete(services).UseSoftDelete(services);
option.UseSoftDelete().UseSoftDelete();
});
}
}