我是 .NET core 的新手,正在设计 Asp.net core 应用程序分层架构。我们正在使用具有工作单元存储库模式的通用存储库。完成了通用存储库方法,需要对带有集合的工作单元有一些了解。
通用存储库
public interface IMongoRepository<TDocument> where TDocument : IAudiableEntity
{
void InsertOne(TDocument document);
Task<TDocument> InsertOneAsync(TDocument document);
void InsertMany(ICollection<TDocument> documents);
Task InsertManyAsync(ICollection<TDocument> documents);
void ReplaceOne(TDocument document);
Task ReplaceOneAsync(TDocument document);
void DeleteOne(Expression<Func<TDocument, bool>> filterExpression);
Task DeleteOneAsync(Expression<Func<TDocument, bool>> filterExpression);
void DeleteById(string id);
Task DeleteByIdAsync(string id);
void DeleteMany(Expression<Func<TDocument, bool>> filterExpression);
Task DeleteManyAsync(Expression<Func<TDocument, bool>> filterExpression);
}
public class MongoRepository<TDocument> : IMongoRepository<TDocument>
where TDocument : IAudiableEntity
{
private readonly IMongoCollection<TDocument> _collection;
public MongoRepository(IMongoDbSettings settings)
{
var database = new MongoClient(settings.ConnectionString).GetDatabase(settings.DatabaseName);
_collection = database.GetCollection<TDocument>(GetCollectionName(typeof(TDocument)));
}
private protected string GetCollectionName(Type documentType)
{
return ((BsonCollectionAttribute)documentType.GetCustomAttributes(
typeof(BsonCollectionAttribute),
true)
.FirstOrDefault())?.CollectionName;
}
public virtual void InsertOne(TDocument document)
{
_collection.InsertOne(document);
}
public virtual async Task<TDocument> InsertOneAsync(TDocument document)
{
await _collection.InsertOneAsync(document);
return document;
}
public void InsertMany(ICollection<TDocument> documents)
{
_collection.InsertMany(documents);
}
public virtual async Task InsertManyAsync(ICollection<TDocument> documents)
{
await _collection.InsertManyAsync(documents);
}
public void ReplaceOne(TDocument document)
{
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, document.Id);
if (document is Slides slide)
{
slide.IsDirty = true;
}
_collection.FindOneAndReplace(filter, document);
}
public void ReplaceWODirtyOne(TDocument document)
{
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, document.Id);
_collection.FindOneAndReplace(filter, document);
}
public virtual async Task ReplaceOneAsync(TDocument document)
{
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, document.Id);
if (document is Slides slide)
{
slide.IsDirty = true;
}
await _collection.FindOneAndReplaceAsync(filter, document);
}
public virtual async Task ReplaceWODirtyOneAsync(TDocument document)
{
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, document.Id);
await _collection.FindOneAndReplaceAsync(filter, document);
}
public void DeleteOne(Expression<Func<TDocument, bool>> filterExpression)
{
_collection.FindOneAndDelete(filterExpression);
}
public Task DeleteOneAsync(Expression<Func<TDocument, bool>> filterExpression)
{
return Task.Run(() => _collection.FindOneAndDeleteAsync(filterExpression));
}
public void DeleteById(string id)
{
var objectId = new ObjectId(id);
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId);
_collection.FindOneAndDelete(filter);
}
public Task DeleteByIdAsync(string id)
{
return Task.Run(() =>
{
var objectId = new ObjectId(id);
var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId);
_collection.FindOneAndDeleteAsync(filter);
});
}
public void DeleteMany(Expression<Func<TDocument, bool>> filterExpression)
{
_collection.DeleteMany(filterExpression);
}
public Task DeleteManyAsync(Expression<Func<TDocument, bool>> filterExpression)
{
return Task.Run(() => _collection.DeleteManyAsync(filterExpression));
}
}
如何完成这里的工作单元,让我知道可能性。
我举个例子来回答你的问题会更容易理解。
假设您有一个名为“ApplicationUser”的存储库
您的界面将如下所示
public interface IApplicationUserRepository : IMongoRepository<ApplicationUser>
{
}
您的存储库就在这里;
public class ApplicationUserRepository : IMongoRepository<ApplicationUser>, IApplicationUserRepository
{
public ApplicationUserRepository(EFContext context) : base(context)
{
}
}
如果你想使用 UnitOfWork 模式,你应该将你的接口添加到 IUnitOfWork 类中
public interface IUnitOfWork : IDisposable
{
IApplicationUserRepository ApplicationUserRepository { get; set; }
EFContext Context { get; }
List<string> Tables { get; }
List<string> Columns(string tableName);
int Commit();
}
该示例可能不适合您的结构,但您需要创建的结构将如下所示。此外,对于 EF 使用哪个数据库并不重要。
我会放弃通用存储库并直接使用 MongoDB 驱动程序,因为 MongoCollection 已经是一个存储库。 然后您可以使用交易功能或必须提供。