From 85c76f7e8feeaa07b233eedab7303396bd50c681 Mon Sep 17 00:00:00 2001 From: Farzaneh Soltanzadeh Date: Mon, 4 Mar 2024 11:56:35 -0800 Subject: [PATCH 1/3] check for Indexed attribute existence and add dropindex --- Source/BSN.Commons.Orm.Redis/RepositoryBase.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs index 5ea0526..cd9f758 100644 --- a/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs +++ b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs @@ -6,10 +6,14 @@ 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; namespace BSN.Commons.Orm.Redis { @@ -27,9 +31,16 @@ protected RepositoryBase(IDatabaseFactory databaseFactory) { DatabaseFactory = databaseFactory; dbCollection = DataContext.RedisCollection(); - - // TODO: Check that IndexCreationService is necessary or not. - DataContext.Connection.CreateIndex(typeof(T)); + + bool hasIndexedAttribute = typeof(T).GetProperties() + .Where(pi => pi.GetCustomAttribute() != null) + .Any(); + + if (hasIndexedAttribute) + { + DataContext.Connection.DropIndex(typeof(T)); + DataContext.Connection.CreateIndex(typeof(T)); + } } /// From 6725b00e24e10c9b9cfc0f5976eca79b4b479ff3 Mon Sep 17 00:00:00 2001 From: Farzaneh Soltanzadeh Date: Mon, 4 Mar 2024 15:10:53 -0800 Subject: [PATCH 2/3] Use ConfigurationSectionName --- Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs b/Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs index 51dda91..8d7596f 100644 --- a/Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs +++ b/Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs @@ -22,7 +22,7 @@ public class DatabaseFactory : Disposable, IDatabaseFactory where TD /// App configuration public DatabaseFactory(IConfiguration configuration) { - redisConnectionOptions = Options.Create(configuration.GetSection("Redis").Get()); + redisConnectionOptions = Options.Create(configuration.GetSection(Infrastructure.Redis.RedisConnectionOptions.ConfigurationSectionName).Get()); } /// From fc367c58778041ccefdce9f3fdb703f048c392dd Mon Sep 17 00:00:00 2001 From: Farzaneh Soltanzadeh Date: Mon, 4 Mar 2024 15:18:33 -0800 Subject: [PATCH 3/3] raise exception in case of unexistence of object in GetById --- .../BSN.Commons.Orm.Redis/RepositoryBase.cs | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs index cd9f758..c53a384 100644 --- a/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs +++ b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs @@ -14,6 +14,7 @@ using StackExchange.Redis; using System.Security.Principal; using System.Reflection; +using System.Text.Json; namespace BSN.Commons.Orm.Redis { @@ -43,6 +44,27 @@ protected RepositoryBase(IDatabaseFactory databaseFactory) } } + // We can use IComparable pattern for T in IRepository but in that case all of our Domains are forced to implement Equals method. + + /// + /// Checks if preoperties of two entities are equal. + /// + /// + /// + /// + 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; + } + /// public void Add(T entity) { @@ -109,7 +131,8 @@ public T GetById(KeyType id) if (id is string str_id) { T? entity = dbCollection.FindById(str_id); - if (entity == null) + T DefaultEntity = JsonSerializer.Deserialize("{}"); + if (entity == null || Equals(DefaultEntity, entity)) { throw new KeyNotFoundException($"entity with key of {id} was not found."); } @@ -129,7 +152,7 @@ public T Get(Expression> where) /// public IEnumerable GetAll() { - return dbCollection.Where(entity => true); + return dbCollection.ToList(); } ///