Skip to content

Commit

Permalink
Resolve Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
meghasemim1999 committed Feb 25, 2024
1 parent 83c6ece commit 26d5000
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
66 changes: 32 additions & 34 deletions Source/BSN.Commons.Orm.Redis/RedisRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using Redis.OM.Searching;
using Redis.OM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Security.Cryptography;
using BSN.Commons.Infrastructure;
using Apache.NMS.ActiveMQ.Util.Synchronization;
using static Amazon.S3.Util.S3EventNotification;
using StackExchange.Redis;
using Redis.OM.Contracts;
using Redis.OM.Aggregation;
using Redis.OM.Modeling;
using Redis.OM.Searching.Query;
using BSN.Commons.Infrastructure.Redis;

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

/// <inheritdoc />
public void Add(T entity)
{
_redisCollection.Insert(entity);
Collection.Insert(entity);
}

/// <inheritdoc />
Expand All @@ -59,7 +42,7 @@ public void AddRange(IEnumerable<T> entities)
/// <inheritdoc />
public void Update(T entity)
{
_redisCollection.Update(entity);
Collection.Update(entity);
}

/// <inheritdoc />
Expand All @@ -74,7 +57,7 @@ public void UpdateRange(IEnumerable<T> entities)
foreach (var entity in entities)
{
Update(entity);
}
}
}

/// <inheritdoc />
Expand All @@ -86,27 +69,27 @@ public void UpdateRange(IEnumerable<T> entities, Action<IUpdateConfig<T>> config
/// <inheritdoc />
public void Delete(T entity)
{
_redisCollection.Delete(entity);
Collection.Delete(entity);
}

/// <inheritdoc />
public void Delete(Expression<Func<T, bool>> where)
{
DeleteRange(_redisCollection.Where(where));
DeleteRange(Collection.Where(where));
}

/// <inheritdoc />
public void DeleteRange(IEnumerable<T> entities)
{
_redisCollection.Delete(entities);
Collection.Delete(entities);
}

/// <inheritdoc />
public T GetById<KeyType>(KeyType id)
{
if (id is string str_id)
{
T? entity = _redisCollection.FindById(str_id);
T? entity = Collection.FindById(str_id);
if (entity == null)
{
throw new KeyNotFoundException($"entity with key of {id} was not found.");
Expand All @@ -121,25 +104,43 @@ public T GetById<KeyType>(KeyType id)
/// <inheritdoc />
public T Get(Expression<Func<T, bool>> where)
{
return _redisCollection.Where(where).FirstOrDefault();
return Collection.Where(where).FirstOrDefault();
}

/// <inheritdoc />
public IEnumerable<T> GetAll()
{
return _redisCollection.Where(entity => true);
return Collection.Where(entity => true);
}

/// <inheritdoc />
public IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
return _redisCollection.Where(where);
return Collection.Where(where);
}

/// <summary>
/// Redis Collection accosiated with the type of T
/// </summary>
protected readonly RedisCollection<T> _redisCollection;
public IRedisCollection<T> Collection
{
get
{
if (_redisCollection == null)
{
if (DatabaseFactory.Get() is IRedisDbContext dbContext)
{
_redisCollection = Provider.RedisCollection<T>();
}
else
{
throw new InvalidCastException("The database factory for redis must return an IRedisDbContext");
}
}

return _redisCollection;
}
}

/// <summary>
/// Redis Connection Provider to access collections
Expand All @@ -164,11 +165,8 @@ protected IRedisConnectionProvider Provider
}
}

/// <summary>
/// Redis Database Factory
/// </summary>
protected IDatabaseFactory DatabaseFactory { get; private set; }

protected IRedisConnectionProvider? _provider;
protected IRedisCollection<T>? _redisCollection;
}
}
14 changes: 7 additions & 7 deletions Test/BSN.Commons.Orm.Redis.Tests/RepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ public void SetUp()
_userRepository = CreateUserRepository(_databaseFactory);
}

[TearDown]
public void TearDown()
{
_databaseFactory.Dispose();
}

[Test]
public void AddUserToDataBaseAndNullQueue_CorrectInput_UsereShouldBeCorectlyAddedToDatabase()
public void AddUserToDataBase_UserShouldBeCorrectlyAddedToDatabase()
{
User user = new User()
{
Expand All @@ -39,12 +45,6 @@ public void AddUserToDataBaseAndNullQueue_CorrectInput_UsereShouldBeCorectlyAdde
Assert.That(_userRepository.GetMany(x => x.FirstName == "Reza" && x.LastName == "Alizadeh"), Is.Not.Empty);
}

[TearDown]
public void TearDown()
{
_databaseFactory.Dispose();
}

public IDatabaseFactory CreateDatabaseFactory()
{
var redisConnectionOptions = new RedisConnectionOptions
Expand Down

0 comments on commit 26d5000

Please sign in to comment.