Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public sealed class RepositoryQuery<TEntity> : IRepositoryQuery<TEntity> where TEntity : BaseEntity
- {
- private readonly List<Expression<Func<TEntity, object>>> _includeProperties;
- private readonly Repository<TEntity> _repository;
- private readonly List<Expression<Func<TEntity, bool>>> _filters;
- private Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> _orderByQuerable;
- private int? _page;
- private int? _pageSize;
- public RepositoryQuery(Repository<TEntity> repository)
- {
- _repository = repository;
- _includeProperties = new List<Expression<Func<TEntity, object>>>();
- _filters = new List<Expression<Func<TEntity, bool>>>();
- }
- public RepositoryQuery<TEntity> Filter(Expression<Func<TEntity, bool>> filter)
- {
- _filters.Add(filter);
- return this;
- }
- public RepositoryQuery<TEntity> OrderBy(Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy)
- {
- _orderByQuerable = orderBy;
- return this;
- }
- public RepositoryQuery<TEntity> Include(Expression<Func<TEntity, object>> expression)
- {
- _includeProperties.Add(expression);
- return this;
- }
- public IQueryable<TEntity> Get()
- {
- return _repository.Get(_filters, _orderByQuerable, _includeProperties, _page, _pageSize);
- }
- }
- public class Repository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
- {
- private readonly Guid _instanceId;
- private readonly DbSet<TEntity> _dbSet;
- private readonly IDbContext _context;
- public Repository(IDbContext context)
- {
- _context = context;
- _dbSet = context.Set<TEntity>();
- _instanceId = Guid.NewGuid();
- }
- public virtual IRepositoryQuery<TEntity> Query()
- {
- var repositoryGetFluentHelper = new RepositoryQuery<TEntity>(this);
- return repositoryGetFluentHelper;
- }
- internal IQueryable<TEntity> Get(
- List<Expression<Func<TEntity, bool>>> filters = null,
- Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
- List<Expression<Func<TEntity, object>>> includeProperties = null,
- int? page = null,
- int? pageSize = null)
- {
- IQueryable<TEntity> query = _dbSet;
- if (includeProperties != null)
- {
- includeProperties.ForEach(i => query = query.Include(i));
- }
- if (filters != null && filters.Any())
- {
- query = filters.Aggregate(query, (current, filter) => current.Where(filter));
- }
- query = orderBy != null ? orderBy(query) : query.OrderBy(a => a.Id);
- if (page != null && pageSize != null)
- {
- query = query
- .Skip((page.Value - 1)*pageSize.Value)
- .Take(pageSize.Value);
- }
- return query;
- }
- }
- var mockQuery = new Mock<IRepositoryQuery<TEntity>>();
- // perform any setup needed on mockQuery for the particular System Under Test
- var mockRepository = new Mock<IRepository<TEntity>>();
- // perform any setup needed on mockRepository for the particular System Under Test
- // component that relies on query and repository
- // that is the System Under Test i.e. the focus of the unit test
- var systemUnderTest = new SystemUnderTest(mockRepository.Object, mockQuery.Object);
- var mock = new Mock<IRepositoryQuery<SomeClass>>();
- mock.Setup(o => o.Get(/* test parameters */)).Returns(/* result */);
- var myService = new SomeService(mock.Object);
- Assert.That(myService.DoSomething(), Is.EqualTo(/* expected result*/));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement