Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. public sealed class RepositoryQuery<TEntity> : IRepositoryQuery<TEntity> where TEntity : BaseEntity
  2. {
  3.  
  4. private readonly List<Expression<Func<TEntity, object>>> _includeProperties;
  5. private readonly Repository<TEntity> _repository;
  6. private readonly List<Expression<Func<TEntity, bool>>> _filters;
  7. private Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> _orderByQuerable;
  8. private int? _page;
  9. private int? _pageSize;
  10.  
  11. public RepositoryQuery(Repository<TEntity> repository)
  12. {
  13. _repository = repository;
  14. _includeProperties = new List<Expression<Func<TEntity, object>>>();
  15. _filters = new List<Expression<Func<TEntity, bool>>>();
  16. }
  17.  
  18. public RepositoryQuery<TEntity> Filter(Expression<Func<TEntity, bool>> filter)
  19. {
  20. _filters.Add(filter);
  21. return this;
  22. }
  23.  
  24. public RepositoryQuery<TEntity> OrderBy(Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy)
  25. {
  26. _orderByQuerable = orderBy;
  27. return this;
  28. }
  29.  
  30. public RepositoryQuery<TEntity> Include(Expression<Func<TEntity, object>> expression)
  31. {
  32. _includeProperties.Add(expression);
  33. return this;
  34. }
  35.  
  36. public IQueryable<TEntity> Get()
  37. {
  38. return _repository.Get(_filters, _orderByQuerable, _includeProperties, _page, _pageSize);
  39. }
  40.  
  41. }
  42.  
  43. public class Repository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
  44. {
  45. private readonly Guid _instanceId;
  46. private readonly DbSet<TEntity> _dbSet;
  47. private readonly IDbContext _context;
  48.  
  49. public Repository(IDbContext context)
  50. {
  51. _context = context;
  52. _dbSet = context.Set<TEntity>();
  53. _instanceId = Guid.NewGuid();
  54. }
  55.  
  56. public virtual IRepositoryQuery<TEntity> Query()
  57. {
  58. var repositoryGetFluentHelper = new RepositoryQuery<TEntity>(this);
  59. return repositoryGetFluentHelper;
  60. }
  61.  
  62. internal IQueryable<TEntity> Get(
  63. List<Expression<Func<TEntity, bool>>> filters = null,
  64. Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
  65. List<Expression<Func<TEntity, object>>> includeProperties = null,
  66. int? page = null,
  67. int? pageSize = null)
  68. {
  69. IQueryable<TEntity> query = _dbSet;
  70.  
  71. if (includeProperties != null)
  72. {
  73. includeProperties.ForEach(i => query = query.Include(i));
  74. }
  75.  
  76. if (filters != null && filters.Any())
  77. {
  78. query = filters.Aggregate(query, (current, filter) => current.Where(filter));
  79. }
  80.  
  81. query = orderBy != null ? orderBy(query) : query.OrderBy(a => a.Id);
  82.  
  83. if (page != null && pageSize != null)
  84. {
  85. query = query
  86. .Skip((page.Value - 1)*pageSize.Value)
  87. .Take(pageSize.Value);
  88. }
  89. return query;
  90. }
  91.  
  92.  
  93. }
  94.  
  95. var mockQuery = new Mock<IRepositoryQuery<TEntity>>();
  96.  
  97. // perform any setup needed on mockQuery for the particular System Under Test
  98.  
  99. var mockRepository = new Mock<IRepository<TEntity>>();
  100.  
  101. // perform any setup needed on mockRepository for the particular System Under Test
  102.  
  103. // component that relies on query and repository
  104. // that is the System Under Test i.e. the focus of the unit test
  105. var systemUnderTest = new SystemUnderTest(mockRepository.Object, mockQuery.Object);
  106.  
  107. var mock = new Mock<IRepositoryQuery<SomeClass>>();
  108. mock.Setup(o => o.Get(/* test parameters */)).Returns(/* result */);
  109.  
  110. var myService = new SomeService(mock.Object);
  111. Assert.That(myService.DoSomething(), Is.EqualTo(/* expected result*/));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement