1+ using System . Linq . Expressions ;
2+ using Redis . OM . Searching ;
3+ using Redis . OM ;
4+ using BSN . Commons . Infrastructure ;
5+ using Redis . OM . Contracts ;
6+ using BSN . Commons . Infrastructure . Redis ;
7+
8+ namespace BSN . Commons . Orm . Redis
9+ {
10+ /// <summary>
11+ /// Repository Base for Redis Implementation
12+ /// </summary>
13+ /// <typeparam name="T"></typeparam>
14+ public class RedisRepositoryBase < T > : IRepository < T > where T : class
15+ {
16+ /// <summary>
17+ /// Constructor for Redis Repository Base
18+ /// </summary>
19+ /// <param name="databaseFactory">Database Factory Containing an IRedisDbContext</param>
20+ protected RedisRepositoryBase ( IDatabaseFactory databaseFactory )
21+ {
22+ DatabaseFactory = databaseFactory ;
23+ // TODO: Check that IndexCreationService is necessary or not.
24+ Provider . Connection . CreateIndex ( typeof ( T ) ) ;
25+ }
26+
27+ /// <inheritdoc />
28+ public void Add ( T entity )
29+ {
30+ Collection . Insert ( entity ) ;
31+ }
32+
33+ /// <inheritdoc />
34+ public void AddRange ( IEnumerable < T > entities )
35+ {
36+ foreach ( var entity in entities )
37+ {
38+ Add ( entity ) ;
39+ }
40+ }
41+
42+ /// <inheritdoc />
43+ public void Update ( T entity )
44+ {
45+ Collection . Update ( entity ) ;
46+ }
47+
48+ /// <inheritdoc />
49+ public void Update ( T entity , Action < IUpdateConfig < T > > configurer )
50+ {
51+ throw new NotImplementedException ( "We don't have a way to update with a configuration on redis" ) ;
52+ }
53+
54+ /// <inheritdoc />
55+ public void UpdateRange ( IEnumerable < T > entities )
56+ {
57+ foreach ( var entity in entities )
58+ {
59+ Update ( entity ) ;
60+ }
61+ }
62+
63+ /// <inheritdoc />
64+ public void UpdateRange ( IEnumerable < T > entities , Action < IUpdateConfig < T > > configurer )
65+ {
66+ throw new NotImplementedException ( "We don't have a way to update range with a configuration on redis" ) ;
67+ }
68+
69+ /// <inheritdoc />
70+ public void Delete ( T entity )
71+ {
72+ Collection . Delete ( entity ) ;
73+ }
74+
75+ /// <inheritdoc />
76+ public void Delete ( Expression < Func < T , bool > > where )
77+ {
78+ DeleteRange ( Collection . Where ( where ) ) ;
79+ }
80+
81+ /// <inheritdoc />
82+ public void DeleteRange ( IEnumerable < T > entities )
83+ {
84+ Collection . Delete ( entities ) ;
85+ }
86+
87+ /// <inheritdoc />
88+ public T GetById < KeyType > ( KeyType id )
89+ {
90+ if ( id is string str_id )
91+ {
92+ T ? entity = Collection . FindById ( str_id ) ;
93+ if ( entity == null )
94+ {
95+ throw new KeyNotFoundException ( $ "entity with key of { id } was not found.") ;
96+ }
97+
98+ return entity ;
99+ }
100+
101+ throw new NotImplementedException ( $ "KeyType of { typeof ( KeyType ) } is not supported.") ;
102+ }
103+
104+ /// <inheritdoc />
105+ public T Get ( Expression < Func < T , bool > > where )
106+ {
107+ return Collection . Where ( where ) . FirstOrDefault ( ) ;
108+ }
109+
110+ /// <inheritdoc />
111+ public IEnumerable < T > GetAll ( )
112+ {
113+ return Collection . Where ( entity => true ) ;
114+ }
115+
116+ /// <inheritdoc />
117+ public IEnumerable < T > GetMany ( Expression < Func < T , bool > > where )
118+ {
119+ return Collection . Where ( where ) ;
120+ }
121+
122+ /// <summary>
123+ /// Redis Collection accosiated with the type of T
124+ /// </summary>
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+ }
144+
145+ /// <summary>
146+ /// Redis Connection Provider to access collections
147+ /// </summary>
148+ protected IRedisConnectionProvider Provider
149+ {
150+ get
151+ {
152+ if ( _provider == null )
153+ {
154+ if ( DatabaseFactory . Get ( ) is IRedisDbContext dbContext )
155+ {
156+ _provider = dbContext . GetConnectionProvider ( ) ;
157+ }
158+ else
159+ {
160+ throw new InvalidCastException ( "The database factory for redis must return an IRedisDbContext" ) ;
161+ }
162+ }
163+
164+ return _provider ;
165+ }
166+ }
167+
168+ protected IDatabaseFactory DatabaseFactory { get ; private set ; }
169+ protected IRedisConnectionProvider ? _provider ;
170+ protected IRedisCollection < T > ? _redisCollection ;
171+ }
172+ }
0 commit comments