C# 如何绕过Dublicate代码Mongo/Core API

C# 如何绕过Dublicate代码Mongo/Core API,c#,mongodb,core,core-api,C#,Mongodb,Core,Core Api,我有两个实体 public class EndUser { public const string CollectionName = "Clients"; [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } [BsonElement("FirstName")] public string FirstName { get; set; }

我有两个实体

public class EndUser
{
    public const string CollectionName = "Clients";

    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    [BsonElement("FirstName")]
    public string FirstName { get; set; }

    [BsonElement("LastName")]
    public string LastName { get; set; }
}

public class AppDeveloper : EndUser
{
    [BsonElement("CompanyName")]
    public string CompanyName { get; set; }

    [BsonElement("CompanyAdress")]
    public string CompanyAdress { get; set; }
}
并为Mongo创建上下文

public class MongoContext
{
    private readonly IMongoDatabase _database = null;

    public MongoContext(IOptions<MongoSettings> _settings)
    {
        var client = new MongoClient(_settings.Value.ConnectionString);
        if (client != null)
            _database = client.GetDatabase(_settings.Value.Database);
    }

    public IMongoCollection<EndUser> Users
    {
        get
        {
            IMongoCollection<EndUser> dbConnection = _database.GetCollection<EndUser>(EndUser.CollectionName);
            return dbConnection;
        }
    }

    public IMongoCollection<AppDeveloper> Developers
    {
        get
        {
            IMongoCollection<AppDeveloper> dbConnection = _database.GetCollection<AppDeveloper>(AppDeveloper.CollectionName);
            return dbConnection;
        }
    }
}
公共类MongoContext
{
私有只读IMongoDatabase _database=null;
公共MongoContext(IOOptions\u设置)
{
var client=newmongoclient(_settings.Value.ConnectionString);
如果(客户端!=null)
_database=client.GetDatabase(_settings.Value.database);
}
公共收藏用户
{
收到
{
IMongoCollection dbConnection=\u database.GetCollection(EndUser.CollectionName);
返回数据库连接;
}
}
公共收藏开发商
{
收到
{
IMongoCollection dbConnection=\u database.GetCollection(AppDeveloper.CollectionName);
返回数据库连接;
}
}
}
例如,现在我遇到了dublicate代码的问题

用户实现

public class UserRepository : IMainMongoRepository<EndUser>, IClientSearching<EndUser>
{
    private readonly MongoContext mongoContext = null;

    public UserRepository(MongoContext mongoContext)
    {
        this.mongoContext = mongoContext;
    }

    public async Task AddAsync(EndUser user)
    {
        try
        {
            await mongoContext.Users.InsertOneAsync(user);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public async Task<EndUser> GetByIdAsync(string id)
    {
        try
        {
            FilterDefinition<EndUser> filter = Builders<EndUser>.Filter.Eq("Id", id);
            EndUser user = await mongoContext.Users.Find(filter).FirstOrDefaultAsync();
            return user;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 public class DeveloperRepository : IMainMongoRepository<AppDeveloper>, IClientSearching<AppDeveloper>
{
    private readonly MongoContext mongoContext = null;

    public DeveloperRepository(MongoContext mongoContext)
    {
        this.mongoContext = mongoContext;
    }

    public async Task AddAsync(AppDeveloper developer)
    {
        try
        {
            await mongoContext.Developers.InsertOneAsync(developer);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public async Task<AppDeveloper> GetByIdAsync(string id)
    {
        try
        {
            FilterDefinition<AppDeveloper> filter = Builders<AppDeveloper>.Filter.Eq("Id", id);
            AppDeveloper developer = await mongoContext.Developers.Find(filter).FirstOrDefaultAsync();
            return developer;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
public类用户存储库:IMainMongoRepository,IClientSearching
{
私有只读MongoContext MongoContext=null;
公共用户存储库(MongoContext MongoContext)
{
this.mongoContext=mongoContext;
}
公共异步任务AddAsync(最终用户)
{
尝试
{
等待mongoContext.Users.InsertOneAsync(user);
}
捕获(例外情况除外)
{
掷骰子;
}
}
公共异步任务GetByIdAsync(字符串id)
{
尝试
{
FilterDefinition filter=Builders.filter.Eq(“Id”,Id);
EndUser user=wait mongoContext.Users.Find(filter.FirstOrDefaultAsync();
返回用户;
}
捕获(例外情况除外)
{
掷骰子;
}
}
和开发人员实现

public class UserRepository : IMainMongoRepository<EndUser>, IClientSearching<EndUser>
{
    private readonly MongoContext mongoContext = null;

    public UserRepository(MongoContext mongoContext)
    {
        this.mongoContext = mongoContext;
    }

    public async Task AddAsync(EndUser user)
    {
        try
        {
            await mongoContext.Users.InsertOneAsync(user);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public async Task<EndUser> GetByIdAsync(string id)
    {
        try
        {
            FilterDefinition<EndUser> filter = Builders<EndUser>.Filter.Eq("Id", id);
            EndUser user = await mongoContext.Users.Find(filter).FirstOrDefaultAsync();
            return user;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 public class DeveloperRepository : IMainMongoRepository<AppDeveloper>, IClientSearching<AppDeveloper>
{
    private readonly MongoContext mongoContext = null;

    public DeveloperRepository(MongoContext mongoContext)
    {
        this.mongoContext = mongoContext;
    }

    public async Task AddAsync(AppDeveloper developer)
    {
        try
        {
            await mongoContext.Developers.InsertOneAsync(developer);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public async Task<AppDeveloper> GetByIdAsync(string id)
    {
        try
        {
            FilterDefinition<AppDeveloper> filter = Builders<AppDeveloper>.Filter.Eq("Id", id);
            AppDeveloper developer = await mongoContext.Developers.Find(filter).FirstOrDefaultAsync();
            return developer;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
公共类DeveloperRepository:ImainMongorRepository,IClientSearching
{
私有只读MongoContext MongoContext=null;
公共DeveloperRepository(MongoContext MongoContext)
{
this.mongoContext=mongoContext;
}
公共异步任务AddAsync(AppDeveloper开发人员)
{
尝试
{
等待mongoContext.Developers.InsertOneAsync(developer);
}
捕获(例外情况除外)
{
掷骰子;
}
}
公共异步任务GetByIdAsync(字符串id)
{
尝试
{
FilterDefinition filter=Builders.filter.Eq(“Id”,Id);
AppDeveloper developer=wait mongoContext.Developers.Find(filter.FirstOrDefaultAsync();
归还开发商;
}
捕获(例外情况除外)
{
掷骰子;
}
}
在本例中,我对不同的模型有相同的实现。使用相同的代码(现在我只有两个模型,但如果我使用10个),这是不好的,我不知道如何修复,如何创建正确的代码而不使用dublicate?(模型应保存在一个集合中)


我有这个模型的CRUD,大部分实现都有相同的问题

你可以这样做

public class MongoContext<T>
{
    private readonly IMongoDatabase _database = null;
    private readonly string _collection = null;

    public MongoContext(IOptions<MongoSettings> _settings, string collection)
    {
        var client = new MongoClient(_settings.Value.ConnectionString);
        if (client != null)
            _database = client.GetDatabase(_settings.Value.Database);
        _collection = collection;
    }

    public IMongoCollection<T> Objects
    {
        get
        {
            IMongoCollection<T> dbConnection = _database.GetCollection<T>(_collection);
            return dbConnection;
        }
    }
}

public class Repository<T> : IMainMongoRepository<T>, IClientSearching<T>
{
    private readonly MongoContext<T> mongoContext = null;

    public Repository(MongoContext<T> mongoContext)
    {
        this.mongoContext = mongoContext;
    }

    public async Task AddAsync(T obj)
    {
        try
        {
            await mongoContext.Objects.InsertOneAsync(obj);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public async Task<T> GetByIdAsync(string id)
    {
        try
        {
            FilterDefinition<T> filter = Builders<T>.Filter.Eq("Id", id);
            T obj = await mongoContext.Objects.Find(filter).FirstOrDefaultAsync();
            return obj;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}
公共类MongoContext
{
私有只读IMongoDatabase _database=null;
私有只读字符串_collection=null;
公共MongoContext(IOOptions\u设置、字符串集合)
{
var client=newmongoclient(_settings.Value.ConnectionString);
如果(客户端!=null)
_database=client.GetDatabase(_settings.Value.database);
_收集=收集;
}
公共收集对象
{
收到
{
IMongoCollection dbConnection=\u database.GetCollection(\u collection);
返回数据库连接;
}
}
}
公共类存储库:IMainMongoRepository、IClientSearching
{
私有只读MongoContext MongoContext=null;
公共存储库(MongoContext MongoContext)
{
this.mongoContext=mongoContext;
}
公共异步任务AddAsync(T obj)
{
尝试
{
等待mongoContext.Objects.InsertOneAsync(obj);
}
捕获(例外情况除外)
{
掷骰子;
}
}
公共异步任务GetByIdAsync(字符串id)
{
尝试
{
FilterDefinition filter=Builders.filter.Eq(“Id”,Id);
T obj=wait mongoContext.Objects.Find(filter.FirstOrDefaultAsync();
返回obj;
}
捕获(例外情况除外)
{
掷骰子;
}
}
}

如果唯一的区别是处理的类型,那么如果您更仔细地观察,您会发现除了类型以外的问题(
mongoContext.Developers.
mongoContext.Users
),我不知道如何解决这两个问题!!!!您编写
私有只读字符串\u collection=null;
来发送集合的名称,如何编写如果我创建不同的集合在这段代码中,可以在MongoContext创建时设置集合(请参阅构造函数中的“字符串集合”)