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()); } /// diff --git a/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs index 5ea0526..c53a384 100644 --- a/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs +++ b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs @@ -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(); + + bool hasIndexedAttribute = typeof(T).GetProperties() + .Where(pi => pi.GetCustomAttribute() != 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. + + /// + /// 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; } /// @@ -98,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."); } @@ -118,7 +152,7 @@ public T Get(Expression> where) /// public IEnumerable GetAll() { - return dbCollection.Where(entity => true); + return dbCollection.ToList(); } ///