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
|
using System.Data;
using Dapper;
namespace CrupestApi.Commons.Crud;
[Flags]
public enum UpdateBehavior
{
None = 0,
SaveNull = 1
}
public class CrudService<TEntity> : IDisposable where TEntity : class
{
protected readonly TableInfo _table;
protected readonly string? _connectionName;
protected readonly IDbConnection _dbConnection;
private readonly bool _shouldDisposeConnection;
private readonly ILogger<CrudService<TEntity>> _logger;
public CrudService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, ILoggerFactory loggerFactory)
{
_connectionName = GetConnectionName();
_table = tableInfoFactory.Get(typeof(TEntity));
_dbConnection = dbConnectionFactory.Get(_connectionName);
_shouldDisposeConnection = dbConnectionFactory.ShouldDisposeConnection;
_logger = loggerFactory.CreateLogger<CrudService<TEntity>>();
CheckDatabase(_dbConnection);
}
protected virtual string GetConnectionName()
{
return typeof(TEntity).Name;
}
protected virtual void CheckDatabase(IDbConnection dbConnection)
{
if (!_table.CheckExistence(dbConnection))
{
DoInitializeDatabase(dbConnection);
}
}
protected virtual void DoInitializeDatabase(IDbConnection connection)
{
using var transaction = connection.BeginTransaction();
connection.Execute(_table.GenerateCreateTableSql(), transaction: transaction);
transaction.Commit();
}
public void Dispose()
{
if (_shouldDisposeConnection)
_dbConnection.Dispose();
}
public List<TEntity> GetAll()
{
var result = _table.Select<TEntity>(_dbConnection, null);
return result;
}
public TEntity GetByKey(object key)
{
var result = _table.Select<TEntity>(_dbConnection, null, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key));
return result.Single();
}
public IInsertClause ConvertEntityToInsertClauses(TEntity entity)
{
var result = new InsertClause();
foreach (var column in _table.PropertyColumns)
{
var value = column.PropertyInfo!.GetValue(entity);
result.Add(column.ColumnName, value);
}
return result;
}
public string Create(TEntity entity)
{
var insertClause = ConvertEntityToInsertClauses(entity);
var key = _table.Insert(_dbConnection, insertClause);
return (string)key;
}
public IUpdateClause ConvertEntityToUpdateClauses(TEntity entity, UpdateBehavior behavior)
{
var result = UpdateClause.Create();
var saveNull = behavior.HasFlag(UpdateBehavior.SaveNull);
foreach (var column in _table.PropertyColumns)
{
var value = column.PropertyInfo!.GetValue(entity);
if (!saveNull && value is null) continue;
result.Add(column.ColumnName, value);
}
return result;
}
public void UpdateByKey(object key, TEntity entity, UpdateBehavior behavior)
{
_table.Update(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key),
ConvertEntityToUpdateClauses(entity, behavior));
}
public void DeleteByKey(object key)
{
_table.Delete(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key));
}
}
|