Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create index bug. #81

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ public class DatabaseFactory<TDbContext> : Disposable, IDatabaseFactory where TD
/// <param name="configuration">App configuration</param>
public DatabaseFactory(IConfiguration configuration)
{
redisConnectionOptions = Options.Create(configuration.GetSection("Redis").Get<RedisConnectionOptions>());
redisConnectionOptions = Options.Create(configuration.GetSection(Infrastructure.Redis.RedisConnectionOptions.ConfigurationSectionName).Get<RedisConnectionOptions>());
}

/// <summary>
42 changes: 38 additions & 4 deletions Source/BSN.Commons.Orm.Redis/RepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -6,10 +6,15 @@
using Redis.OM;
using Redis.OM.Contracts;
using Redis.OM.Searching;
using RedisModeling = Redis.OM.Modeling;

using BSN.Commons.Infrastructure;
using BSN.Commons.Infrastructure.Redis;
using System.Data.Common;
using StackExchange.Redis;
using System.Security.Principal;
using System.Reflection;
using System.Text.Json;

namespace BSN.Commons.Orm.Redis
{
@@ -27,9 +32,37 @@ protected RepositoryBase(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
dbCollection = DataContext.RedisCollection<T>();

bool hasIndexedAttribute = typeof(T).GetProperties()
.Where(pi => pi.GetCustomAttribute<RedisModeling.IndexedAttribute>() != null)
.Any();

if (hasIndexedAttribute)
{
DataContext.Connection.DropIndex(typeof(T));
DataContext.Connection.CreateIndex(typeof(T));
}
}

// TODO: Check that IndexCreationService is necessary or not.
DataContext.Connection.CreateIndex(typeof(T));
// We can use IComparable pattern for T in IRepository but in that case all of our Domains are forced to implement Equals method.

/// <summary>
/// Checks if preoperties of two entities are equal.
/// </summary>
/// <param name="entity1"></param>
/// <param name="entity2"></param>
/// <returns></returns>
public bool Equals(T entity1, T entity2)
{
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
if (!Equals(property.GetValue(entity1), property.GetValue(entity2)))
{
return false;
}
}
return true;
}

/// <inheritdoc />
@@ -98,7 +131,8 @@ public T GetById<KeyType>(KeyType id)
if (id is string str_id)
{
T? entity = dbCollection.FindById(str_id);
if (entity == null)
T DefaultEntity = JsonSerializer.Deserialize<T>("{}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this logic is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As FindById(str_id) does not return null in case of inexistence of object and it returns a default object, I created a new default object ( JsonSerializer.Deserialize("{}");) and checked if two objects are equal.

if (entity == null || Equals(DefaultEntity, entity))
{
throw new KeyNotFoundException($"entity with key of {id} was not found.");
}
@@ -118,7 +152,7 @@ public T Get(Expression<Func<T, bool>> where)
/// <inheritdoc />
public IEnumerable<T> GetAll()
{
return dbCollection.Where(entity => true);
return dbCollection.ToList();
}

/// <inheritdoc />
Loading