-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathLinqRepositoryBase.cs
More file actions
151 lines (109 loc) · 5.22 KB
/
Copy pathLinqRepositoryBase.cs
File metadata and controls
151 lines (109 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SharpRepository.Repository.Caching;
using SharpRepository.Repository.FetchStrategies;
using SharpRepository.Repository.Queries;
using SharpRepository.Repository.Specifications;
namespace SharpRepository.Repository
{
public abstract class LinqRepositoryBase<T, TKey> : RepositoryBase<T, TKey> where T : class
{
protected LinqRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
{
}
public override IQueryable<T> AsQueryable()
{
return BaseQuery();
}
protected override T GetQuery(TKey key, IFetchStrategy<T> fetchStrategy)
{
return FindQuery(ByPrimaryKeySpecification(key, fetchStrategy));
}
protected override TResult GetQuery<TResult>(TKey key, IFetchStrategy<T> fetchStrategy, Expression<Func<T, TResult>> selector)
{
return FindQuery(ByPrimaryKeySpecification(key, fetchStrategy), selector);
}
protected override T FindQuery(ISpecification<T> criteria)
{
var query = BaseQuery(criteria.FetchStrategy);
SetTraceInfo("Find", query);
return criteria.SatisfyingEntityFrom(query);
}
protected TResult FindQuery<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector)
{
var query = BaseQuery(criteria.FetchStrategy);
SetTraceInfo("Find", query);
return criteria.SatisfyingEntitiesFrom(query).Select(selector).FirstOrDefault();
}
protected override T FindQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions)
{
if (queryOptions == null)
return FindQuery(criteria);
var query = queryOptions.Apply(BaseQuery(criteria.FetchStrategy));
SetTraceInfo("Find", query);
return criteria.SatisfyingEntityFrom(query);
}
protected override TResult FindQuery<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions)
{
if (queryOptions == null)
return FindQuery(criteria, selector);
var query = queryOptions.Apply(BaseQuery(criteria.FetchStrategy));
SetTraceInfo("Find", query);
return criteria.SatisfyingEntitiesFrom(query).Select(selector).FirstOrDefault();
}
protected override IQueryable<T> GetAllQuery(IFetchStrategy<T> fetchStrategy)
{
var query = BaseQuery(fetchStrategy);
SetTraceInfo("GetAll", query);
return query;
}
protected override IQueryable<T> GetAllQuery(IQueryOptions<T> queryOptions, IFetchStrategy<T> fetchStrategy)
{
if (queryOptions == null)
return GetAllQuery(fetchStrategy);
var query = BaseQuery(fetchStrategy);
query = queryOptions.Apply(query);
SetTraceInfo("GetAll", query);
return query;
}
protected override IQueryable<T> FindAllQuery(ISpecification<T> criteria)
{
var query = BaseQuery(criteria.FetchStrategy);
query = criteria.SatisfyingEntitiesFrom(query);
SetTraceInfo("FindAll", query);
return query;
}
protected override IQueryable<T> FindAllQuery(ISpecification<T> criteria, IQueryOptions<T> queryOptions)
{
if (queryOptions == null)
return FindAllQuery(criteria);
var query = BaseQuery(criteria.FetchStrategy);
query = criteria.SatisfyingEntitiesFrom(query);
query = queryOptions.Apply(query);
SetTraceInfo("FindAll", query);
return query;
}
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)
{
var innerQuery = innerRepository.AsQueryable();
var outerQuery = BaseQuery();
var innerType = innerRepository.GetType();
var outerType = GetType();
var outerKeySelectorFunc = outerKeySelector.Compile();
var innerKeySelectorFunc = innerKeySelector.Compile();
var resultSelectorFunc = resultSelector.Compile();
// if these are 2 different Repository types then let's bring down each query into memory so that they can be joined
// if they are the same type then they will use the native IQueryable and take advantage of the back-end side join if possible
if (innerType.Name != outerType.Name)
{
innerQuery = innerQuery.ToList().AsQueryable();
outerQuery = outerQuery.ToList().AsQueryable();
}
var query = outerQuery.Join(innerQuery, outerKeySelectorFunc, innerKeySelectorFunc, resultSelectorFunc).AsQueryable();
SetTraceInfo("Join", query);
return new CompositeRepository<TResult>(query);
}
}
}