|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Linq.Expressions; |
5 | | -using SharpRepository.Repository.Caching; |
6 | | -using SharpRepository.Repository.FetchStrategies; |
7 | | -using SharpRepository.Repository.Queries; |
8 | | -using SharpRepository.Repository.Specifications; |
9 | | - |
10 | | -namespace SharpRepository.Repository |
11 | | -{ |
12 | | - public abstract class LinqRepositoryBase<T, TKey> : RepositoryBase<T, TKey> where T : class |
13 | | - { |
14 | | - protected LinqRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy) |
15 | | - { |
16 | | - } |
17 | | - |
18 | | - public override IQueryable<T> AsQueryable() |
19 | | - { |
20 | | - return BaseQuery(); |
21 | | - } |
22 | | - |
23 | | - protected abstract IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null); |
24 | | - |
25 | | - protected override T GetQuery(TKey key) |
26 | | - { |
27 | | - return FindQuery(ByPrimaryKeySpecification(key)); |
28 | | - } |
29 | | - |
30 | | - protected override T FindQuery(ISpecification<T> criteria) |
31 | | - { |
32 | | - return criteria.SatisfyingEntityFrom(BaseQuery()); |
33 | | - } |
34 | | - |
35 | | - protected override T FindQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions) |
36 | | - { |
37 | | - if (queryOptions == null) |
38 | | - return FindQuery(criteria); |
39 | | - |
40 | | - var query = queryOptions.Apply(BaseQuery()); |
41 | | - |
42 | | - return criteria.SatisfyingEntityFrom(query); |
43 | | - } |
44 | | - |
45 | | - protected override IEnumerable<T> GetAllQuery() |
46 | | - { |
47 | | - return BaseQuery().ToList(); |
48 | | - } |
49 | | - |
50 | | - protected override IEnumerable<T> GetAllQuery(IQueryOptions<T> queryOptions) |
51 | | - { |
52 | | - if (queryOptions == null) |
53 | | - return GetAllQuery(); |
54 | | - |
55 | | - var query = BaseQuery(); |
56 | | - |
57 | | - return queryOptions.Apply(query).ToList(); |
58 | | - } |
59 | | - |
60 | | - protected override IEnumerable<T> FindAllQuery(ISpecification<T> criteria) |
61 | | - { |
62 | | - var query = BaseQuery(criteria.FetchStrategy); |
63 | | - return criteria.SatisfyingEntitiesFrom(query).ToList(); |
64 | | - } |
65 | | - |
66 | | - protected override IEnumerable<T> FindAllQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions) |
67 | | - { |
68 | | - if (queryOptions == null) |
69 | | - return FindAllQuery(criteria); |
70 | | - |
71 | | - var query = BaseQuery(criteria.FetchStrategy); |
72 | | - |
73 | | - query = criteria.SatisfyingEntitiesFrom(query); |
74 | | - |
75 | | - return queryOptions.Apply(query).ToList(); |
76 | | - } |
77 | | - |
78 | | - public override IEnumerator<T> GetEnumerator() |
79 | | - { |
80 | | - return BaseQuery().GetEnumerator(); |
81 | | - } |
82 | | - |
83 | | - public override IRepositoryQueryable<TResult> Join<TJoinKey, TInner, TResult>(IRepositoryQueryable<TInner> innerRepository, Expression<Func<T, TJoinKey>> outerKeySelector, Expression<Func<TInner, TJoinKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector) |
84 | | - { |
85 | | - var innerQuery = innerRepository.AsQueryable(); |
86 | | - var outerQuery = BaseQuery(); |
87 | | - |
88 | | - var innerType = innerRepository.GetType(); |
89 | | - var outerType = GetType(); |
90 | | - |
91 | | - // if these are 2 different Repository types then let's bring down each query into memory so that they can be joined |
92 | | - // if they are the same type then they will use the native IQueryable and take advantage of the back-end side join if possible |
93 | | - if (innerType.Name != outerType.Name) |
94 | | - { |
95 | | - innerQuery = innerQuery.ToList().AsQueryable(); |
96 | | - outerQuery = outerQuery.ToList().AsQueryable(); |
97 | | - return new CompositeRepository<TResult>(outerQuery.Join(innerQuery, outerKeySelector, innerKeySelector, resultSelector)); |
98 | | - } |
99 | | - |
100 | | - return new CompositeRepository<TResult>(outerQuery.Join(innerQuery, outerKeySelector, innerKeySelector, resultSelector)); |
101 | | - } |
102 | | - } |
103 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using SharpRepository.Repository.Caching; |
| 6 | +using SharpRepository.Repository.FetchStrategies; |
| 7 | +using SharpRepository.Repository.Queries; |
| 8 | +using SharpRepository.Repository.Specifications; |
| 9 | + |
| 10 | +namespace SharpRepository.Repository |
| 11 | +{ |
| 12 | + public abstract class LinqRepositoryBase<T, TKey> : RepositoryBase<T, TKey> where T : class |
| 13 | + { |
| 14 | + protected LinqRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy) |
| 15 | + { |
| 16 | + } |
| 17 | + |
| 18 | + public override IQueryable<T> AsQueryable() |
| 19 | + { |
| 20 | + return BaseQuery(); |
| 21 | + } |
| 22 | + |
| 23 | + protected abstract IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null); |
| 24 | + |
| 25 | + protected override T GetQuery(TKey key) |
| 26 | + { |
| 27 | + return FindQuery(ByPrimaryKeySpecification(key)); |
| 28 | + } |
| 29 | + |
| 30 | + protected override T FindQuery(ISpecification<T> criteria) |
| 31 | + { |
| 32 | + return criteria.SatisfyingEntityFrom(BaseQuery()); |
| 33 | + } |
| 34 | + |
| 35 | + protected override T FindQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions) |
| 36 | + { |
| 37 | + if (queryOptions == null) |
| 38 | + return FindQuery(criteria); |
| 39 | + |
| 40 | + var query = queryOptions.Apply(BaseQuery()); |
| 41 | + |
| 42 | + return criteria.SatisfyingEntityFrom(query); |
| 43 | + } |
| 44 | + |
| 45 | + protected override IQueryable<T> GetAllQuery() |
| 46 | + { |
| 47 | + return BaseQuery(); |
| 48 | + } |
| 49 | + |
| 50 | + protected override IQueryable<T> GetAllQuery(IQueryOptions<T> queryOptions) |
| 51 | + { |
| 52 | + if (queryOptions == null) |
| 53 | + return GetAllQuery(); |
| 54 | + |
| 55 | + var query = BaseQuery(); |
| 56 | + |
| 57 | + return queryOptions.Apply(query); |
| 58 | + } |
| 59 | + |
| 60 | + protected override IQueryable<T> FindAllQuery(ISpecification<T> criteria) |
| 61 | + { |
| 62 | + var query = BaseQuery(criteria.FetchStrategy); |
| 63 | + return criteria.SatisfyingEntitiesFrom(query); |
| 64 | + } |
| 65 | + |
| 66 | + protected override IQueryable<T> FindAllQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions) |
| 67 | + { |
| 68 | + if (queryOptions == null) |
| 69 | + return FindAllQuery(criteria); |
| 70 | + |
| 71 | + var query = BaseQuery(criteria.FetchStrategy); |
| 72 | + |
| 73 | + query = criteria.SatisfyingEntitiesFrom(query); |
| 74 | + |
| 75 | + return queryOptions.Apply(query); |
| 76 | + } |
| 77 | + |
| 78 | + public override IEnumerator<T> GetEnumerator() |
| 79 | + { |
| 80 | + return BaseQuery().GetEnumerator(); |
| 81 | + } |
| 82 | + |
| 83 | + public override IRepositoryQueryable<TResult> Join<TJoinKey, TInner, TResult>(IRepositoryQueryable<TInner> innerRepository, Expression<Func<T, TJoinKey>> outerKeySelector, Expression<Func<TInner, TJoinKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector) |
| 84 | + { |
| 85 | + var innerQuery = innerRepository.AsQueryable(); |
| 86 | + var outerQuery = BaseQuery(); |
| 87 | + |
| 88 | + var innerType = innerRepository.GetType(); |
| 89 | + var outerType = GetType(); |
| 90 | + |
| 91 | + // if these are 2 different Repository types then let's bring down each query into memory so that they can be joined |
| 92 | + // if they are the same type then they will use the native IQueryable and take advantage of the back-end side join if possible |
| 93 | + if (innerType.Name != outerType.Name) |
| 94 | + { |
| 95 | + innerQuery = innerQuery.ToList().AsQueryable(); |
| 96 | + outerQuery = outerQuery.ToList().AsQueryable(); |
| 97 | + return new CompositeRepository<TResult>(outerQuery.Join(innerQuery, outerKeySelector, innerKeySelector, resultSelector)); |
| 98 | + } |
| 99 | + |
| 100 | + return new CompositeRepository<TResult>(outerQuery.Join(innerQuery, outerKeySelector, innerKeySelector, resultSelector)); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments