Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Improvements/repository (#576)

Guy Taggar7478ca7f9e19
diff --git a/src/Core/Grand.Data/LiteDb/LiteDBContext.cs b/src/Core/Grand.Data/LiteDb/LiteDBContext.cs
index 6e70d82f0..3da831aa5 100644
--- a/src/Core/Grand.Data/LiteDb/LiteDBContext.cs
+++ b/src/Core/Grand.Data/LiteDb/LiteDBContext.cs
@@ -6,7 +6,7 @@ namespace Grand.Data.LiteDb;
public class LiteDBContext : IDatabaseContext
{
- protected LiteDatabase _database;
+ private readonly LiteDatabase _database;
public LiteDBContext(LiteDatabase database)
{
diff --git a/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs b/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
index 49109a0c8..665338c9a 100644
--- a/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
+++ b/src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
@@ -19,17 +19,13 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <summary>
/// Gets the collection
/// </summary>
- protected ILiteCollection<T> _collection;
-
- public ILiteCollection<T> Collection => _collection;
+ protected ILiteCollection<T> Collection { get; init; }
/// <summary>
/// Mongo Database
/// </summary>
- protected LiteDatabase _database;
-
- public LiteDatabase Database => _database;
-
+ protected LiteDatabase Database { get; init; }
+
#endregion
#region Ctor
@@ -44,24 +40,24 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
if (!string.IsNullOrEmpty(connection.ConnectionString))
{
- _database = new LiteDatabase(connection.ConnectionString);
- _collection = _database.GetCollection<T>(typeof(T).Name);
+ Database = new LiteDatabase(connection.ConnectionString);
+ Collection = Database.GetCollection<T>(typeof(T).Name);
}
}
public LiteDBRepository(string connectionString, IAuditInfoProvider auditInfoProvider)
{
_auditInfoProvider = auditInfoProvider;
- _database = new LiteDatabase(connectionString);
- _collection = _database.GetCollection<T>(typeof(T).Name);
+ Database = new LiteDatabase(connectionString);
+ Collection = Database.GetCollection<T>(typeof(T).Name);
}
public LiteDBRepository(LiteDatabase database, IAuditInfoProvider auditInfoProvider)
{
- _database = database;
+ Database = database;
_auditInfoProvider = auditInfoProvider;
- _collection = _database.GetCollection<T>(typeof(T).Name);
+ Collection = Database.GetCollection<T>(typeof(T).Name);
}
#endregion
@@ -75,7 +71,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <returns>Entity</returns>
public virtual T GetById(string id)
{
- return _collection.Find(e => e.Id == id).FirstOrDefault();
+ return Collection.Find(e => e.Id == id).FirstOrDefault();
}
/// <summary>
@@ -98,7 +94,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
var visitor = new ToLowerInvariantVisitor();
//WORKAROUND Issue #479 - Method ToLowerInvariant() in String are not supported when convert to BsonExpression
var modifiedExpression = (Expression<Func<T, bool>>)visitor.Visit(predicate);
- return Task.FromResult(_collection.Find(modifiedExpression).FirstOrDefault());
+ return Task.FromResult(Collection.Find(modifiedExpression).FirstOrDefault());
}
/// <summary>
@@ -109,7 +105,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
{
entity.CreatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
entity.CreatedBy = _auditInfoProvider.GetCurrentUser();
- _collection.Insert(entity);
+ Collection.Insert(entity);
return entity;
}
@@ -131,7 +127,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
{
entity.UpdatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
entity.UpdatedBy = _auditInfoProvider.GetCurrentUser();
- _collection.Update(entity);
+ Collection.Update(entity);
return entity;
}
@@ -154,12 +150,12 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <param name="value">value</param>
public virtual Task UpdateField<U>(string id, Expression<Func<T, U>> expression, U value)
{
- var entity = _database.GetCollection(typeof(T).Name).FindById(new BsonValue(id));
+ var entity = Database.GetCollection(typeof(T).Name).FindById(new BsonValue(id));
var bsonValue = BsonMapper.Global.Serialize(value);
entity[GetName(expression)] = bsonValue;
entity["UpdatedOnUtc"] = _auditInfoProvider.GetCurrentDateTime();
entity["UpdatedBy"] = _auditInfoProvider.GetCurrentUser();
- _database.GetCollection(typeof(T).Name).Update(entity);
+ Database.GetCollection(typeof(T).Name).Update(entity);
return Task.CompletedTask;
}
@@ -173,20 +169,20 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <param name="value">value</param>
public virtual Task IncField<U>(string id, Expression<Func<T, U>> expression, U value)
{
- var entity = _database.GetCollection(typeof(T).Name).FindById(new BsonValue(id));
+ var entity = Database.GetCollection(typeof(T).Name).FindById(new BsonValue(id));
switch (value)
{
case int intValue:
var intrawValue = Convert.ToInt32(entity[GetName(expression)].RawValue);
var bsonIntValue = BsonMapper.Global.Serialize(intrawValue + intValue);
entity[GetName(expression)] = bsonIntValue;
- _database.GetCollection(typeof(T).Name).Update(entity);
+ Database.GetCollection(typeof(T).Name).Update(entity);
break;
case long longValue:
var longrawValue = Convert.ToInt64(entity[GetName(expression)].RawValue);
var bsonLongValue = BsonMapper.Global.Serialize(longrawValue + longValue);
entity[GetName(expression)] = bsonLongValue;
- _database.GetCollection(typeof(T).Name).Update(entity);
+ Database.GetCollection(typeof(T).Name).Update(entity);
break;
}
@@ -201,7 +197,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual Task UpdateOneAsync(Expression<Func<T, bool>> filterexpression, UpdateBuilder<T> updateBuilder)
{
- var entity = _collection.FindOne(filterexpression);
+ var entity = Collection.FindOne(filterexpression);
Update(entity, updateBuilder);
return Task.CompletedTask;
}
@@ -214,7 +210,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual Task UpdateManyAsync(Expression<Func<T, bool>> filterexpression, UpdateBuilder<T> updateBuilder)
{
- var entities = _collection.Find(filterexpression);
+ var entities = Collection.Find(filterexpression);
foreach (var entity in entities) Update(entity, updateBuilder);
return Task.CompletedTask;
}
@@ -234,7 +230,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
entity.UpdatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
entity.UpdatedBy = _auditInfoProvider.GetCurrentUser();
- _collection.Update(entity);
+ Collection.Update(entity);
return Task.CompletedTask;
}
@@ -248,7 +244,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual Task AddToSet<U>(string id, Expression<Func<T, IEnumerable<U>>> field, U value)
{
- var collection = _database.GetCollection(_collection.Name);
+ var collection = Database.GetCollection(Collection.Name);
var entity = collection.FindById(new BsonValue(id));
var fieldName = ((MemberExpression)field.Body).Member.Name;
@@ -279,7 +275,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
public virtual Task UpdateToSet<U, Z>(string id, Expression<Func<T, IEnumerable<U>>> field,
Expression<Func<U, Z>> elemFieldMatch, Z elemMatch, U value)
{
- var collection = _database.GetCollection(_collection.Name);
+ var collection = Database.GetCollection(Collection.Name);
var entity = collection.FindById(new BsonValue(id));
var fieldName = ((MemberExpression)field.Body).Member.Name;
@@ -315,7 +311,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
public virtual Task UpdateToSet<U>(string id, Expression<Func<T, IEnumerable<U>>> field,
Expression<Func<U, bool>> elemFieldMatch, U value)
{
- var collection = _database.GetCollection(_collection.Name);
+ var collection = Database.GetCollection(Collection.Name);
var entity = collection.FindById(new BsonValue(id));
var fieldName = ((MemberExpression)field.Body).Member.Name;
if (entity == null) return Task.CompletedTask;
@@ -357,7 +353,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual Task UpdateToSet<U>(Expression<Func<T, IEnumerable<U>>> field, U elemFieldMatch, U value)
{
- var collection = _database.GetCollection(_collection.Name);
+ var collection = Database.GetCollection(Collection.Name);
var fieldName = ((MemberExpression)field.Body).Member.Name;
var records = collection.Find(Query.EQ($"{fieldName}[*] ANY", elemFieldMatch.ToString())).ToList();
foreach (var entity in records)
@@ -393,7 +389,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
public virtual Task PullFilter<U, Z>(string id, Expression<Func<T, IEnumerable<U>>> field,
Expression<Func<U, Z>> elemFieldMatch, Z elemMatch)
{
- var collection = _database.GetCollection(_collection.Name);
+ var collection = Database.GetCollection(Collection.Name);
var fieldName = ((MemberExpression)field.Body).Member.Name;
var member = ((MemberExpression)elemFieldMatch.Body).Member;
@@ -445,7 +441,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
public virtual Task PullFilter<U>(string id, Expression<Func<T, IEnumerable<U>>> field,
Expression<Func<U, bool>> elemFieldMatch)
{
- var collection = _database.GetCollection(_collection.Name);
+ var collection = Database.GetCollection(Collection.Name);
var entity = collection.FindById(new BsonValue(id));
var fieldName = ((MemberExpression)field.Body).Member.Name;
if (entity == null) return Task.CompletedTask;
@@ -477,7 +473,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual Task Pull(string id, Expression<Func<T, IEnumerable<string>>> field, string element)
{
- var collection = _database.GetCollection(_collection.Name);
+ var collection = Database.GetCollection(Collection.Name);
var fieldName = ((MemberExpression)field.Body).Member.Name;
if (string.IsNullOrEmpty(id))
{
@@ -516,7 +512,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <param name="entity">Entity</param>
public virtual void Delete(T entity)
{
- _collection.Delete(new BsonValue(entity.Id));
+ Collection.Delete(new BsonValue(entity.Id));
}
/// <summary>
@@ -545,7 +541,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual Task DeleteManyAsync(Expression<Func<T, bool>> filterExpression)
{
- _collection.DeleteMany(filterExpression);
+ Collection.DeleteMany(filterExpression);
return Task.CompletedTask;
}
@@ -554,7 +550,7 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// </summary>
public Task ClearAsync()
{
- _collection.DeleteAll();
+ Collection.DeleteAll();
return Task.CompletedTask;
}
@@ -565,14 +561,14 @@ public class LiteDBRepository<T> : IRepository<T> where T : BaseEntity
/// <summary>
/// Gets a table
/// </summary>
- public virtual IQueryable<T> Table => _collection.Query().ToEnumerable().AsQueryable();
+ public virtual IQueryable<T> Table => Collection.Query().ToEnumerable().AsQueryable();
/// <summary>
/// Gets a table collection
/// </summary>
public virtual IQueryable<C> TableCollection<C>() where C : class
{
- return _database.GetCollection<C>(nameof(T)).Query().ToEnumerable().AsQueryable();
+ return Database.GetCollection<C>(nameof(T)).Query().ToEnumerable().AsQueryable();
}
#endregion
diff --git a/src/Core/Grand.Data/LiteDb/LiteDBStoreFilesContext.cs b/src/Core/Grand.Data/LiteDb/LiteDBStoreFilesContext.cs
index fa5d12d96..86f878a30 100644
--- a/src/Core/Grand.Data/LiteDb/LiteDBStoreFilesContext.cs
+++ b/src/Core/Grand.Data/LiteDb/LiteDBStoreFilesContext.cs
@@ -5,7 +5,7 @@ namespace Grand.Data.LiteDb;
public class LiteDBStoreFilesContext : IStoreFilesContext
{
- protected LiteDatabase _database;
+ private readonly LiteDatabase _database;
public LiteDBStoreFilesContext(LiteDatabase database)
{
@@ -16,7 +16,7 @@ public class LiteDBStoreFilesContext : IStoreFilesContext
{
var fs = _database.FileStorage;
var file = fs.FindById(id);
-
+
if (file == null)
throw new ArgumentNullException(nameof(file));
diff --git a/src/Core/Grand.Data/Mongo/MongoDBContext.cs b/src/Core/Grand.Data/Mongo/MongoDBContext.cs
index 8a1b62b4d..ce8d3a33d 100644
--- a/src/Core/Grand.Data/Mongo/MongoDBContext.cs
+++ b/src/Core/Grand.Data/Mongo/MongoDBContext.cs
@@ -6,7 +6,7 @@ namespace Grand.Data.Mongo;
public class MongoDBContext : IDatabaseContext
{
- protected IMongoDatabase _database;
+ private readonly IMongoDatabase _database;
public MongoDBContext(IMongoDatabase mongodatabase)
{
diff --git a/src/Core/Grand.Data/Mongo/MongoRepository.cs b/src/Core/Grand.Data/Mongo/MongoRepository.cs
index 1a4d6a97c..8e30468b6 100644
--- a/src/Core/Grand.Data/Mongo/MongoRepository.cs
+++ b/src/Core/Grand.Data/Mongo/MongoRepository.cs
@@ -17,14 +17,12 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <summary>
/// Gets the collection
/// </summary>
- protected IMongoCollection<T> _collection;
-
- public IMongoCollection<T> Collection => _collection;
+ public IMongoCollection<T> Collection { get; protected init; }
/// <summary>
/// Mongo Database
/// </summary>
- protected IMongoDatabase _database;
+ protected IMongoDatabase Database { get; init; }
#endregion
@@ -33,35 +31,29 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <summary>
/// Ctor
/// </summary>
- public MongoRepository(IAuditInfoProvider auditInfoProvider)
+ public MongoRepository(IAuditInfoProvider auditInfoProvider) : this(
+ DataSettingsManager.Instance.LoadSettings().ConnectionString, auditInfoProvider)
{
- _auditInfoProvider = auditInfoProvider;
- var connection = DataSettingsManager.Instance.LoadSettings();
-
- if (!string.IsNullOrEmpty(connection.ConnectionString))
- {
- var client = new MongoClient(connection.ConnectionString);
- var databaseName = new MongoUrl(connection.ConnectionString).DatabaseName;
- _database = client.GetDatabase(databaseName);
- _collection = _database.GetCollection<T>(typeof(T).Name);
- }
}
public MongoRepository(string connectionString, IAuditInfoProvider auditInfoProvider)
{
_auditInfoProvider = auditInfoProvider;
- var client = new MongoClient(connectionString);
- var databaseName = new MongoUrl(connectionString).DatabaseName;
- _database = client.GetDatabase(databaseName);
- _collection = _database.GetCollection<T>(typeof(T).Name);
- }
-
+ if (!string.IsNullOrEmpty(connectionString))
+ {
+ var client = new MongoClient(connectionString);
+ var databaseName = new MongoUrl(connectionString).DatabaseName;
+ Database = client.GetDatabase(databaseName);
+ Collection = Database.GetCollection<T>(typeof(T).Name);
+ }
+ }
+
public MongoRepository(IMongoDatabase database, IAuditInfoProvider auditInfoProvider)
{
- _database = database;
+ Database = database;
_auditInfoProvider = auditInfoProvider;
- _collection = _database.GetCollection<T>(typeof(T).Name);
+ Collection = Database.GetCollection<T>(typeof(T).Name);
}
#endregion
@@ -75,7 +67,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <returns>Entity</returns>
public virtual T GetById(string id)
{
- return _collection.Find(e => e.Id == id).FirstOrDefault();
+ return Collection.Find(e => e.Id == id).FirstOrDefault();
}
/// <summary>
@@ -85,7 +77,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <returns>Entity</returns>
public virtual Task<T> GetByIdAsync(string id)
{
- return _collection.Find(e => e.Id == id).FirstOrDefaultAsync();
+ return Collection.Find(e => e.Id == id).FirstOrDefaultAsync();
}
/// <summary>
@@ -95,7 +87,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <returns>Entity</returns>
public virtual Task<T> GetOneAsync(Expression<Func<T, bool>> predicate)
{
- return _collection.Find(predicate).FirstOrDefaultAsync();
+ return Collection.Find(predicate).FirstOrDefaultAsync();
}
/// <summary>
@@ -106,7 +98,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
{
entity.CreatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
entity.CreatedBy = _auditInfoProvider.GetCurrentUser();
- _collection.InsertOne(entity);
+ Collection.InsertOne(entity);
return entity;
}
@@ -118,7 +110,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
{
entity.CreatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
entity.CreatedBy = _auditInfoProvider.GetCurrentUser();
- await _collection.InsertOneAsync(entity);
+ await Collection.InsertOneAsync(entity);
return entity;
}
@@ -130,7 +122,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
{
entity.UpdatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
entity.UpdatedBy = _auditInfoProvider?.GetCurrentUser();
- _collection.ReplaceOne(x => x.Id == entity.Id, entity, new ReplaceOptions { IsUpsert = false });
+ Collection.ReplaceOne(x => x.Id == entity.Id, entity, new ReplaceOptions { IsUpsert = false });
return entity;
}
@@ -142,7 +134,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
{
entity.UpdatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
entity.UpdatedBy = _auditInfoProvider.GetCurrentUser();
- await _collection.ReplaceOneAsync(x => x.Id == entity.Id, entity,
+ await Collection.ReplaceOneAsync(x => x.Id == entity.Id, entity,
new ReplaceOptions { IsUpsert = false });
return entity;
}
@@ -163,7 +155,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser())
.Set(expression, value);
- await _collection.UpdateOneAsync(filter, update);
+ await Collection.UpdateOneAsync(filter, update);
}
/// <summary>
@@ -180,7 +172,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var update = Builders<T>.Update
.Inc(expression, value);
- await _collection.UpdateOneAsync(filter, update);
+ await Collection.UpdateOneAsync(filter, update);
}
/// <summary>
@@ -195,7 +187,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
updateBuilder.Set(x => x.UpdatedOnUtc, _auditInfoProvider.GetCurrentDateTime());
updateBuilder.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser());
var update = Builders<T>.Update.Combine(updateBuilder.Fields);
- await _collection.UpdateOneAsync(filterExpression, update);
+ await Collection.UpdateOneAsync(filterExpression, update);
}
/// <summary>
@@ -210,7 +202,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
updateBuilder.Set(x => x.UpdatedOnUtc, _auditInfoProvider.GetCurrentDateTime());
updateBuilder.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser());
var update = Builders<T>.Update.Combine(updateBuilder.Fields);
- await _collection.UpdateManyAsync(filterExpression, update);
+ await Collection.UpdateManyAsync(filterExpression, update);
}
/// <summary>
@@ -230,7 +222,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var updateDate = Builders<T>.Update.Set(x => x.UpdatedOnUtc, _auditInfoProvider.GetCurrentDateTime());
var updateUser = Builders<T>.Update.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser());
var combinedUpdate = Builders<T>.Update.Combine(update, updateDate, updateUser);
- await _collection.UpdateOneAsync(filter, combinedUpdate);
+ await Collection.UpdateOneAsync(filter, combinedUpdate);
}
/// <summary>
@@ -249,14 +241,14 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var filter = Builders<T>.Filter.Eq(x => x.Id, id)
& Builders<T>.Filter.ElemMatch(field, Builders<U>.Filter.Eq(elemFieldMatch, elemMatch));
- var me = field.Body as MemberExpression;
+ var me = (MemberExpression)field.Body;
var minfo = me.Member;
var update = Builders<T>.Update.Set($"{minfo.Name}.$", value);
var updateDate = Builders<T>.Update.Set(x => x.UpdatedOnUtc, _auditInfoProvider.GetCurrentDateTime());
var updateUser = Builders<T>.Update.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser());
var combinedUpdate = Builders<T>.Update.Combine(update, updateDate, updateUser);
- await _collection.UpdateOneAsync(filter, combinedUpdate);
+ await Collection.UpdateOneAsync(filter, combinedUpdate);
}
/// <summary>
@@ -275,7 +267,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
: Builders<T>.Filter.Eq(x => x.Id, id)
& Builders<T>.Filter.ElemMatch(field, elemFieldMatch);
- var me = field.Body as MemberExpression;
+ var me = (MemberExpression)field.Body;
var minfo = me.Member;
var update = Builders<T>.Update.Set($"{minfo.Name}.$", value);
@@ -283,9 +275,9 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var updateUser = Builders<T>.Update.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser());
var combinedUpdate = Builders<T>.Update.Combine(update, updateDate, updateUser);
if (string.IsNullOrEmpty(id))
- await _collection.UpdateManyAsync(filter, combinedUpdate);
+ await Collection.UpdateManyAsync(filter, combinedUpdate);
else
- await _collection.UpdateOneAsync(filter, combinedUpdate);
+ await Collection.UpdateOneAsync(filter, combinedUpdate);
}
/// <summary>
@@ -299,7 +291,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual async Task UpdateToSet<U>(Expression<Func<T, IEnumerable<U>>> field, U elemFieldMatch, U value)
{
- var me = field.Body as MemberExpression;
+ var me = (MemberExpression)field.Body;
var minfo = me.Member;
var filter = new BsonDocument {
@@ -312,7 +304,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var updateUser = Builders<T>.Update.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser());
var combinedUpdate = Builders<T>.Update.Combine(update, updateDate, updateUser);
- await _collection.UpdateManyAsync(filter, combinedUpdate);
+ await Collection.UpdateManyAsync(filter, combinedUpdate);
}
/// <summary>
@@ -338,9 +330,9 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var combinedUpdate = Builders<T>.Update.Combine(update, updateDate, updateUser);
if (string.IsNullOrEmpty(id))
- await _collection.UpdateManyAsync(filter, combinedUpdate);
+ await Collection.UpdateManyAsync(filter, combinedUpdate);
else
- await _collection.UpdateOneAsync(filter, combinedUpdate);
+ await Collection.UpdateOneAsync(filter, combinedUpdate);
}
/// <summary>
@@ -361,7 +353,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var updateUser = Builders<T>.Update.Set(x => x.UpdatedBy, _auditInfoProvider.GetCurrentUser());
var combinedUpdate = Builders<T>.Update.Combine(update, updateDate, updateUser);
- await _collection.UpdateOneAsync(filter, combinedUpdate);
+ await Collection.UpdateOneAsync(filter, combinedUpdate);
}
/// <summary>
@@ -380,9 +372,9 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
var combinedUpdate = Builders<T>.Update.Combine(update, updateDate, updateUser);
if (string.IsNullOrEmpty(id))
- await _collection.UpdateManyAsync(Builders<T>.Filter.Where(x => true), combinedUpdate);
+ await Collection.UpdateManyAsync(Builders<T>.Filter.Where(x => true), combinedUpdate);
else
- await _collection.UpdateOneAsync(Builders<T>.Filter.Eq(x => x.Id, id), combinedUpdate);
+ await Collection.UpdateOneAsync(Builders<T>.Filter.Eq(x => x.Id, id), combinedUpdate);
}
/// <summary>
@@ -391,7 +383,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <param name="entity">Entity</param>
public virtual void Delete(T entity)
{
- _collection.FindOneAndDelete(e => e.Id == entity.Id);
+ Collection.FindOneAndDelete(e => e.Id == entity.Id);
}
/// <summary>
@@ -400,7 +392,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <param name="entity">Entity</param>
public virtual async Task<T> DeleteAsync(T entity)
{
- await _collection.DeleteOneAsync(e => e.Id == entity.Id);
+ await Collection.DeleteOneAsync(e => e.Id == entity.Id);
return entity;
}
@@ -420,7 +412,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <returns></returns>
public virtual async Task DeleteManyAsync(Expression<Func<T, bool>> filterExpression)
{
- await _collection.DeleteManyAsync(filterExpression);
+ await Collection.DeleteManyAsync(filterExpression);
}
/// <summary>
@@ -428,7 +420,7 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// </summary>
public Task ClearAsync()
{
- return _collection.DeleteManyAsync(Builders<T>.Filter.Empty);
+ return Collection.DeleteManyAsync(Builders<T>.Filter.Empty);
}
#endregion
@@ -438,14 +430,14 @@ public class MongoRepository<T> : IRepository<T> where T : BaseEntity
/// <summary>
/// Gets a table
/// </summary>
- public virtual IQueryable<T> Table => _collection.AsQueryable();
+ public virtual IQueryable<T> Table => Collection.AsQueryable();
/// <summary>
/// Gets a table collection
/// </summary>
public virtual IQueryable<C> TableCollection<C>() where C : class
{
- return _database.GetCollection<C>(typeof(T).Name).AsQueryable();
+ return Database.GetCollection<C>(typeof(T).Name).AsQueryable();
}
#endregion
diff --git a/src/Core/Grand.Data/Mongo/MongoStoreFilesContext.cs b/src/Core/Grand.Data/Mongo/MongoStoreFilesContext.cs
index 3a7ecace1..07f095e70 100644
--- a/src/Core/Grand.Data/Mongo/MongoStoreFilesContext.cs
+++ b/src/Core/Grand.Data/Mongo/MongoStoreFilesContext.cs
@@ -6,14 +6,14 @@ namespace Grand.Data.Mongo;
public class MongoStoreFilesContext : IStoreFilesContext
{
- protected IMongoDatabase _database;
+ private readonly IMongoDatabase _database;
public MongoStoreFilesContext()
{
var connectionString = DataSettingsManager.Instance.LoadSettings().ConnectionString;
- var mongourl = new MongoUrl(connectionString);
- var databaseName = mongourl.DatabaseName;
+ var mongoUrl = new MongoUrl(connectionString);
+ var databaseName = mongoUrl.DatabaseName;
_database = new MongoClient(connectionString).GetDatabase(databaseName);
}
diff --git a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryMock.cs b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryMock.cs
index 93330677a..12388d8ef 100644
--- a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryMock.cs
+++ b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryMock.cs
@@ -8,8 +8,8 @@ public class LiteDBRepositoryMock<T> : LiteDBRepository<T>, IRepository<T> where
{
public LiteDBRepositoryMock() : base(Guid.NewGuid().ToString(), new AuditInfoProvider())
{
- _database = new LiteDatabase(new MemoryStream());
- _database.DropCollection(typeof(T).Name);
- _collection = _database.GetCollection<T>(typeof(T).Name);
+ Database = new LiteDatabase(new MemoryStream());
+ Database.DropCollection(typeof(T).Name);
+ Collection = Database.GetCollection<T>(typeof(T).Name);
}
}
\ No newline at end of file
diff --git a/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs b/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs
index 84aee7d7f..34491be57 100644
--- a/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs
+++ b/src/Tests/Grand.Data.Tests/MongoDb/MongoDBRepositoryTest.cs
@@ -7,7 +7,7 @@ using MongoDB.Bson.Serialization.Serializers;
namespace Grand.Data.Tests.MongoDb;
-public class MongoDBRepositoryTest<T> : MongoRepository<T>, IRepository<T> where T : BaseEntity
+public class MongoDBRepositoryTest<T> : MongoRepository<T> where T : BaseEntity
{
public MongoDBRepositoryTest() : base(
DriverTestConfiguration.Client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName),
@@ -22,8 +22,8 @@ public class MongoDBRepositoryTest<T> : MongoRepository<T>, IRepository<T> where
var client = DriverTestConfiguration.Client;
- _database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
- _database.DropCollection(typeof(T).Name);
- _collection = _database.GetCollection<T>(typeof(T).Name);
+ Database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
+ Database.DropCollection(typeof(T).Name);
+ Collection = Database.GetCollection<T>(typeof(T).Name);
}
}
\ No newline at end of file