Mongo仓储服务

  • binGe博客
  • 工具使用
  • 2024/1/24 16:38:38
  • 人已阅读
简介
using DingTalkService.NuGet.Mongo.Service;
using DingTalkService.NuGet.Mongo.Single;
using DingTalkService.NuGet.SnowFlake.Single;
using DingTalkService.Domain;
using DingTalkService.Service.IService.ICommonService;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DingTalkService.NuGet.Mongo.Expand;
using System.Linq.Expressions;
using MongoDB.Driver.Linq;

namespace DingTalkService.Service.Service.CommonService
{
    /// <summary>
    /// Mongo仓储服务
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public class MongoRepository<TEntity, TKey> : BaseService, IMongoRepository<TEntity, TKey> where TEntity : BaseEntity, new() where TKey : struct
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContextFactory"></param>
        /// <param name="snowFlake"></param>
        public MongoRepository(IMongoFactory dbContextFactory, ISnowFlake snowFlake) : base(dbContextFactory, snowFlake)
        {
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="Entity">实体对象</param>
        public async Task<TEntity> InsertAsync(TEntity Entity) 
        {
            Entity.ID = _SnowFlake.NextId();
            Entity.CreateDate = DateTime.Now;
            await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).InsertOneAsync(Entity);
            return Entity;
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="Entities">实体对象列表</param>
        public async Task<List<TEntity>> InsertManyAsync(List<TEntity> Entities)
        {
            foreach (TEntity Entity in Entities)
            {
                Entity.ID = _SnowFlake.NextId();
                Entity.CreateDate = DateTime.Now;
            }
            await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).InsertManyAsync(Entities);
            return Entities;
        }

        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="Entity">需要删除的实体</param>
        //public async Task<DeleteResult> DeleteAsync(TEntity Entity)
        //{
        //    return await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).UpdateOneAsync<TEntity>(MongoExpend.Query<TEntity>().Where(sa => sa.ID == Entity.ID).Build());
        //}


        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="Predicate">Lambda条件</param>
        //public async Task<DeleteResult> DeleteAsync(Expression<Func<TEntity, bool>> Predicate)
        //{
        //    return await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).UpdateOneAsync<TEntity>(MongoExpend.Query<TEntity>().Where(Predicate).Build());
        //}

        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="ID">ID</param>
        ///// <returns>受影响行数</returns>
        //public async Task<int> DeleteAsync(long ID)
        //{
        //    return await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).UpdateOneAsync<TEntity>(MongoExpend.Query<TEntity>().Where(sa => sa.ID == ID).Build());
        //}

        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="entitys">需要删除的实体列表</param>
        ///// <returns>受影响行数</returns>
        //public async Task<int> DeleteAsync(List<TEntity> entitys)
        //{
        //    return await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).UpdateOneAsync<TEntity>(MongoExpend.Query<TEntity>().Where(sa => sa.ID == ID).Build());
        //}

        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>受影响行数</returns>
        public async Task<long> HardDeleteAsync(TEntity Entity) => (await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).DeleteOneAsync(sa => sa.ID == Entity.ID)).DeletedCount;

        ///// <summary>
        ///// 物理删除
        ///// </summary>
        ///// <param name="Key">主键值</param>
        ///// <returns>受影响行数</returns>
        //public async Task<DeleteResult> HardDeleteAsync(TKey Key) => await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).DeleteOneAsync(sa => sa.ID == Key);

        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="Predicate">Lambda条件</param>
        /// <returns>受影响行数</returns>
        public async Task<long> HardDeleteAsync(Expression<Func<TEntity, bool>> Predicate) => (await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).DeleteManyAsync(Predicate)).DeletedCount;

        ///// <summary>
        ///// 更新
        ///// </summary>
        ///// <param name="entity">需要更新的实体</param>
        ///// <returns>受影响行数</returns>
        //public async Task<int> UpdateAsync(TEntity entity)
        //{

        //}

        ///// <summary>
        ///// 仅更新指定字段(注意:“仅”!)
        ///// </summary>
        ///// <param name="Predicate">Lambda条件</param>
        ///// <returns>受影响行数</returns>
        //public async Task<int> UpdateAsync(Expression<Func<TEntity, bool>> Predicate, Expression<Func<TEntity, TEntity>> Content)
        //{

        //}

        ///// <summary>
        ///// 更新
        ///// </summary>
        ///// <param name="entitys">需要删除的实体列表</param>
        ///// <returns>受影响行数</returns>
        //public async Task<TEntity> UpdateAsync(Expression<Func<TEntity, bool>> Predicate,TEntity Entity)
        //{
            
        //}

        /// <summary>
        /// 获取IQuery
        /// </summary>
        /// <returns>IQuery</returns>
        public IMongoQueryable<TEntity> Query() => DbContext.GetCollection<TEntity>(typeof(TEntity).Name).AsQueryable();

        /// <summary>
        /// 获取一个实体
        /// </summary>
        /// <param name="Predicate"></param>
        /// <returns></returns>
        public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> Predicate) => await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).AsQueryable().Where(Predicate).FirstOrDefaultAsync();

        /// <summary>
        /// 获取实体列表
        /// </summary>
        /// <param name="Predicate"></param>
        /// <returns></returns>
        public async Task<List<TEntity>> ToListAsync(Expression<Func<TEntity, bool>> Predicate) => await DbContext.GetCollection<TEntity>(typeof(TEntity).Name).AsQueryable().Where(Predicate).ToListAsync();

    }
}

 

文章评论

评论
  • 消灭零回复
Top