From d2677f2b6832b77d664e83b832c9a5fe037e3466 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Sun, 25 Feb 2024 21:11:24 +0330 Subject: [PATCH 1/3] Update version --- Build/BSN.Commons.Orm.EntityFramework.nuspec | 4 +- Build/BSN.Commons.Users.nuspec | 4 +- Build/build.cake | 25 ++++--- GitVersion.yml | 2 +- README.md | 3 +- .../ModelBuilderExtensions.cs | 12 +-- .../BSN.Commons.Orm.Redis.csproj | 75 ++++++++++++++++++- Source/BSN.Commons/CHANGELOG.md | 6 ++ .../Infrastructure/Redis/RedisDbContext.cs | 2 +- .../Data/UnitTestContext.cs | 34 +++++++++ .../Infrastructure/DatabaseFactory.cs | 45 +++++++++++ 11 files changed, 184 insertions(+), 28 deletions(-) create mode 100644 Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs create mode 100644 Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs diff --git a/Build/BSN.Commons.Orm.EntityFramework.nuspec b/Build/BSN.Commons.Orm.EntityFramework.nuspec index b4a0eee..ab6429f 100644 --- a/Build/BSN.Commons.Orm.EntityFramework.nuspec +++ b/Build/BSN.Commons.Orm.EntityFramework.nuspec @@ -2,7 +2,7 @@ BSN.Commons.Orm.EntityFramework - 1.14.0 + 1.15.0 Seyyed Soroosh Hosseinalipour sorosh_sabz, BSN MIT @@ -16,7 +16,7 @@ BSN Co 2019-2024 Commons ORM Enterprise - + diff --git a/Build/BSN.Commons.Users.nuspec b/Build/BSN.Commons.Users.nuspec index 825d55f..9fd82ac 100644 --- a/Build/BSN.Commons.Users.nuspec +++ b/Build/BSN.Commons.Users.nuspec @@ -2,7 +2,7 @@ BSN.Commons.Users - 1.14.0 + 1.15.0 Seyyed Soroosh Hosseinalipour sorosh_sabz, BSN MIT @@ -16,7 +16,7 @@ BSN Co 2019-2024 Commons Users Enterprise - + diff --git a/Build/build.cake b/Build/build.cake index e489db7..149665a 100644 --- a/Build/build.cake +++ b/Build/build.cake @@ -16,14 +16,15 @@ var artifactsDir = "./artifacts/"; var solutionPath = "../BSN.Commons.sln"; var projectName = "BSN.Commons"; var projectFolder = "../Source/"; -var solutionVersion = "1.14.0"; +var solutionVersion = "1.15.0"; var projects = new List<(string path, string name, string version)> { ("BSN.Commons/", "BSN.Commons.csproj", solutionVersion), ("BSN.Commons.Users/", "BSN.Commons.Users.csproj", solutionVersion), ("BSN.Commons.PresentationInfrastructure/", "BSN.Commons.PresentationInfrastructure.csproj", solutionVersion), ("BSN.Commons.Orm.EntityFramework/", "BSN.Commons.Orm.EntityFramework.csproj", solutionVersion), - ("BSN.Commons.Orm.EntityFrameworkCore/", "BSN.Commons.Orm.EntityFrameworkCore.csproj", solutionVersion) + ("BSN.Commons.Orm.EntityFrameworkCore/", "BSN.Commons.Orm.EntityFrameworkCore.csproj", solutionVersion), + ("BSN.Commons.Orm.Redis/", "BSN.Commons.Orm.Redis.csproj", solutionVersion) }; var mainProject = "../Source/BSN.Commons/BSN.Commons.csproj"; @@ -33,7 +34,8 @@ var testProjects = new List<(string path, string name, string dll)> { ("BSN.Commons.Tests/", "BSN.Commons.Tests.csproj", "bin/Release/net472/BSN.Commons.Tests.dll"), ("BSN.Commons.Orm.EntityFramework.Tests/", "BSN.Commons.Orm.EntityFramework.Tests.csproj", "bin/Release/net48/BSN.Commons.Orm.EntityFramework.Tests.dll"), - ("BSN.Commons.Orm.EntityFrameworkCore.Tests/", "BSN.Commons.Orm.EntityFrameworkCore.Tests.csproj", "bin/Release/netcoreapp3.1/BSN.Commons.Orm.EntityFrameworkCore.Tests.dll") + ("BSN.Commons.Orm.EntityFrameworkCore.Tests/", "BSN.Commons.Orm.EntityFrameworkCore.Tests.csproj", "bin/Release/netcoreapp3.1/BSN.Commons.Orm.EntityFrameworkCore.Tests.dll"), + ("BSN.Commons.Orm.Redis.Tests/", "BSN.Commons.Orm.Redis.Tests.csproj", "bin/Release/net8.0/BSN.Commons.Orm.Redis.Tests.dll") }; var coverageResultsFileName = "coverage.xml"; var testResultsFileName = "nunitResults.xml"; @@ -51,7 +53,7 @@ Task("Clean") if (DirectoryExists(artifactsDir)) { DeleteDirectory( - artifactsDir, + artifactsDir, new DeleteDirectorySettings { Recursive = true, Force = true @@ -92,7 +94,7 @@ Task("Build") .Does(() => { DotNetBuild( solutionPath, - new DotNetBuildSettings + new DotNetBuildSettings { Configuration = configuration } @@ -117,7 +119,7 @@ Task("Test") CoverletOutputDirectory = Directory(artifactsDir), CoverletOutputName = specificCoverageResultsFileName }; - + DotNetTest(testFolder + testProject.path + testProject.name, settings, coverletSettings); @@ -152,7 +154,7 @@ Task("Package") { string pureName = project.name.Remove(project.name.IndexOf(".csproj")); var nuGetPackSettings = new NuGetPackSettings - { + { BasePath = projectFolder + project.path + "bin/" + Directory(configuration), OutputDirectory = artifactsDir, ArgumentCustomization = args => args.Append("-Prop Configuration=" + configuration), @@ -185,16 +187,16 @@ Task("Package") Task("Publish") .IsDependentOn("Package") .Does(() => { - var pushSettings = new DotNetNuGetPushSettings + var pushSettings = new DotNetNuGetPushSettings { Source = nugetSource, ApiKey = nugetApiKey }; var pkgs = GetFiles(artifactsDir + "*.nupkg"); - foreach(var pkg in pkgs) + foreach(var pkg in pkgs) { - if(!IsNuGetPublished(pkg)) + if(!IsNuGetPublished(pkg)) { Information($"Publishing \"{pkg}\"."); DotNetNuGetPush(pkg.FullPath, pushSettings); @@ -202,7 +204,6 @@ Task("Publish") else { Information($"Bypassing publishing \"{pkg}\" as it is already published."); } - } }); @@ -211,7 +212,7 @@ private bool IsNuGetPublished(FilePath packagePath) { var latestPublishedVersions = NuGetList( package.NuspecReader.GetId(), - new NuGetListSettings + new NuGetListSettings { Prerelease = true } diff --git a/GitVersion.yml b/GitVersion.yml index e6e8bb1..95c630d 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -1,5 +1,5 @@ mode: ContinuousDelivery -next-version: 1.14.0 +next-version: 1.15.0 branches: {} ignore: sha: [] diff --git a/README.md b/README.md index f54ca9c..58330d3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # BSN.Commons + ITNOA | | | @@ -7,7 +8,7 @@ ITNOA | **.Net Core Build** | [![.NET Core](https://github.com/BSVN/Commons/workflows/.NET%20Core/badge.svg)](https://github.com/BSVN/Commons/actions?query=workflow%3A%22.NET+Core%22) | | **OpenCover** | [![Coverage Status](https://coveralls.io/repos/github/BSVN/Commons/badge.svg?branch=master)](https://coveralls.io/github/BSVN/Commons?branch=master) | | **NuGet** | [![nuget](https://img.shields.io/nuget/v/BSN.Commons.svg)](https://www.nuget.org/packages/BSN.Commons/) - + [![Build history](https://buildstats.info/appveyor/chart/BSVN/commons)](https://ci.appveyor.com/project/BSVN/commons/history) Commons library diff --git a/Source/BSN.Commons.Orm.EntityFrameworkCore/ModelBuilderExtensions.cs b/Source/BSN.Commons.Orm.EntityFrameworkCore/ModelBuilderExtensions.cs index 7a0be15..b634154 100644 --- a/Source/BSN.Commons.Orm.EntityFrameworkCore/ModelBuilderExtensions.cs +++ b/Source/BSN.Commons.Orm.EntityFrameworkCore/ModelBuilderExtensions.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using System.Text; namespace BSN.Commons.Orm.EntityFrameworkCore @@ -69,20 +69,20 @@ public static class ModelBuilderExtensions var navigations = entityType.GetNavigations().Select(n => n.Name); IEnumerable properties = from property in typeof(T).GetProperties() where property.CanWrite == false - && property.GetCustomAttribute() == null - && property.GetMethod.GetCustomAttribute() != null + && property.GetCustomAttribute() == null + && property.GetMethod.GetCustomAttribute() != null && !ignores.Any(ignoreProperty => ignoreProperty == property.Name) && !navigations.Contains(property.Name) select property; - + // about following condition in above code: // && property.GetMethod.GetCustomAttribute() != null // getter-only properties and expression-bodied properties are looking so much similar in C# // 1. public string FullName => $"{FirstName} {LastName}" // 2. public string FirstName { get; } // in example #1 there will be no backing-field in compilation process. - // but in the next example (#2) we have a compiler generated backing-field. - // By default EF marks a property as column if it be able to write on it. + // but in the next example (#2) we have a compiler generated backing-field. + // By default EF marks a property as column if it be able to write on it. // So when we add read-only things to it, we should care about such a case. // to identify expression-bodied properties we can use this attribute check on GetMethod. diff --git a/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj b/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj index 44a8d60..fc072ff 100644 --- a/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj +++ b/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj @@ -1,11 +1,73 @@  - net6.0 - enable - enable + net6.0;net8.0 + 1.15.0 + 1.15.0 + BSN Developers + BSN Company + ORM Helpers for using redis-om in enterprise application + BSN Co 2019-2024 + MIT + https://github.com/BSVN/Commons + https://github.com/BSVN/Commons.git + git + Please see CHANGELOG.md + 1.15.0 + True + True + BSN.Commons.Orm.Redis + README.md + True + snupkg + BSN.jpg + BSN;Commons;Onion;Enterprise;Infrastructure;DDD;redis + + true + + true + + + + true + + + + true + + + + disable + disable + + + + disable + disable + + + + true + + + True + \ + + + True + \ + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + @@ -14,4 +76,11 @@ + + + True + \ + + + diff --git a/Source/BSN.Commons/CHANGELOG.md b/Source/BSN.Commons/CHANGELOG.md index 5446966..333444d 100644 --- a/Source/BSN.Commons/CHANGELOG.md +++ b/Source/BSN.Commons/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.15.0] - 2024-02-25 + +### Added + +- Redis repository + ## [1.14.0] - 2024-01-13 ### Added diff --git a/Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs b/Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs index dfdb9b9..337de9f 100644 --- a/Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs +++ b/Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs @@ -21,7 +21,7 @@ public RedisDbContext(IOptions options) } /// - public int SaveChanges() + public virtual int SaveChanges() { throw new System.NotImplementedException("We don't have a way to save changes on redis om yet."); } diff --git a/Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs b/Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs new file mode 100644 index 0000000..7ea4b96 --- /dev/null +++ b/Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs @@ -0,0 +1,34 @@ +using BSN.Commons.Infrastructure; +using BSN.Commons.Infrastructure.Redis; +using BSN.Commons.Tests; +using Microsoft.Extensions.Options; +using Redis.OM.Searching; +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Text; + +namespace BSN.Commons.Test.Data +{ + public class UnitTestContext : RedisDbContext + { + public UnitTestContext(IOptions options) : base(options) + { + + } + + public static UnitTestContext Create(IOptions options) + { + return new UnitTestContext(options); + } + + public IRedisCollection Users { get; set; } + public IRedisCollection Documents { get; set; } + + public override int SaveChanges() + { + return base.SaveChanges(); + } + } +} diff --git a/Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs b/Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs new file mode 100644 index 0000000..dd2e5d3 --- /dev/null +++ b/Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs @@ -0,0 +1,45 @@ +using BSN.Commons.Test.Data; +using BSN.Commons.Infrastructure; +using System; +using System.Collections.Generic; +using System.Text; +using BSN.Commons.Infrastructure.Redis; + +namespace BSN.Commons.Test.Infrastructure +{ + internal class InMemoryDatabaseFactory : Disposable, IDatabaseFactory + { + private RedisDbContext _dataContext; + + protected override void DisposeCore() + { + if (_dataContext != null) + _dataContext.Dispose(); + } + + public new void Dispose() + { + if (_dataContext != null) + { + _dataContext.Dispose(); + _dataContext = null; + GC.SuppressFinalize(this); + } + } + + IDbContext IDatabaseFactory.Get() + { + if (_dataContext == null) + { + // TODO: Use UseInMemoryDatabase after implemented https://github.com/redis/redis-om-dotnet/issues/437 + _dataContext = new UnitTestContext(new DbContextOptionsBuilder() + .UseInMemoryDatabase(Guid.NewGuid().ToString()) + .Options); + + return (IDbContext)_dataContext; + } + else + return (IDbContext)_dataContext; + } + } +} From ac530ae3ed2bd207804d1b0a392d17f0fe7270f3 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Sun, 25 Feb 2024 22:58:35 +0330 Subject: [PATCH 2/3] Correct some file to better abstraction --- .../BSN.Commons.Orm.Redis.csproj | 6 +- .../BSN.Commons.Orm.Redis/DatabaseFactory.cs | 51 +++++++++++ Source/BSN.Commons.Orm.Redis/DbContext.cs | 47 ++++++++++ Source/BSN.Commons.Orm.Redis/ICreatable.cs | 21 +++++ ...disRepositoryBase.cs => RepositoryBase.cs} | 88 ++++++------------- .../Infrastructure/Redis/IRedisDbContext.cs | 15 ---- .../Redis/RedisConnectionOptions.cs | 6 ++ .../Redis/RedisDatabaseFactory.cs | 42 --------- .../Infrastructure/Redis/RedisDbContext.cs | 43 --------- .../Data/UnitTestContext.cs | 8 +- .../Infrastructure/DatabaseFactory.cs | 25 ++++-- .../Mock/UserRepository.cs | 2 +- .../RepositoryTest.cs | 10 +-- 13 files changed, 179 insertions(+), 185 deletions(-) create mode 100644 Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs create mode 100644 Source/BSN.Commons.Orm.Redis/DbContext.cs create mode 100644 Source/BSN.Commons.Orm.Redis/ICreatable.cs rename Source/BSN.Commons.Orm.Redis/{RedisRepositoryBase.cs => RepositoryBase.cs} (56%) delete mode 100644 Source/BSN.Commons/Infrastructure/Redis/IRedisDbContext.cs delete mode 100644 Source/BSN.Commons/Infrastructure/Redis/RedisDatabaseFactory.cs delete mode 100644 Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs diff --git a/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj b/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj index fc072ff..186189d 100644 --- a/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj +++ b/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj @@ -1,7 +1,7 @@  - net6.0;net8.0 + net8.0 1.15.0 1.15.0 BSN Developers @@ -62,6 +62,10 @@ + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs b/Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs new file mode 100644 index 0000000..51dda91 --- /dev/null +++ b/Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using BSN.Commons.Infrastructure; +using BSN.Commons.Infrastructure.Redis; +using System.Diagnostics.CodeAnalysis; + +namespace BSN.Commons.Orm.Redis +{ + /// + /// Database Factory for Redis + /// + public class DatabaseFactory : Disposable, IDatabaseFactory where TDbContext : Redis.DbContext, ICreatable, TDbContext> + { + /// + /// Constructor of Redis Database Factory + /// + /// App configuration + public DatabaseFactory(IConfiguration configuration) + { + redisConnectionOptions = Options.Create(configuration.GetSection("Redis").Get()); + } + + /// + /// Constructor of Redis Database Factory + /// + /// + public DatabaseFactory(IOptions options) + { + redisConnectionOptions = options; + } + + /// + public IDbContext Get() + { + return _redisDbContext ?? (_redisDbContext = TDbContext.Create(RedisConnectionOptions)); + } + + /// + /// Redis Connection Options + /// + protected IOptions RedisConnectionOptions => redisConnectionOptions; + + private readonly IOptions redisConnectionOptions; + private IDbContext _redisDbContext; + } +} diff --git a/Source/BSN.Commons.Orm.Redis/DbContext.cs b/Source/BSN.Commons.Orm.Redis/DbContext.cs new file mode 100644 index 0000000..a16a325 --- /dev/null +++ b/Source/BSN.Commons.Orm.Redis/DbContext.cs @@ -0,0 +1,47 @@ +using BSN.Commons.Infrastructure; +using BSN.Commons.Infrastructure.Redis; +using Microsoft.Extensions.Options; +using Redis.OM; +using Redis.OM.Contracts; +using Redis.OM.Searching; +using StackExchange.Redis; + +namespace BSN.Commons.Orm.Redis +{ + /// + /// Redis Database Context + /// + public class DbContext : RedisConnectionProvider, IDbContext, ICreatable, DbContext> + { + /// + /// Constructor of Redis Database Context + /// + /// Redis Connection Options + public DbContext(IOptions options) + : base(options.Value.ConnectionString) + { + } + + /// + /// Static factory method to create a new instance of the + /// + /// + /// + public static DbContext Create(IOptions options) + { + return new DbContext(options); + } + + /// + public virtual int SaveChanges() + { + throw new System.NotImplementedException("We don't have a way to save changes on redis om yet."); + } + + /// + public void Dispose() + { + // TODO release managed resources here + } + } +} \ No newline at end of file diff --git a/Source/BSN.Commons.Orm.Redis/ICreatable.cs b/Source/BSN.Commons.Orm.Redis/ICreatable.cs new file mode 100644 index 0000000..d9d1082 --- /dev/null +++ b/Source/BSN.Commons.Orm.Redis/ICreatable.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BSN.Commons.Orm.Redis +{ + /// + /// TODO + /// + public interface ICreatable + { + /// + /// TODO + /// + /// + /// + static abstract TResult Create(TArgument argument); + } +} diff --git a/Source/BSN.Commons.Orm.Redis/RedisRepositoryBase.cs b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs similarity index 56% rename from Source/BSN.Commons.Orm.Redis/RedisRepositoryBase.cs rename to Source/BSN.Commons.Orm.Redis/RepositoryBase.cs index 8e0da27..5ea0526 100644 --- a/Source/BSN.Commons.Orm.Redis/RedisRepositoryBase.cs +++ b/Source/BSN.Commons.Orm.Redis/RepositoryBase.cs @@ -1,9 +1,15 @@ -using System.Linq.Expressions; -using Redis.OM.Searching; +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Collections.Generic; + using Redis.OM; -using BSN.Commons.Infrastructure; using Redis.OM.Contracts; +using Redis.OM.Searching; + +using BSN.Commons.Infrastructure; using BSN.Commons.Infrastructure.Redis; +using System.Data.Common; namespace BSN.Commons.Orm.Redis { @@ -11,23 +17,25 @@ namespace BSN.Commons.Orm.Redis /// Repository Base for Redis Implementation /// /// - public class RedisRepositoryBase : IRepository where T : class + public class RepositoryBase : IRepository where T : class { /// /// Constructor for Redis Repository Base /// /// Database Factory Containing an IRedisDbContext - protected RedisRepositoryBase(IDatabaseFactory databaseFactory) + protected RepositoryBase(IDatabaseFactory databaseFactory) { DatabaseFactory = databaseFactory; + dbCollection = DataContext.RedisCollection(); + // TODO: Check that IndexCreationService is necessary or not. - Provider.Connection.CreateIndex(typeof(T)); + DataContext.Connection.CreateIndex(typeof(T)); } /// public void Add(T entity) { - Collection.Insert(entity); + dbCollection.Insert(entity); } /// @@ -42,7 +50,7 @@ public void AddRange(IEnumerable entities) /// public void Update(T entity) { - Collection.Update(entity); + dbCollection.Update(entity); } /// @@ -69,19 +77,19 @@ public void UpdateRange(IEnumerable entities, Action> config /// public void Delete(T entity) { - Collection.Delete(entity); + dbCollection.Delete(entity); } /// public void Delete(Expression> where) { - DeleteRange(Collection.Where(where)); + DeleteRange(dbCollection.Where(where)); } /// public void DeleteRange(IEnumerable entities) { - Collection.Delete(entities); + dbCollection.Delete(entities); } /// @@ -89,7 +97,7 @@ public T GetById(KeyType id) { if (id is string str_id) { - T? entity = Collection.FindById(str_id); + T? entity = dbCollection.FindById(str_id); if (entity == null) { throw new KeyNotFoundException($"entity with key of {id} was not found."); @@ -104,69 +112,27 @@ public T GetById(KeyType id) /// public T Get(Expression> where) { - return Collection.Where(where).FirstOrDefault(); + return dbCollection.Where(where).FirstOrDefault(); } /// public IEnumerable GetAll() { - return Collection.Where(entity => true); + return dbCollection.Where(entity => true); } /// public IEnumerable GetMany(Expression> where) { - return Collection.Where(where); + return dbCollection.Where(where); } - /// - /// Redis Collection accosiated with the type of T - /// - public IRedisCollection Collection - { - get - { - if (_redisCollection == null) - { - if (DatabaseFactory.Get() is IRedisDbContext dbContext) - { - _redisCollection = Provider.RedisCollection(); - } - else - { - throw new InvalidCastException("The database factory for redis must return an IRedisDbContext"); - } - } - - return _redisCollection; - } - } + protected readonly IRedisCollection dbCollection; - /// - /// Redis Connection Provider to access collections - /// - protected IRedisConnectionProvider Provider - { - get - { - if (_provider == null) - { - if (DatabaseFactory.Get() is IRedisDbContext dbContext) - { - _provider = dbContext.GetConnectionProvider(); - } - else - { - throw new InvalidCastException("The database factory for redis must return an IRedisDbContext"); - } - } + protected IDatabaseFactory DatabaseFactory { get; private set; } - return _provider; - } - } + protected IRedisConnectionProvider DataContext => _dataContext ?? (_dataContext = (IRedisConnectionProvider)DatabaseFactory.Get()); - protected IDatabaseFactory DatabaseFactory { get; private set; } - protected IRedisConnectionProvider? _provider; - protected IRedisCollection? _redisCollection; + private IRedisConnectionProvider _dataContext; } } \ No newline at end of file diff --git a/Source/BSN.Commons/Infrastructure/Redis/IRedisDbContext.cs b/Source/BSN.Commons/Infrastructure/Redis/IRedisDbContext.cs deleted file mode 100644 index 10fc758..0000000 --- a/Source/BSN.Commons/Infrastructure/Redis/IRedisDbContext.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Redis.OM.Contracts; - -namespace BSN.Commons.Infrastructure.Redis -{ - /// - /// Interface for Redis Database Context - /// - public interface IRedisDbContext : IDbContext - { - /// - /// Get the connection provider for Redis - /// - IRedisConnectionProvider GetConnectionProvider(); - } -} diff --git a/Source/BSN.Commons/Infrastructure/Redis/RedisConnectionOptions.cs b/Source/BSN.Commons/Infrastructure/Redis/RedisConnectionOptions.cs index 37fb290..07a917d 100644 --- a/Source/BSN.Commons/Infrastructure/Redis/RedisConnectionOptions.cs +++ b/Source/BSN.Commons/Infrastructure/Redis/RedisConnectionOptions.cs @@ -5,6 +5,12 @@ /// public class RedisConnectionOptions { + /// + /// Contains the name of the configuration section to bind to. In this scenario, + /// the options object provides the name of its configuration section. + /// + public const string ConfigurationSectionName = "RedisConnectionOptions"; + /// /// Connection String to connect to Redis for example: localhost:6379 /// diff --git a/Source/BSN.Commons/Infrastructure/Redis/RedisDatabaseFactory.cs b/Source/BSN.Commons/Infrastructure/Redis/RedisDatabaseFactory.cs deleted file mode 100644 index 257d6e0..0000000 --- a/Source/BSN.Commons/Infrastructure/Redis/RedisDatabaseFactory.cs +++ /dev/null @@ -1,42 +0,0 @@ -using BSN.Commons.Infrastructure; -using Microsoft.Extensions.Options; -using Redis.OM; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Redis.OM.Contracts; -using BSN.Commons.Infrastructure.Redis; - -namespace BSN.Commons.Infrastructure.Redis -{ - /// - /// Database Factory for Redis - /// - public class RedisDatabaseFactory : IDatabaseFactory - { - /// - /// Constructor of Redis Database Factory - /// - /// Redis Database Context - public RedisDatabaseFactory(IRedisDbContext redisDbContext) - { - _redisDbContext = redisDbContext; - } - - /// - public IDbContext Get() - { - return _redisDbContext; - } - - /// - public void Dispose() - { - // TODO release managed resources here - } - - private readonly IRedisDbContext _redisDbContext; - } -} diff --git a/Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs b/Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs deleted file mode 100644 index 337de9f..0000000 --- a/Source/BSN.Commons/Infrastructure/Redis/RedisDbContext.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Microsoft.Extensions.Options; -using Redis.OM; -using Redis.OM.Contracts; -using Redis.OM.Searching; -using StackExchange.Redis; - -namespace BSN.Commons.Infrastructure.Redis -{ - /// - /// Redis Database Context - /// - public class RedisDbContext : IRedisDbContext - { - /// - /// Constructor of Redis Database Context - /// - /// Redis Connection Options - public RedisDbContext(IOptions options) - { - _provider = new RedisConnectionProvider(options.Value.ConnectionString); - } - - /// - public virtual int SaveChanges() - { - throw new System.NotImplementedException("We don't have a way to save changes on redis om yet."); - } - - /// - public IRedisConnectionProvider GetConnectionProvider() - { - return _provider; - } - - /// - public void Dispose() - { - // TODO release managed resources here - } - - private IRedisConnectionProvider _provider; - } -} \ No newline at end of file diff --git a/Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs b/Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs index 7ea4b96..50b45c7 100644 --- a/Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs +++ b/Test/BSN.Commons.Orm.Redis.Tests/Data/UnitTestContext.cs @@ -1,5 +1,6 @@ using BSN.Commons.Infrastructure; using BSN.Commons.Infrastructure.Redis; +using BSN.Commons.Orm.Redis; using BSN.Commons.Tests; using Microsoft.Extensions.Options; using Redis.OM.Searching; @@ -11,17 +12,14 @@ namespace BSN.Commons.Test.Data { - public class UnitTestContext : RedisDbContext + public class UnitTestContext : Orm.Redis.DbContext, ICreatable, UnitTestContext> { public UnitTestContext(IOptions options) : base(options) { } - public static UnitTestContext Create(IOptions options) - { - return new UnitTestContext(options); - } + public new static UnitTestContext Create(IOptions options) => new UnitTestContext(options); public IRedisCollection Users { get; set; } public IRedisCollection Documents { get; set; } diff --git a/Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs b/Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs index dd2e5d3..30e3a78 100644 --- a/Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs +++ b/Test/BSN.Commons.Orm.Redis.Tests/Infrastructure/DatabaseFactory.cs @@ -4,37 +4,44 @@ using System.Collections.Generic; using System.Text; using BSN.Commons.Infrastructure.Redis; +using Redis.OM; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Configuration; +using BSN.Commons.Orm.Redis; namespace BSN.Commons.Test.Infrastructure { - internal class InMemoryDatabaseFactory : Disposable, IDatabaseFactory + internal class InMemoryDatabaseFactory : DatabaseFactory { - private RedisDbContext _dataContext; + public InMemoryDatabaseFactory() : base(Options.Create(new RedisConnectionOptions + { + ConnectionString = "redis://localhost:6379" + })) + { + + } + + private RedisConnectionProvider _dataContext; protected override void DisposeCore() { - if (_dataContext != null) - _dataContext.Dispose(); } public new void Dispose() { if (_dataContext != null) { - _dataContext.Dispose(); _dataContext = null; GC.SuppressFinalize(this); } } - IDbContext IDatabaseFactory.Get() + public IDbContext Get() { if (_dataContext == null) { // TODO: Use UseInMemoryDatabase after implemented https://github.com/redis/redis-om-dotnet/issues/437 - _dataContext = new UnitTestContext(new DbContextOptionsBuilder() - .UseInMemoryDatabase(Guid.NewGuid().ToString()) - .Options); + _dataContext = new UnitTestContext(RedisConnectionOptions); return (IDbContext)_dataContext; } diff --git a/Test/BSN.Commons.Orm.Redis.Tests/Mock/UserRepository.cs b/Test/BSN.Commons.Orm.Redis.Tests/Mock/UserRepository.cs index e3c9330..57ecfde 100644 --- a/Test/BSN.Commons.Orm.Redis.Tests/Mock/UserRepository.cs +++ b/Test/BSN.Commons.Orm.Redis.Tests/Mock/UserRepository.cs @@ -4,7 +4,7 @@ namespace BSN.Commons.Orm.Redis.Tests.Mock { - public sealed class UserRepository : RedisRepositoryBase, IRepository + public sealed class UserRepository : RepositoryBase, IRepository { public UserRepository(IDatabaseFactory databaseFactory) : base(databaseFactory) { } diff --git a/Test/BSN.Commons.Orm.Redis.Tests/RepositoryTest.cs b/Test/BSN.Commons.Orm.Redis.Tests/RepositoryTest.cs index 0fee6da..3e24e4f 100644 --- a/Test/BSN.Commons.Orm.Redis.Tests/RepositoryTest.cs +++ b/Test/BSN.Commons.Orm.Redis.Tests/RepositoryTest.cs @@ -9,6 +9,7 @@ using BSN.Commons.Infrastructure.Redis; using Microsoft.Extensions.Options; using NUnit.Framework; +using BSN.Commons.Test.Infrastructure; namespace BSN.Commons.Orm.Redis.Tests { @@ -47,14 +48,7 @@ public void AddUserToDataBase_UserShouldBeCorrectlyAddedToDatabase() public IDatabaseFactory CreateDatabaseFactory() { - var redisConnectionOptions = new RedisConnectionOptions - { - ConnectionString = "redis://localhost:6379" - }; - - var dbContext = new RedisDbContext(Options.Create(redisConnectionOptions)); - - return new RedisDatabaseFactory(dbContext); + return new InMemoryDatabaseFactory(); } public IRepository CreateUserRepository(IDatabaseFactory databaseFactory) From a85144424cc71e6112deeebed38be1cac7e8bf04 Mon Sep 17 00:00:00 2001 From: soroshsabz Date: Mon, 26 Feb 2024 02:10:18 +0330 Subject: [PATCH 3/3] Update version --- .../Properties/AssemblyInfo.cs | 6 +++--- .../BSN.Commons.Orm.EntityFrameworkCore.csproj | 6 +++--- Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj | 2 +- .../BSN.Commons.PresentationInfrastructure.csproj | 6 +++--- Source/BSN.Commons.Users/Properties/AssemblyInfo.cs | 6 +++--- Source/BSN.Commons/BSN.Commons.csproj | 6 +++--- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Source/BSN.Commons.Orm.EntityFramework/Properties/AssemblyInfo.cs b/Source/BSN.Commons.Orm.EntityFramework/Properties/AssemblyInfo.cs index 86a64b6..c94dd6b 100644 --- a/Source/BSN.Commons.Orm.EntityFramework/Properties/AssemblyInfo.cs +++ b/Source/BSN.Commons.Orm.EntityFramework/Properties/AssemblyInfo.cs @@ -11,9 +11,9 @@ [assembly: AssemblyDescription("ORM Helpers for using entity framework in enterprise application")] [assembly: AssemblyCompany("BSN Company")] [assembly: AssemblyProduct("BSN.Commons.Orm.EntityFramework")] -[assembly: AssemblyVersion("1.14.0")] -[assembly: AssemblyFileVersion("1.14.0")] -[assembly: AssemblyInformationalVersion("1.14.0")] +[assembly: AssemblyVersion("1.15.0")] +[assembly: AssemblyFileVersion("1.15.0")] +[assembly: AssemblyInformationalVersion("1.15.0")] [assembly: AssemblyCopyright("Copyright © 2020-2024 BSN Co.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyConfiguration("")] diff --git a/Source/BSN.Commons.Orm.EntityFrameworkCore/BSN.Commons.Orm.EntityFrameworkCore.csproj b/Source/BSN.Commons.Orm.EntityFrameworkCore/BSN.Commons.Orm.EntityFrameworkCore.csproj index d760368..934eed2 100644 --- a/Source/BSN.Commons.Orm.EntityFrameworkCore/BSN.Commons.Orm.EntityFrameworkCore.csproj +++ b/Source/BSN.Commons.Orm.EntityFrameworkCore/BSN.Commons.Orm.EntityFrameworkCore.csproj @@ -2,8 +2,8 @@ net6.0;net8.0 - 1.14.0 - 1.14.0 + 1.15.0 + 1.15.0 BSN Developers BSN Company ORM Helpers for using entity framework core in enterprise application @@ -13,7 +13,7 @@ https://github.com/BSVN/Commons.git git Please see CHANGELOG.md - 1.14.0 + 1.15.0 True True BSN.Commons.Orm.EntityFrameworkCore diff --git a/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj b/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj index 186189d..0073155 100644 --- a/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj +++ b/Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/Source/BSN.Commons.PresentationInfrastructure/BSN.Commons.PresentationInfrastructure.csproj b/Source/BSN.Commons.PresentationInfrastructure/BSN.Commons.PresentationInfrastructure.csproj index 8360a4b..b7a0dc7 100644 --- a/Source/BSN.Commons.PresentationInfrastructure/BSN.Commons.PresentationInfrastructure.csproj +++ b/Source/BSN.Commons.PresentationInfrastructure/BSN.Commons.PresentationInfrastructure.csproj @@ -3,9 +3,9 @@ netstandard2.0 Presentation infrastructure layer of BSN.Commons library for enterprise application. - 1.14.0 - 1.14.0 - 1.14.0 + 1.15.0 + 1.15.0 + 1.15.0 BSN Developers BSN Company BSN Co 2019-2024 diff --git a/Source/BSN.Commons.Users/Properties/AssemblyInfo.cs b/Source/BSN.Commons.Users/Properties/AssemblyInfo.cs index 3aba32b..b8e17db 100644 --- a/Source/BSN.Commons.Users/Properties/AssemblyInfo.cs +++ b/Source/BSN.Commons.Users/Properties/AssemblyInfo.cs @@ -11,9 +11,9 @@ [assembly: AssemblyDescription("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("BSN.Commons.Users")] -[assembly: AssemblyVersion("1.14.0")] -[assembly: AssemblyFileVersion("1.14.0")] -[assembly: AssemblyInformationalVersion("1.14.0")] +[assembly: AssemblyVersion("1.15.0")] +[assembly: AssemblyFileVersion("1.15.0")] +[assembly: AssemblyInformationalVersion("1.15.0")] [assembly: AssemblyCopyright("Copyright © 2022 BSN Co.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyConfiguration("")] diff --git a/Source/BSN.Commons/BSN.Commons.csproj b/Source/BSN.Commons/BSN.Commons.csproj index 1049303..0c39837 100644 --- a/Source/BSN.Commons/BSN.Commons.csproj +++ b/Source/BSN.Commons/BSN.Commons.csproj @@ -2,13 +2,13 @@ netstandard2.0 - 1.14.0 - 1.14.0 + 1.15.0 + 1.15.0 BSN Company BSN Developers BSN Co 2019-2024 true - 1.14.0 + 1.15.0 https://github.com/BSVN/Commons Commons library for enterprise application