-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathDispatcherOptionsExtensions.cs
88 lines (77 loc) · 3.57 KB
/
DispatcherOptionsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
namespace Masa.Contrib.Data.UoW.EFCore;
public static class DispatcherOptionsExtensions
{
public static IEventBusBuilder UseUoW<TDbContext>(
this IEventBusBuilder eventBusBuilder,
Action<MasaDbContextBuilder>? optionsBuilder = null,
bool disableRollbackOnFailure = false,
bool useTransaction = true)
where TDbContext : MasaDbContext, IMasaDbContext
=> eventBusBuilder.UseUoW<TDbContext, Guid>(optionsBuilder, disableRollbackOnFailure, useTransaction);
public static IEventBusBuilder UseUoW<TDbContext, TUserId>(
this IEventBusBuilder eventBusBuilder,
Action<MasaDbContextBuilder>? optionsBuilder = null,
bool disableRollbackOnFailure = false,
bool useTransaction = true)
where TDbContext : MasaDbContext, IMasaDbContext
where TUserId : IComparable
{
eventBusBuilder.Services.UseUoW<TDbContext, TUserId>(
nameof(eventBusBuilder.Services),
optionsBuilder,
disableRollbackOnFailure,
useTransaction);
return eventBusBuilder;
}
public static IDispatcherOptions UseUoW<TDbContext>(
this IDispatcherOptions options,
Action<MasaDbContextBuilder>? optionsBuilder = null,
bool disableRollbackOnFailure = false,
bool useTransaction = true)
where TDbContext : MasaDbContext, IMasaDbContext
=> options.UseUoW<TDbContext, Guid>(optionsBuilder, disableRollbackOnFailure, useTransaction);
public static IDispatcherOptions UseUoW<TDbContext, TUserId>(
this IDispatcherOptions options,
Action<MasaDbContextBuilder>? optionsBuilder = null,
bool disableRollbackOnFailure = false,
bool useTransaction = true)
where TDbContext : MasaDbContext, IMasaDbContext
where TUserId : IComparable
{
options.Services.UseUoW<TDbContext, TUserId>(nameof(options.Services), optionsBuilder, disableRollbackOnFailure, useTransaction);
return options;
}
private static IServiceCollection UseUoW<TDbContext, TUserId>(
this IServiceCollection services,
string paramName,
Action<MasaDbContextBuilder>? optionsBuilder = null,
bool disableRollbackOnFailure = false,
bool useTransaction = true)
where TDbContext : MasaDbContext, IMasaDbContext
where TUserId : IComparable
{
if (services == null)
throw new ArgumentNullException(paramName);
if (services.Any(service => service.ImplementationType == typeof(UoWProvider)))
return services;
services.AddSingleton<UoWProvider>();
services.TryAddScoped<IUnitOfWorkAccessor, UnitOfWorkAccessor>();
services.TryAddSingleton<IUnitOfWorkManager, UnitOfWorkManager<TDbContext>>();
services.TryAddScoped<IConnectionStringProvider, DefaultConnectionStringProvider>();
services.AddScoped<IUnitOfWork>(serviceProvider => new UnitOfWork<TDbContext>(serviceProvider)
{
DisableRollbackOnFailure = disableRollbackOnFailure,
UseTransaction = useTransaction
});
if (services.All(service => service.ServiceType != typeof(MasaDbContextOptions<TDbContext>)))
services.AddMasaDbContext<TDbContext, TUserId>(optionsBuilder);
services.AddScoped<ITransaction, Transaction>();
MasaApp.TrySetServiceCollection(services);
return services;
}
private sealed class UoWProvider
{
}
}