Skip to content

Commit 26d5000

Browse files
Resolve Comments
1 parent 83c6ece commit 26d5000

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

Source/BSN.Commons.Orm.Redis/RedisRepositoryBase.cs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
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;
72
using Redis.OM.Searching;
83
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;
144
using BSN.Commons.Infrastructure;
15-
using Apache.NMS.ActiveMQ.Util.Synchronization;
16-
using static Amazon.S3.Util.S3EventNotification;
17-
using StackExchange.Redis;
185
using Redis.OM.Contracts;
19-
using Redis.OM.Aggregation;
20-
using Redis.OM.Modeling;
21-
using Redis.OM.Searching.Query;
226
using BSN.Commons.Infrastructure.Redis;
237

248
namespace BSN.Commons.Orm.Redis
@@ -36,15 +20,14 @@ public class RedisRepositoryBase<T> : IRepository<T> where T : class
3620
protected RedisRepositoryBase(IDatabaseFactory databaseFactory)
3721
{
3822
DatabaseFactory = databaseFactory;
39-
_redisCollection = (RedisCollection<T>)Provider.RedisCollection<T>();
4023
// TODO: Check that IndexCreationService is necessary or not.
4124
Provider.Connection.CreateIndex(typeof(T));
4225
}
4326

4427
/// <inheritdoc />
4528
public void Add(T entity)
4629
{
47-
_redisCollection.Insert(entity);
30+
Collection.Insert(entity);
4831
}
4932

5033
/// <inheritdoc />
@@ -59,7 +42,7 @@ public void AddRange(IEnumerable<T> entities)
5942
/// <inheritdoc />
6043
public void Update(T entity)
6144
{
62-
_redisCollection.Update(entity);
45+
Collection.Update(entity);
6346
}
6447

6548
/// <inheritdoc />
@@ -74,7 +57,7 @@ public void UpdateRange(IEnumerable<T> entities)
7457
foreach (var entity in entities)
7558
{
7659
Update(entity);
77-
}
60+
}
7861
}
7962

8063
/// <inheritdoc />
@@ -86,27 +69,27 @@ public void UpdateRange(IEnumerable<T> entities, Action<IUpdateConfig<T>> config
8669
/// <inheritdoc />
8770
public void Delete(T entity)
8871
{
89-
_redisCollection.Delete(entity);
72+
Collection.Delete(entity);
9073
}
9174

9275
/// <inheritdoc />
9376
public void Delete(Expression<Func<T, bool>> where)
9477
{
95-
DeleteRange(_redisCollection.Where(where));
78+
DeleteRange(Collection.Where(where));
9679
}
9780

9881
/// <inheritdoc />
9982
public void DeleteRange(IEnumerable<T> entities)
10083
{
101-
_redisCollection.Delete(entities);
84+
Collection.Delete(entities);
10285
}
10386

10487
/// <inheritdoc />
10588
public T GetById<KeyType>(KeyType id)
10689
{
10790
if (id is string str_id)
10891
{
109-
T? entity = _redisCollection.FindById(str_id);
92+
T? entity = Collection.FindById(str_id);
11093
if (entity == null)
11194
{
11295
throw new KeyNotFoundException($"entity with key of {id} was not found.");
@@ -121,25 +104,43 @@ public T GetById<KeyType>(KeyType id)
121104
/// <inheritdoc />
122105
public T Get(Expression<Func<T, bool>> where)
123106
{
124-
return _redisCollection.Where(where).FirstOrDefault();
107+
return Collection.Where(where).FirstOrDefault();
125108
}
126109

127110
/// <inheritdoc />
128111
public IEnumerable<T> GetAll()
129112
{
130-
return _redisCollection.Where(entity => true);
113+
return Collection.Where(entity => true);
131114
}
132115

133116
/// <inheritdoc />
134117
public IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
135118
{
136-
return _redisCollection.Where(where);
119+
return Collection.Where(where);
137120
}
138-
121+
139122
/// <summary>
140123
/// Redis Collection accosiated with the type of T
141124
/// </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+
}
143144

144145
/// <summary>
145146
/// Redis Connection Provider to access collections
@@ -164,11 +165,8 @@ protected IRedisConnectionProvider Provider
164165
}
165166
}
166167

167-
/// <summary>
168-
/// Redis Database Factory
169-
/// </summary>
170168
protected IDatabaseFactory DatabaseFactory { get; private set; }
171-
172169
protected IRedisConnectionProvider? _provider;
170+
protected IRedisCollection<T>? _redisCollection;
173171
}
174172
}

Test/BSN.Commons.Orm.Redis.Tests/RepositoryTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ public void SetUp()
2222
_userRepository = CreateUserRepository(_databaseFactory);
2323
}
2424

25+
[TearDown]
26+
public void TearDown()
27+
{
28+
_databaseFactory.Dispose();
29+
}
30+
2531
[Test]
26-
public void AddUserToDataBaseAndNullQueue_CorrectInput_UsereShouldBeCorectlyAddedToDatabase()
32+
public void AddUserToDataBase_UserShouldBeCorrectlyAddedToDatabase()
2733
{
2834
User user = new User()
2935
{
@@ -39,12 +45,6 @@ public void AddUserToDataBaseAndNullQueue_CorrectInput_UsereShouldBeCorectlyAdde
3945
Assert.That(_userRepository.GetMany(x => x.FirstName == "Reza" && x.LastName == "Alizadeh"), Is.Not.Empty);
4046
}
4147

42-
[TearDown]
43-
public void TearDown()
44-
{
45-
_databaseFactory.Dispose();
46-
}
47-
4848
public IDatabaseFactory CreateDatabaseFactory()
4949
{
5050
var redisConnectionOptions = new RedisConnectionOptions

0 commit comments

Comments
 (0)