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 279 280 281 282 283 284 285 286 287 288 289 290
| using System.Linq.Expressions; using LiteDB; using Microsoft.Extensions.Logging;
namespace Arturia.Core.Storage;
public class LiteDbRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity<Guid> { private readonly ILiteCollection<TEntity> _collection; private readonly ILogger<LiteDbRepository<TEntity>> _logger; private readonly string _entityName;
public LiteDbRepository(LiteDbContext context, ILogger<LiteDbRepository<TEntity>> logger) { _logger = logger; _entityName = typeof(TEntity).Name;
var db = context.Database;
string collectionName = $"{typeof(TEntity).Name.ToLower()}s"; _collection = db.GetCollection<TEntity>(collectionName); }
public TEntity FindById(Guid id) { try { _logger.LogDebug("查询{Entity},ID: {Id}", _entityName, id); return _collection.FindById(id); } catch (Exception ex) { _logger.LogError(ex, "查询{Entity}失败,ID: {Id}", _entityName, id); throw; } }
public TEntity? FindFirstOrDefault(Expression<Func<TEntity, bool>> predicate) { try { _logger.LogDebug("查询{Entity},谓词: {Predicate}", _entityName, predicate); return _collection.Find(predicate).FirstOrDefault(); } catch (Exception ex) { _logger.LogError(ex, "查询{Entity}失败,谓词: {Predicate}", _entityName, predicate); throw; } }
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate) { try { _logger.LogDebug("查询{Entity}列表,谓词: {Predicate}", _entityName, predicate); return _collection.Find(predicate); } catch (Exception ex) { _logger.LogError(ex, "查询{Entity}列表失败,谓词: {Predicate}", _entityName, predicate); throw; } }
public bool Upsert(TEntity entity) { try { _logger.LogDebug("新增或更新{Entity},ID: {Id}", _entityName, entity.Id); return _collection.Upsert(entity); } catch (Exception ex) { _logger.LogError(ex, "新增或更新{Entity}失败,ID: {Id}", _entityName, entity.Id); throw; } }
public int Upsert(IEnumerable<TEntity> entities) { var list = entities.ToList(); try { _logger.LogDebug("批量新增或更新{Entity},共{Count}条", _entityName, list.Count); return _collection.Upsert(list); } catch (Exception ex) { _logger.LogError(ex, "批量新增或更新{Entity}失败,共{Count}条", _entityName, list.Count); throw; } }
public bool Upsert(Guid id, TEntity entity) { try { _logger.LogDebug("新增或更新{Entity},ID: {Id}", _entityName, id); return _collection.Upsert(id, entity); } catch (Exception ex) { _logger.LogError(ex, "新增或更新{Entity}失败,ID: {Id}", _entityName, id); throw; } }
public bool Update(TEntity entity) { try { _logger.LogDebug("更新{Entity},ID: {Id}", _entityName, entity.Id); return _collection.Update(entity); } catch (Exception ex) { _logger.LogError(ex, "更新{Entity}失败,ID: {Id}", _entityName, entity.Id); throw; } }
public bool Update(Guid id, TEntity entity) { try { _logger.LogDebug("更新{Entity},ID: {Id}", _entityName, id); return _collection.Update(id, entity); } catch (Exception ex) { _logger.LogError(ex, "更新{Entity}失败,ID: {Id}", _entityName, id); throw; } }
public int Update(IEnumerable<TEntity> entities) { var list = entities.ToList(); try { _logger.LogDebug("批量更新{Entity},共{Count}条", _entityName, list.Count); return _collection.Update(list); } catch (Exception ex) { _logger.LogError(ex, "批量更新{Entity}失败,共{Count}条", _entityName, list.Count); throw; } }
public int UpdateMany(string transform, string predicate) { if (string.IsNullOrEmpty(transform)) throw new ArgumentNullException(nameof(transform)); if (string.IsNullOrEmpty(predicate)) throw new ArgumentNullException(nameof(predicate));
try { _logger.LogDebug("批量更新{Entity},转换: {Transform},谓词: {Predicate}", _entityName, transform, predicate);
BsonExpression bsonTransform = BsonExpression.Create(transform); BsonExpression bsonPredicate = BsonExpression.Create(predicate);
return _collection.UpdateMany(bsonTransform, bsonPredicate); } catch (Exception ex) { _logger.LogError(ex, "批量更新{Entity}失败,转换: {Transform},谓词: {Predicate}", _entityName, transform, predicate); throw; } }
public Guid Insert(TEntity entity) { try { _logger.LogDebug("插入{Entity}", _entityName); return _collection.Insert(entity); } catch (Exception ex) { _logger.LogError(ex, "插入{Entity}失败", _entityName); throw; } }
public int Insert(IEnumerable<TEntity> entities) { var list = entities.ToList(); try { _logger.LogDebug("批量插入{Entity},共{Count}条", _entityName, list.Count); return _collection.Insert(list); } catch (Exception ex) { _logger.LogError(ex, "批量插入{Entity}失败,共{Count}条", _entityName, list.Count); throw; } }
public int InsertBulk(IEnumerable<TEntity> entities, int batchSize = 5000) { var list = entities.ToList(); try { _logger.LogDebug("批量插入{Entity},共{Count}条,批次大小: {BatchSize}", _entityName, list.Count, batchSize); return _collection.InsertBulk(list, batchSize); } catch (Exception ex) { _logger.LogError(ex, "批量插入{Entity}失败,共{Count}条,批次大小: {BatchSize}", _entityName, list.Count, batchSize); throw; } }
public bool EnsureIndex(Expression<Func<TEntity, bool>> expression, bool unique = false) { try { _logger.LogDebug("确保索引{Entity},表达式: {Expression},唯一: {Unique}", _entityName, expression, unique); return _collection.EnsureIndex(expression, unique); } catch (Exception ex) { _logger.LogError(ex, "确保索引{Entity}失败,表达式: {Expression},唯一: {Unique}", _entityName, expression, unique); throw; } }
public bool EnsureIndex(string name, string expression, bool unique = false) { try { _logger.LogDebug("确保索引{IndexName},表达式: {Expression},唯一: {Unique}", name, expression, unique);
BsonExpression bsonExpression = BsonExpression.Create(expression); return _collection.EnsureIndex(name, bsonExpression, unique); } catch (Exception ex) { _logger.LogError(ex, "确保索引{IndexName}失败,表达式: {Expression},唯一: {Unique}", name, expression, unique); throw; } }
public bool DropIndex(string name) { try { _logger.LogDebug("删除索引{IndexName}", name); return _collection.DropIndex(name); } catch (Exception ex) { _logger.LogError(ex, "删除索引{IndexName}失败", name); throw; } }
public bool Delete(Guid id) { try { _logger.LogDebug("删除{Entity},ID: {Id}", _entityName, id); return _collection.Delete(id); } catch (Exception ex) { _logger.LogError(ex, "删除{Entity}失败,ID: {Id}", _entityName, id); throw; } }
public int DeleteMany(Expression<Func<TEntity, bool>> predicate) { try { _logger.LogDebug("批量删除{Entity},谓词: {Predicate}", _entityName, predicate); return _collection.DeleteMany(predicate); } catch (Exception ex) { _logger.LogError(ex, "批量删除{Entity}失败,谓词: {Predicate}", _entityName, predicate); throw; } } }
|