using MadRunFabric.Common.Options;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// 目前非关系型数据库mongodb用的此
/// </summary>
namespace MadRunFabric.Common.DbContext
{
    public class BaseContext
    {
        private readonly IMongoDatabase _database = null;

        public BaseContext(IOptions<MongodbOptions> settings)
        {
            var client = new MongoClient(settings.Value.ConnectionString);
            if (client != null)
                _database = client.GetDatabase(settings.Value.Database);
        }

        /// <summary>
        /// 根据T获取Collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IMongoCollection<T> GetCollection<T>() where T : class
        {
            return _database.GetCollection<T>(typeof(T).Name.ToLower());
        }
        /// <summary>
        /// 删除集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void DropCollection<T>() where T : class
        {
            _database.DropCollection(typeof(T).Name.ToLower());
        }
    }
}
