From 948995d2eecdd2a95be8a020913622237eb4624d Mon Sep 17 00:00:00 2001 From: Young Date: Mon, 11 Nov 2024 22:18:12 +0800 Subject: [PATCH] optimise mongo repository --- src/Infrastructure/Repository/Mongo/IIdentifiable.cs | 9 ++++++--- .../Repository/Mongo/IMongoRepositoryBase.cs | 2 +- .../Repository/Mongo/MongoRepositoryBase.cs | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Infrastructure/Repository/Mongo/IIdentifiable.cs b/src/Infrastructure/Repository/Mongo/IIdentifiable.cs index ab75d10..6ccc225 100644 --- a/src/Infrastructure/Repository/Mongo/IIdentifiable.cs +++ b/src/Infrastructure/Repository/Mongo/IIdentifiable.cs @@ -1,6 +1,9 @@ -namespace Infrastructure.Repository.Mongo; +using MongoDB.Bson.Serialization.Attributes; -public interface IIdentifiable +namespace Infrastructure.Repository.Mongo; + +public interface IIdentifiable where TKey : IEquatable { - string Id { get; set; } + [BsonId] + TKey Id { get; set; } } \ No newline at end of file diff --git a/src/Infrastructure/Repository/Mongo/IMongoRepositoryBase.cs b/src/Infrastructure/Repository/Mongo/IMongoRepositoryBase.cs index c5b9396..3f81e56 100644 --- a/src/Infrastructure/Repository/Mongo/IMongoRepositoryBase.cs +++ b/src/Infrastructure/Repository/Mongo/IMongoRepositoryBase.cs @@ -3,7 +3,7 @@ namespace Infrastructure.Repository.Mongo; public interface IMongoRepositoryBase - where TEntity : class, IIdentifiable, new() where TKey : IEquatable + where TEntity : class, IIdentifiable, new() where TKey : IEquatable { IMongoCollection Collection { get; } diff --git a/src/Infrastructure/Repository/Mongo/MongoRepositoryBase.cs b/src/Infrastructure/Repository/Mongo/MongoRepositoryBase.cs index 875165e..4da6186 100644 --- a/src/Infrastructure/Repository/Mongo/MongoRepositoryBase.cs +++ b/src/Infrastructure/Repository/Mongo/MongoRepositoryBase.cs @@ -6,7 +6,7 @@ namespace Infrastructure.Repository.Mongo; public abstract class MongoRepositoryBase(IMongoDatabase database) : IMongoRepositoryBase - where TEntity : class, IIdentifiable, new() + where TEntity : class, IIdentifiable, new() where TKey : IEquatable { private readonly IMongoCollection _collection = database.GetCollection(typeof(TEntity).Name);