1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Linq . Expressions ;
5
- using System . Text ;
6
- using System . Threading . Tasks ;
1
+ using System . Linq . Expressions ;
7
2
using Redis . OM . Searching ;
8
3
using Redis . OM ;
9
- using System ;
10
- using System . Collections . Generic ;
11
- using System . Linq ;
12
- using System . Threading . Tasks ;
13
- using System . Security . Cryptography ;
14
4
using BSN . Commons . Infrastructure ;
15
- using Apache . NMS . ActiveMQ . Util . Synchronization ;
16
- using static Amazon . S3 . Util . S3EventNotification ;
17
- using StackExchange . Redis ;
18
5
using Redis . OM . Contracts ;
19
- using Redis . OM . Aggregation ;
20
- using Redis . OM . Modeling ;
21
- using Redis . OM . Searching . Query ;
22
6
using BSN . Commons . Infrastructure . Redis ;
23
7
24
8
namespace BSN . Commons . Orm . Redis
@@ -36,15 +20,14 @@ public class RedisRepositoryBase<T> : IRepository<T> where T : class
36
20
protected RedisRepositoryBase ( IDatabaseFactory databaseFactory )
37
21
{
38
22
DatabaseFactory = databaseFactory ;
39
- _redisCollection = ( RedisCollection < T > ) Provider . RedisCollection < T > ( ) ;
40
23
// TODO: Check that IndexCreationService is necessary or not.
41
24
Provider . Connection . CreateIndex ( typeof ( T ) ) ;
42
25
}
43
26
44
27
/// <inheritdoc />
45
28
public void Add ( T entity )
46
29
{
47
- _redisCollection . Insert ( entity ) ;
30
+ Collection . Insert ( entity ) ;
48
31
}
49
32
50
33
/// <inheritdoc />
@@ -59,7 +42,7 @@ public void AddRange(IEnumerable<T> entities)
59
42
/// <inheritdoc />
60
43
public void Update ( T entity )
61
44
{
62
- _redisCollection . Update ( entity ) ;
45
+ Collection . Update ( entity ) ;
63
46
}
64
47
65
48
/// <inheritdoc />
@@ -74,7 +57,7 @@ public void UpdateRange(IEnumerable<T> entities)
74
57
foreach ( var entity in entities )
75
58
{
76
59
Update ( entity ) ;
77
- }
60
+ }
78
61
}
79
62
80
63
/// <inheritdoc />
@@ -86,27 +69,27 @@ public void UpdateRange(IEnumerable<T> entities, Action<IUpdateConfig<T>> config
86
69
/// <inheritdoc />
87
70
public void Delete ( T entity )
88
71
{
89
- _redisCollection . Delete ( entity ) ;
72
+ Collection . Delete ( entity ) ;
90
73
}
91
74
92
75
/// <inheritdoc />
93
76
public void Delete ( Expression < Func < T , bool > > where )
94
77
{
95
- DeleteRange ( _redisCollection . Where ( where ) ) ;
78
+ DeleteRange ( Collection . Where ( where ) ) ;
96
79
}
97
80
98
81
/// <inheritdoc />
99
82
public void DeleteRange ( IEnumerable < T > entities )
100
83
{
101
- _redisCollection . Delete ( entities ) ;
84
+ Collection . Delete ( entities ) ;
102
85
}
103
86
104
87
/// <inheritdoc />
105
88
public T GetById < KeyType > ( KeyType id )
106
89
{
107
90
if ( id is string str_id )
108
91
{
109
- T ? entity = _redisCollection . FindById ( str_id ) ;
92
+ T ? entity = Collection . FindById ( str_id ) ;
110
93
if ( entity == null )
111
94
{
112
95
throw new KeyNotFoundException ( $ "entity with key of { id } was not found.") ;
@@ -121,25 +104,43 @@ public T GetById<KeyType>(KeyType id)
121
104
/// <inheritdoc />
122
105
public T Get ( Expression < Func < T , bool > > where )
123
106
{
124
- return _redisCollection . Where ( where ) . FirstOrDefault ( ) ;
107
+ return Collection . Where ( where ) . FirstOrDefault ( ) ;
125
108
}
126
109
127
110
/// <inheritdoc />
128
111
public IEnumerable < T > GetAll ( )
129
112
{
130
- return _redisCollection . Where ( entity => true ) ;
113
+ return Collection . Where ( entity => true ) ;
131
114
}
132
115
133
116
/// <inheritdoc />
134
117
public IEnumerable < T > GetMany ( Expression < Func < T , bool > > where )
135
118
{
136
- return _redisCollection . Where ( where ) ;
119
+ return Collection . Where ( where ) ;
137
120
}
138
-
121
+
139
122
/// <summary>
140
123
/// Redis Collection accosiated with the type of T
141
124
/// </summary>
142
- protected readonly RedisCollection < T > _redisCollection ;
125
+ public IRedisCollection < T > Collection
126
+ {
127
+ get
128
+ {
129
+ if ( _redisCollection == null )
130
+ {
131
+ if ( DatabaseFactory . Get ( ) is IRedisDbContext dbContext )
132
+ {
133
+ _redisCollection = Provider . RedisCollection < T > ( ) ;
134
+ }
135
+ else
136
+ {
137
+ throw new InvalidCastException ( "The database factory for redis must return an IRedisDbContext" ) ;
138
+ }
139
+ }
140
+
141
+ return _redisCollection ;
142
+ }
143
+ }
143
144
144
145
/// <summary>
145
146
/// Redis Connection Provider to access collections
@@ -164,11 +165,8 @@ protected IRedisConnectionProvider Provider
164
165
}
165
166
}
166
167
167
- /// <summary>
168
- /// Redis Database Factory
169
- /// </summary>
170
168
protected IDatabaseFactory DatabaseFactory { get ; private set ; }
171
-
172
169
protected IRedisConnectionProvider ? _provider ;
170
+ protected IRedisCollection < T > ? _redisCollection ;
173
171
}
174
172
}
0 commit comments