【问题标题】:How to get around duplicate code Mongo/Core API如何绕过重复代码 Mongodb/Core API
【发布时间】:2019-10-19 19:03:04
【问题描述】:

我有两个实体

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;
        }
    }
}

现在我遇到了重复代码的问题

用户实现

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;
        }

    }

在这个例子中,我对不同的模型有相同的实现。使用相同的代码是不好的方式(现在我只有两个模型,但如果我拿 10 个模型),我不知道如何修复,如何创建正确的代码而不重复? (模型应保存在一个集合中)

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

【问题讨论】:

  • 如果唯一的区别是处理的类型,你可以选择Generics
  • 如果你仔细看,你注意到除了类型(mongoContext.Developers.mongoContext.Users)之外的问题,我不知道如何解决这两个问题!!!!

标签: c# mongodb core core-api


【解决方案1】:

你可以这样做。

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;
        }

    }
}

【讨论】:

  • 你写private readonly string _collection = null;发送集合的名字,如果我创建不同的集合怎么写
  • 在此代码中,可以在创建 MongoContext 时设置集合(参见构造函数中的“字符串集合”)
猜你喜欢
  • 2018-12-05
  • 1970-01-01
  • 2019-06-20
  • 2015-07-11
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 2019-06-04
相关资源
最近更新 更多