forked from Oceania2018/EntityFrameworkCore.BootKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
278 lines (230 loc) · 8.34 KB
/
Copy pathDatabase.cs
File metadata and controls
278 lines (230 loc) · 8.34 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using Dapper;
using EntityFrameworkCore.BootKit.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace EntityFrameworkCore.BootKit;
public class Database
{
public List<DatabaseBind> DbContextBinds;
public Database()
{
DbContextBinds = new List<DatabaseBind>();
}
public DatabaseBind GetBinding(Type tableInterface)
{
var binding = DbContextBinds.FirstOrDefault(x => (x.TableInterface != null && x.TableInterface.Equals(tableInterface)) ||
(x.Entities != null && x.Entities.Select(e => e.Name).Contains(tableInterface.Name)));
if (binding == null)
{
throw new Exception($"Can't find binding for interface {tableInterface.ToString()}");
}
return binding;
}
public DatabaseBind GetBinding(string tableName)
{
var binding = DbContextBinds.FirstOrDefault(x => x.Entities != null && x.Entities.Select(entity => entity.Name.ToLower()).Contains(tableName.ToLower()));
if (binding == null)
{
throw new Exception($"Can't find binding for table {tableName}");
}
return binding;
}
private List<Type> GetAllEntityTypes(DatabaseBind bind)
{
var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies");
return Utility.GetClassesWithInterface(bind.TableInterface, assemblies);
}
public void BindDbContext(DatabaseBind bind)
{
bind.Entities = GetAllEntityTypes(bind).ToList();
DbContextBinds.Add(bind);
}
public void BindDbContext<TTableInterface, TDbContextType>(DatabaseBind bind)
{
bind.TableInterface = typeof(TTableInterface);
bind.DbContextType = typeof(TDbContextType);
bind.Entities = GetAllEntityTypes(bind).ToList();
if (bind.SlaveConnections == null)
bind.SlaveConnections = new List<DbConnection>();
if (bind.SlaveConnections.Count == 0)
bind.SlaveConnections.Add(bind.MasterConnection);
// random
bind.SlaveId = new Random().Next(bind.SlaveConnections.Count);
DbContextBinds.Add(bind);
if (bind.CreateDbIfNotExist)
GetMaster(bind.TableInterface).Database.EnsureCreated();
}
public DataContext GetMaster(Type tableInterface)
{
var binding = GetBinding(tableInterface);
if (binding.DbContextMaster == null)
{
DbContextOptions options = new DbContextOptions<DataContext>();
DataContext dbContext = Activator.CreateInstance(binding.DbContextType, options, binding.ServiceProvider) as DataContext;
dbContext.ConnectionString = binding.MasterConnection.ConnectionString;
dbContext.EntityTypes = binding.Entities;
binding.DbContextMaster = dbContext;
}
return binding.DbContextMaster;
}
public DataContext GetReader(Type tableInterface)
{
var binding = GetBinding(tableInterface);
if (binding.DbContextSlaver == null)
{
DbContextOptions options = new DbContextOptions<DataContext>();
DataContext dbContext = Activator.CreateInstance(binding.DbContextType, options, binding.ServiceProvider) as DataContext;
dbContext.EntityTypes = binding.Entities;
if (binding.SlaveConnections == null || binding.SlaveConnections.Count == 0)
dbContext.ConnectionString = binding.MasterConnection.ConnectionString;
else
dbContext.ConnectionString = binding.SlaveConnection.ConnectionString;
binding.DbContextSlaver = dbContext;
}
return binding.DbContextSlaver;
}
private string RandomConn(List<DbConnection> connetions)
{
int idx = new Random().Next(connetions.Count);
return connetions[idx].ConnectionString;
}
/*public IMongoCollection<T> Collection<T>(string collection) where T : class
{
DatabaseBind binding = GetBinding(typeof(T));
if (binding.DbContextMaster == null)
{
binding.DbContext = new MongoDbContext(binding.ConnectionString);
}
return binding.DbContextSlavers.First();
}*/
public Object Find(Type type, params string[] keys)
{
DatabaseBind binding = DbContextBinds.First(x => x.Entities.Contains(type));
if (binding.DbContextMaster == null || binding.DbContextMaster.Database.CurrentTransaction == null)
{
return GetReader(type).Find(type, keys);
}
else
{
return GetMaster(type).Find(type, keys);
}
}
public void Add<TTableInterface>(object entity)
{
var db = GetMaster(typeof(TTableInterface));
db.Add(entity);
}
public void Add<TTableInterface>(IEnumerable<object> entities)
{
var db = GetMaster(typeof(TTableInterface));
db.AddRange(entities);
}
public void Delete<TTableInterface>(object entity)
{
var db = GetMaster(typeof(TTableInterface));
db.Remove(entity);
}
public void Delete<TTableInterface>(IEnumerable<object> entities)
{
var db = GetMaster(typeof(TTableInterface));
db.RemoveRange(entities);
}
public DbSet<T> Table<T>() where T : class
{
Type entityType = typeof(T);
DatabaseBind binding = DbContextBinds.First(x => x.Entities.Contains(entityType));
if (binding.DbContextMaster == null || binding.DbContextMaster.Database.CurrentTransaction == null)
{
return GetReader(entityType).Set<T>();
}
else
{
return GetMaster(entityType).Set<T>();
}
}
public int ExecuteSqlCommand<T>(string sql, params object[] parameterms)
{
var db = GetMaster(typeof(T)).Database;
return db.ExecuteSqlRaw(sql, parameterms);
}
public Task<int> ExecuteAsync<T>(string sql,
IEnumerable<object> parameters = default,
DbExecutionOptions options = null,
CancellationToken cancellationToken = default)
{
var db = GetMaster(typeof(T)).Database;
if (options != null)
{
db.SetCommandTimeout(options.Timeout);
}
return db.ExecuteSqlRawAsync(sql, parameters ?? [], cancellationToken);
}
public IEnumerable<TResult> Query<TTableInterface, TResult>(string sql, object parameterms = null)
{
var conn = GetBinding(typeof(TTableInterface)).SlaveConnection;
try
{
conn.Open();
return conn.Query<TResult>(sql, parameterms);
}
finally
{
conn.Close();
}
}
public IEnumerable<dynamic> Query<TTableInterface>(string sql, object parameterms = null)
{
var conn = GetBinding(typeof(TTableInterface)).SlaveConnection;
try
{
conn.Open();
return conn.Query(sql, parameterms);
}
finally
{
conn.Close();
}
}
public int SaveChanges()
{
var bindings = DbContextBinds.Where(x => x.DbContextType != null)
.Where(x => x.IsRelational && x.DbContextMaster != null)
.ToList();
if (bindings.Count() == 0)
{
throw new Exception($"Current transaction is not open.");
}
var affectedRows = 0;
foreach (var binding in bindings)
{
if (binding.DbContextMaster.Database.CurrentTransaction != null)
affectedRows += binding.DbContextMaster.SaveChanges();
}
return affectedRows;
}
public async Task<int> SaveChangesAsync()
{
var bindings = DbContextBinds.Where(x => x.DbContextType != null).Where(x => x.DbContextMaster != null);
if (bindings.Count() == 0)
{
throw new Exception($"Current transaction is not open.");
}
var affectedRows = 0;
var tasks = new List<Task<int>>();
foreach (var binding in bindings)
{
if (binding.DbContextMaster.Database.CurrentTransaction != null)
tasks.Add(binding.DbContextMaster.SaveChangesAsync());
}
await Task.WhenAll();
foreach (var task in tasks)
affectedRows += task.Result;
return affectedRows;
}
}