Advertisement
Shuva_Dev

Repository

Jul 5th, 2024
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.16 KB | None | 0 0
  1. using Blog.Domain.Entities;
  2. using Microsoft.Data.SqlClient;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.EntityFrameworkCore.Query;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Data.Common;
  9. using System.Data.SqlTypes;
  10. using System.Globalization;
  11. using System.Linq;
  12. using System.Linq.Dynamic.Core;
  13. using System.Linq.Expressions;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17.  
  18. namespace Blog.Infrastructure.Repositories
  19. {
  20.     public abstract class Repository<TEntity, TKey>
  21.         : IRepository<TEntity, TKey> where TKey : IComparable
  22.         where TEntity : class, IEntity<TKey>
  23.     {
  24.         private DbContext _dbContext;
  25.         private DbSet<TEntity> _dbSet;
  26.  
  27.         public Repository(DbContext context)
  28.         {
  29.             _dbContext = context;
  30.             _dbSet = _dbContext.Set<TEntity>();
  31.         }
  32.  
  33.         public virtual async Task AddAsync(TEntity entity)
  34.         {
  35.             await _dbSet.AddAsync(entity);
  36.         }
  37.  
  38.         public virtual async Task RemoveAsync(TKey id)
  39.         {
  40.             var entityToDelete = _dbSet.Find(id);
  41.             await RemoveAsync(entityToDelete);
  42.         }
  43.  
  44.         public virtual async Task RemoveAsync(TEntity entityToDelete)
  45.         {
  46.             await Task.Run(() =>
  47.             {
  48.                 if (_dbContext.Entry(entityToDelete).State == EntityState.Detached)
  49.                 {
  50.                     _dbSet.Attach(entityToDelete);
  51.                 }
  52.                 _dbSet.Remove(entityToDelete);
  53.             });
  54.         }
  55.  
  56.         public virtual async Task RemoveAsync(Expression<Func<TEntity, bool>> filter)
  57.         {
  58.             await Task.Run(() =>
  59.             {
  60.                 _dbSet.RemoveRange(_dbSet.Where(filter));
  61.             });
  62.         }
  63.  
  64.         public virtual async Task EditAsync(TEntity entityToUpdate)
  65.         {
  66.             await Task.Run(() =>
  67.             {
  68.                 _dbSet.Attach(entityToUpdate);
  69.                 _dbContext.Entry(entityToUpdate).State = EntityState.Modified;
  70.             });
  71.         }
  72.  
  73.         public virtual async Task<TEntity> GetByIdAsync(TKey id)
  74.         {
  75.             return await _dbSet.FindAsync(id);
  76.         }
  77.  
  78.         public virtual async Task<int> GetCountAsync(Expression<Func<TEntity, bool>> filter = null)
  79.         {
  80.             IQueryable<TEntity> query = _dbSet;
  81.             int count;
  82.  
  83.             if (filter != null)
  84.                 count = await query.CountAsync(filter);
  85.             else
  86.                 count = await query.CountAsync();
  87.  
  88.             return count;
  89.         }
  90.  
  91.         public virtual int GetCount(Expression<Func<TEntity, bool>> filter = null)
  92.         {
  93.             IQueryable<TEntity> query = _dbSet;
  94.             int count;
  95.  
  96.             if (filter != null)
  97.                 count = query.Count(filter);
  98.             else
  99.                 count = query.Count();
  100.  
  101.             return count;
  102.         }
  103.  
  104.         public virtual async Task<IList<TEntity>> GetAsync(Expression<Func<TEntity, bool>> filter,
  105.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null)
  106.         {
  107.             IQueryable<TEntity> query = _dbSet;
  108.  
  109.             if (filter != null)
  110.             {
  111.                 query = query.Where(filter);
  112.             }
  113.  
  114.             if (include != null)
  115.                 query = include(query);
  116.  
  117.             return await query.ToListAsync();
  118.         }
  119.  
  120.         public virtual async Task<IList<TEntity>> GetAllAsync()
  121.         {
  122.             IQueryable<TEntity> query = _dbSet;
  123.             return await query.ToListAsync();
  124.         }
  125.  
  126.         public virtual async Task<(IList<TEntity> data, int total, int totalDisplay)> GetAsync(
  127.             Expression<Func<TEntity, bool>> filter = null,
  128.             Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
  129.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  130.             int pageIndex = 1,
  131.             int pageSize = 10,
  132.             bool isTrackingOff = false)
  133.         {
  134.             IQueryable<TEntity> query = _dbSet;
  135.             var total = query.Count();
  136.             var totalDisplay = query.Count();
  137.  
  138.             if (filter != null)
  139.             {
  140.                 query = query.Where(filter);
  141.                 totalDisplay = query.Count();
  142.             }
  143.  
  144.             if (include != null)
  145.                 query = include(query);
  146.  
  147.             IList<TEntity> data;
  148.  
  149.             if (orderBy != null)
  150.             {
  151.                 var result = orderBy(query).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  152.  
  153.                 if (isTrackingOff)
  154.                     data = await result.AsNoTracking().ToListAsync();
  155.                 else
  156.                     data = await result.ToListAsync();
  157.             }
  158.             else
  159.             {
  160.                 var result = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
  161.  
  162.                 if (isTrackingOff)
  163.                     data = await result.AsNoTracking().ToListAsync();
  164.                 else
  165.                     data = await result.ToListAsync();
  166.             }
  167.  
  168.             return (data, total, totalDisplay);
  169.         }
  170.  
  171.         public virtual async Task<(IList<TEntity> data, int total, int totalDisplay)> GetDynamicAsync(
  172.             Expression<Func<TEntity, bool>> filter = null,
  173.             string orderBy = null,
  174.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  175.             int pageIndex = 1,
  176.             int pageSize = 10,
  177.             bool isTrackingOff = false)
  178.         {
  179.             IQueryable<TEntity> query = _dbSet;
  180.             var total = query.Count();
  181.             var totalDisplay = query.Count();
  182.  
  183.             if (filter != null)
  184.             {
  185.                 query = query.Where(filter);
  186.                 totalDisplay = query.Count();
  187.             }
  188.  
  189.             if (include != null)
  190.                 query = include(query);
  191.  
  192.             IList<TEntity> data;
  193.  
  194.             if (orderBy != null)
  195.             {
  196.                 var result = query.OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  197.  
  198.                 if (isTrackingOff)
  199.                     data = await result.AsNoTracking().ToListAsync();
  200.                 else
  201.                     data = await result.ToListAsync();
  202.             }
  203.             else
  204.             {
  205.                 var result = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
  206.  
  207.                 if (isTrackingOff)
  208.                     data = await result.AsNoTracking().ToListAsync();
  209.                 else
  210.                     data = await result.ToListAsync();
  211.             }
  212.  
  213.             return (data, total, totalDisplay);
  214.         }
  215.  
  216.         public virtual async Task<IList<TEntity>> GetAsync(
  217.             Expression<Func<TEntity, bool>> filter = null,
  218.             Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
  219.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  220.             bool isTrackingOff = false)
  221.         {
  222.             IQueryable<TEntity> query = _dbSet;
  223.  
  224.             if (filter != null)
  225.             {
  226.                 query = query.Where(filter);
  227.             }
  228.  
  229.             if (include != null)
  230.                 query = include(query);
  231.  
  232.             if (orderBy != null)
  233.             {
  234.                 var result = orderBy(query);
  235.  
  236.                 if (isTrackingOff)
  237.                     return await result.AsNoTracking().ToListAsync();
  238.                 else
  239.                     return await result.ToListAsync();
  240.             }
  241.             else
  242.             {
  243.                 if (isTrackingOff)
  244.                     return await query.AsNoTracking().ToListAsync();
  245.                 else
  246.                     return await query.ToListAsync();
  247.             }
  248.         }
  249.  
  250.         public virtual async Task<IList<TEntity>> GetDynamicAsync(
  251.             Expression<Func<TEntity, bool>> filter = null,
  252.             string orderBy = null,
  253.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  254.             bool isTrackingOff = false)
  255.         {
  256.             IQueryable<TEntity> query = _dbSet;
  257.  
  258.             if (filter != null)
  259.             {
  260.                 query = query.Where(filter);
  261.             }
  262.  
  263.             if (include != null)
  264.                 query = include(query);
  265.  
  266.             if (orderBy != null)
  267.             {
  268.                 var result = query.OrderBy(orderBy);
  269.  
  270.                 if (isTrackingOff)
  271.                     return await result.AsNoTracking().ToListAsync();
  272.                 else
  273.                     return await result.ToListAsync();
  274.             }
  275.             else
  276.             {
  277.                 if (isTrackingOff)
  278.                     return await query.AsNoTracking().ToListAsync();
  279.                 else
  280.                     return await query.ToListAsync();
  281.             }
  282.         }
  283.  
  284.         public virtual void Add(TEntity entity)
  285.         {
  286.             _dbSet.Add(entity);
  287.         }
  288.  
  289.         public virtual void Remove(TKey id)
  290.         {
  291.             var entityToDelete = _dbSet.Find(id);
  292.             Remove(entityToDelete);
  293.         }
  294.  
  295.         public virtual void Remove(TEntity entityToDelete)
  296.         {
  297.             if (_dbContext.Entry(entityToDelete).State == EntityState.Detached)
  298.             {
  299.                 _dbSet.Attach(entityToDelete);
  300.             }
  301.             _dbSet.Remove(entityToDelete);
  302.         }
  303.  
  304.         public virtual void Remove(Expression<Func<TEntity, bool>> filter)
  305.         {
  306.             _dbSet.RemoveRange(_dbSet.Where(filter));
  307.         }
  308.  
  309.         public virtual void Edit(TEntity entityToUpdate)
  310.         {
  311.             _dbSet.Attach(entityToUpdate);
  312.             _dbContext.Entry(entityToUpdate).State = EntityState.Modified;
  313.         }
  314.  
  315.         public virtual IList<TEntity> Get(Expression<Func<TEntity, bool>> filter,
  316.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null)
  317.         {
  318.             IQueryable<TEntity> query = _dbSet;
  319.  
  320.             if (filter != null)
  321.             {
  322.                 query = query.Where(filter);
  323.             }
  324.  
  325.             if (include != null)
  326.                 query = include(query);
  327.  
  328.             return query.ToList();
  329.         }
  330.  
  331.         public virtual IList<TEntity> GetAll()
  332.         {
  333.             IQueryable<TEntity> query = _dbSet;
  334.             return query.ToList();
  335.         }
  336.  
  337.         public virtual TEntity GetById(TKey id)
  338.         {
  339.             return _dbSet.Find(id);
  340.         }
  341.  
  342.         public virtual (IList<TEntity> data, int total, int totalDisplay) Get(
  343.             Expression<Func<TEntity, bool>> filter = null,
  344.             Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
  345.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  346.             int pageIndex = 1, int pageSize = 10, bool isTrackingOff = false)
  347.         {
  348.             IQueryable<TEntity> query = _dbSet;
  349.             var total = query.Count();
  350.             var totalDisplay = query.Count();
  351.  
  352.             if (filter != null)
  353.             {
  354.                 query = query.Where(filter);
  355.                 totalDisplay = query.Count();
  356.             }
  357.  
  358.             if (include != null)
  359.                 query = include(query);
  360.  
  361.             if (orderBy != null)
  362.             {
  363.                 var result = orderBy(query).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  364.                 if (isTrackingOff)
  365.                     return (result.AsNoTracking().ToList(), total, totalDisplay);
  366.                 else
  367.                     return (result.ToList(), total, totalDisplay);
  368.             }
  369.             else
  370.             {
  371.                 var result = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
  372.                 if (isTrackingOff)
  373.                     return (result.AsNoTracking().ToList(), total, totalDisplay);
  374.                 else
  375.                     return (result.ToList(), total, totalDisplay);
  376.             }
  377.         }
  378.  
  379.         public virtual (IList<TEntity> data, int total, int totalDisplay) GetDynamic(
  380.             Expression<Func<TEntity, bool>> filter = null,
  381.             string orderBy = null,
  382.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  383.             int pageIndex = 1, int pageSize = 10, bool isTrackingOff = false)
  384.         {
  385.             IQueryable<TEntity> query = _dbSet;
  386.             var total = query.Count();
  387.             var totalDisplay = query.Count();
  388.  
  389.             if (filter != null)
  390.             {
  391.                 query = query.Where(filter);
  392.                 totalDisplay = query.Count();
  393.             }
  394.  
  395.             if (include != null)
  396.                 query = include(query);
  397.  
  398.             if (orderBy != null)
  399.             {
  400.                 var result = query.OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  401.                 if (isTrackingOff)
  402.                     return (result.AsNoTracking().ToList(), total, totalDisplay);
  403.                 else
  404.                     return (result.ToList(), total, totalDisplay);
  405.             }
  406.             else
  407.             {
  408.                 var result = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
  409.                 if (isTrackingOff)
  410.                     return (result.AsNoTracking().ToList(), total, totalDisplay);
  411.                 else
  412.                     return (result.ToList(), total, totalDisplay);
  413.             }
  414.         }
  415.  
  416.         public virtual IList<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
  417.             Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
  418.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  419.             bool isTrackingOff = false)
  420.         {
  421.             IQueryable<TEntity> query = _dbSet;
  422.  
  423.             if (filter != null)
  424.             {
  425.                 query = query.Where(filter);
  426.             }
  427.  
  428.             if (include != null)
  429.                 query = include(query);
  430.  
  431.             if (orderBy != null)
  432.             {
  433.                 var result = orderBy(query);
  434.  
  435.                 if (isTrackingOff)
  436.                     return result.AsNoTracking().ToList();
  437.                 else
  438.                     return result.ToList();
  439.             }
  440.             else
  441.             {
  442.                 if (isTrackingOff)
  443.                     return query.AsNoTracking().ToList();
  444.                 else
  445.                     return query.ToList();
  446.             }
  447.         }
  448.  
  449.         public virtual IList<TEntity> GetDynamic(Expression<Func<TEntity, bool>> filter = null,
  450.             string orderBy = null,
  451.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
  452.             bool isTrackingOff = false)
  453.         {
  454.             IQueryable<TEntity> query = _dbSet;
  455.  
  456.             if (filter != null)
  457.             {
  458.                 query = query.Where(filter);
  459.             }
  460.  
  461.             if (include != null)
  462.                 query = include(query);
  463.  
  464.             if (orderBy != null)
  465.             {
  466.                 var result = query.OrderBy(orderBy);
  467.  
  468.                 if (isTrackingOff)
  469.                     return result.AsNoTracking().ToList();
  470.                 else
  471.                     return result.ToList();
  472.             }
  473.             else
  474.             {
  475.                 if (isTrackingOff)
  476.                     return query.AsNoTracking().ToList();
  477.                 else
  478.                     return query.ToList();
  479.             }
  480.         }
  481.  
  482.         public async Task<IEnumerable<TResult>> GetAsync<TResult>(Expression<Func<TEntity, TResult>>? selector,
  483.             Expression<Func<TEntity, bool>>? predicate = null,
  484.             Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
  485.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null,
  486.             bool disableTracking = true,
  487.             CancellationToken cancellationToken = default) where TResult : class
  488.         {
  489.             var query = _dbSet.AsQueryable();
  490.             if (disableTracking) query.AsNoTracking();
  491.             if (include is not null) query = include(query);
  492.             if (predicate is not null) query = query.Where(predicate);
  493.             return orderBy is not null
  494.                 ? await orderBy(query).Select(selector!).ToListAsync(cancellationToken)
  495.                 : await query.Select(selector!).ToListAsync(cancellationToken);
  496.         }
  497.  
  498.         public async Task<TResult> SingleOrDefaultAsync<TResult>(Expression<Func<TEntity, TResult>>? selector,
  499.             Expression<Func<TEntity, bool>>? predicate = null,
  500.             Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
  501.             Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null,
  502.             bool disableTracking = true)
  503.         {
  504.             var query = _dbSet.AsQueryable();
  505.             if (disableTracking) query.AsNoTracking();
  506.             if (include is not null) query = include(query);
  507.             if (predicate is not null) query = query.Where(predicate);
  508.             return (orderBy is not null
  509.                 ? await orderBy(query).Select(selector!).FirstOrDefaultAsync()
  510.                 : await query.Select(selector!).FirstOrDefaultAsync())!;
  511.         }
  512.     }
  513. }
  514.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement