mongodb

时间:2018-05-19 14:29:34

标签: mongodb generics .net-core mongorepository

我尝试实现与mongodb一起使用的通用存储库和通用上下文。 我鼓励的问题是我收到以下错误消息:

Unable to resolve service for type 'CommonLibrary.Database.GenericContext`

这是我从启动类的配置服务方法:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.Configure<Settings>(
            options =>
            {
                options.ConnectionString = Configuration.GetSection("MongoDb:ConnectionString").Value;
                options.Database = Configuration.GetSection("MongoDb:Database").Value;
            });

        services.AddTransient(typeof(IGenericContext<>), typeof(GenericContext<>));
        services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
    }

我的通用上下文类:

public class GenericContext<T> : IGenericContext<T> where T : BaseClass
{
    private readonly IMongoDatabase db;
    public GenericContext(IOptions<Settings> options)
    {
        var client = new MongoClient(options.Value.ConnectionString);
        db = client.GetDatabase(options.Value.Database);
    }

    public IMongoCollection<T> Entities => db.GetCollection<T>("Entities");
}

我的通用存储库类:

public class GenericRepository<T> : IGenericRepository<T> where T : BaseClass
{
    private readonly GenericContext<T> _context;

    public GenericRepository(GenericContext<T> context)
    {
        _context = context;
    }

    public async Task<IEnumerable<T>> GetAll()
    {
        return await _context
                        .Entities
                        .Find(_ => true)
                        .ToListAsync();
    }

    public Task<T> GetById(string customerId)
    {
        FilterDefinition<T> filter = Builders<T>.Filter.Eq(m => m.EntityId, customerId);

        return _context
                .Entities
                .Find(filter)
                .FirstOrDefaultAsync();
    }

    public async Task Create(T customer)
    {
        await _context.Entities.InsertOneAsync(customer);
    }

    public async Task<bool> Update(T customer)
    {
        ReplaceOneResult updateResult =
            await _context
                    .Entities
                    .ReplaceOneAsync(
                        filter: g => g.Id == customer.Id,
                        replacement: customer);

        return updateResult.IsAcknowledged
                && updateResult.ModifiedCount > 0;
    }

    public async Task<bool> Delete(string entityId)
    {
        FilterDefinition<T> filter = Builders<T>.Filter.Eq(m => m.EntityId, entityId);

        DeleteResult deleteResult = await _context
                                            .Entities
                                            .DeleteOneAsync(filter);

        return deleteResult.IsAcknowledged
            && deleteResult.DeletedCount > 0;
    }
}

我的基类:

public class BaseClass
{
    [BsonId]
    public ObjectId Id { get; set; }
    [BsonElement]
    public string EntityId { get; set; }
}

我的派生客户类:

public class Customer : BaseClass
{
    [BsonElement]
    public string CustomerName { get; set; }
}

您知道为什么我的通用存储库无法实例化吗?这似乎是GenericContext的一个问题,但我不知道在哪里。

谢谢

1 个答案:

答案 0 :(得分:0)

我在我的通用存储库中发现了问题,我注入了类而不是接口:

public GenericRepository(GenericContext<T> context)