-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntityFrameworkDataProviderBuilderExtensions.cs
More file actions
109 lines (96 loc) · 4.51 KB
/
EntityFrameworkDataProviderBuilderExtensions.cs
File metadata and controls
109 lines (96 loc) · 4.51 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Licensed under the MIT license.
// Copyright (c) The AppCore .NET project.
using System;
using System.Data.Entity;
using AppCoreNet.Data;
using AppCoreNet.Data.EntityFramework;
using AppCoreNet.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
// ReSharper disable once CheckNamespace
namespace AppCoreNet.Extensions.DependencyInjection;
/// <summary>
/// Provides extension methods to register a <see cref="DbContextDataProvider{TDbContext}"/>.
/// </summary>
public static class EntityFrameworkDataProviderBuilderExtensions
{
/// <summary>
/// Registers a <see cref="DbContext"/> data provider in the <see cref="IServiceCollection"/>.
/// </summary>
/// <typeparam name="TDbContext">The type of the <see cref="DbContext"/>.</typeparam>
/// <param name="builder">The <see cref="IDataProviderBuilder"/>.</param>
/// <param name="name">The name of the data provider.</param>
/// <param name="providerLifetime">The lifetime of the data provider.</param>
/// <returns>The <see cref="EntityFrameworkDataProviderBuilder{TDbContext}"/>.</returns>
public static EntityFrameworkDataProviderBuilder<TDbContext> AddEntityFramework<TDbContext>(
this IDataProviderBuilder builder,
string name,
ServiceLifetime providerLifetime = ServiceLifetime.Scoped)
where TDbContext : DbContext
{
Ensure.Arg.NotNull(builder);
Ensure.Arg.NotNull(name);
IServiceCollection services = builder.Services;
services.AddOptions();
services.AddLogging();
services.TryAdd(
ServiceDescriptor.Describe(
typeof(TDbContext),
typeof(TDbContext),
providerLifetime));
services.TryAdd(
ServiceDescriptor.Describe(
typeof(DbContextTransactionManager<TDbContext>),
typeof(DbContextTransactionManager<TDbContext>),
providerLifetime));
services.TryAdd(
ServiceDescriptor.DescribeKeyed(
typeof(DbContextQueryHandlerFactory<TDbContext>),
name,
static (sp, name) =>
{
DbContextDataProviderOptions options = GetOptions(sp, (string)name!);
return new DbContextQueryHandlerFactory<TDbContext>(sp, options.QueryHandlerTypes);
},
providerLifetime));
builder.AddProvider<DbContextDataProvider<TDbContext>>(
name,
providerLifetime,
static (sp, name) =>
{
DbContextDataProviderOptions options = GetOptions(sp, name);
return new DbContextDataProvider<TDbContext>(
name,
sp.GetRequiredService<TDbContext>(),
options.EntityMapperFactory(sp),
options.TokenGeneratorFactory(sp),
sp.GetRequiredKeyedService<DbContextQueryHandlerFactory<TDbContext>>(name),
sp.GetRequiredService<DbContextTransactionManager<TDbContext>>(),
sp.GetRequiredService<DataProviderLogger<DbContextDataProvider<TDbContext>>>());
});
return new EntityFrameworkDataProviderBuilder<TDbContext>(name, services, providerLifetime);
static DbContextDataProviderOptions GetOptions(IServiceProvider sp, string name)
{
var optionsMonitor = sp.GetRequiredService<IOptionsMonitor<DbContextDataProviderOptions>>();
return optionsMonitor.Get(name);
}
}
/// <summary>
/// Registers a <see cref="DbContext"/> default data provider in the <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Note that you have to register the <see cref="DbContext"/> on your own.
/// </remarks>
/// <typeparam name="TDbContext">The type of the <see cref="DbContext"/>.</typeparam>
/// <param name="builder">The <see cref="IDataProviderBuilder"/>.</param>
/// <param name="providerLifetime">The lifetime of the data provider.</param>
/// <returns>The <see cref="EntityFrameworkDataProviderBuilder{TDbContext}"/>.</returns>
public static EntityFrameworkDataProviderBuilder<TDbContext> AddEntityFramework<TDbContext>(
this IDataProviderBuilder builder,
ServiceLifetime providerLifetime = ServiceLifetime.Scoped)
where TDbContext : DbContext
{
return builder.AddEntityFramework<TDbContext>(string.Empty, providerLifetime);
}
}