-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDbContextDataProvider.cs
More file actions
102 lines (87 loc) · 3.45 KB
/
DbContextDataProvider.cs
File metadata and controls
102 lines (87 loc) · 3.45 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
// Licensed under the MIT license.
// Copyright (c) The AppCore .NET project.
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Threading;
using System.Threading.Tasks;
using AppCoreNet.Diagnostics;
namespace AppCoreNet.Data.EntityFramework;
/// <summary>
/// Represents a Entity Framework data provider.
/// </summary>
/// <typeparam name="TDbContext">The type of the <see cref="DbContext"/>.</typeparam>
public sealed class DbContextDataProvider<TDbContext> : IDataProvider
where TDbContext : DbContext
{
private readonly string _name;
private readonly DbContextDataProviderServices<TDbContext> _services;
/// <inheritdoc />
public string Name => _name;
/// <summary>
/// Gets the <see cref="DbContext"/> of the data provider.
/// </summary>
/// <value>The <see cref="DbContext"/>.</value>
public TDbContext DbContext => _services.DbContext;
/// <summary>
/// Gets the <see cref="IEntityMapper"/> of the data provider.
/// </summary>
public IEntityMapper EntityMapper => _services.EntityMapper;
/// <summary>
/// Gets the <see cref="ITokenGenerator"/> of the data provider.
/// </summary>
public ITokenGenerator TokenGenerator => _services.TokenGenerator;
/// <summary>
/// Gets the <see cref="DbContextQueryHandlerFactory{TDbContext}"/> of the data provider.
/// </summary>
public DbContextQueryHandlerFactory<TDbContext> QueryHandlerFactory => _services.QueryHandlerFactory;
/// <summary>
/// Gets the <see cref="DbContextTransactionManager{TDbContext}"/>.
/// </summary>
public DbContextTransactionManager<TDbContext> TransactionManager => _services.TransactionManager;
ITransactionManager IDataProvider.TransactionManager => TransactionManager;
internal DataProviderLogger<DbContextDataProvider<TDbContext>> Logger => _services.Logger;
/// <summary>
/// Initializes a new instance of the <see cref="DbContextDataProvider{TDbContext}"/> class.
/// </summary>
/// <param name="name">The name of the data provider.</param>
/// <param name="services">The <see cref="DbContextDataProviderServices{TDbContext}"/>.</param>
public DbContextDataProvider(
string name,
DbContextDataProviderServices<TDbContext> services)
{
Ensure.Arg.NotNull(name);
Ensure.Arg.NotNull(services);
_name = name;
_services = services;
}
/// <summary>
/// Saves changes made to the <see cref="DbContext"/>.
/// </summary>
/// <param name="cancellationToken">Optional <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
try
{
// EF6 SaveChangesAsync returns Task<int>
await DbContext.SaveChangesAsync(cancellationToken)
.ConfigureAwait(false);
}
catch (DbUpdateConcurrencyException error)
{
throw new EntityConcurrencyException(error);
}
catch (DbUpdateException error)
{
throw new EntityUpdateException(error);
}
// detach all entities after saving changes
foreach (DbEntityEntry entry in DbContext.ChangeTracker.Entries())
{
if (entry.Entity != null)
{
entry.State = EntityState.Detached;
}
}
}
}