Skip to content

Commit

Permalink
Redis repository design improvement (#79)
Browse files Browse the repository at this point in the history
* Update version

* Correct some file to better abstraction
  • Loading branch information
soroshsabz committed Feb 25, 2024
1 parent d17597a commit 6a41667
Show file tree
Hide file tree
Showing 20 changed files with 347 additions and 197 deletions.
4 changes: 2 additions & 2 deletions Build/BSN.Commons.Orm.EntityFramework.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>BSN.Commons.Orm.EntityFramework</id>
<version>1.14.0</version>
<version>1.15.0</version>
<authors>Seyyed Soroosh Hosseinalipour</authors>
<owners>sorosh_sabz, BSN</owners>
<license type="expression">MIT</license>
Expand All @@ -16,7 +16,7 @@
<copyright>BSN Co 2019-2024</copyright>
<tags>Commons ORM Enterprise</tags>
<dependencies>
<dependency id="BSN.Commons" version="1.12.0" />
<dependency id="BSN.Commons" version="1.15.0" />
<dependency id="EntityFramework" version="6.4.0" />
<dependency id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.3" />
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions Build/BSN.Commons.Users.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>BSN.Commons.Users</id>
<version>1.14.0</version>
<version>1.15.0</version>
<authors>Seyyed Soroosh Hosseinalipour</authors>
<owners>sorosh_sabz, BSN</owners>
<license type="expression">MIT</license>
Expand All @@ -16,7 +16,7 @@
<copyright>BSN Co 2019-2024</copyright>
<tags>Commons Users Enterprise</tags>
<dependencies>
<dependency id="BSN.Commons" version="1.12.0" />
<dependency id="BSN.Commons" version="1.15.0" />
<dependency id="EntityFramework" version="6.4.0" />
<dependency id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.3" />
</dependencies>
Expand Down
25 changes: 13 additions & 12 deletions Build/build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -51,7 +53,7 @@ Task("Clean")
if (DirectoryExists(artifactsDir))
{
DeleteDirectory(
artifactsDir,
artifactsDir,
new DeleteDirectorySettings {
Recursive = true,
Force = true
Expand Down Expand Up @@ -92,7 +94,7 @@ Task("Build")
.Does(() => {
DotNetBuild(
solutionPath,
new DotNetBuildSettings
new DotNetBuildSettings
{
Configuration = configuration
}
Expand All @@ -117,7 +119,7 @@ Task("Test")
CoverletOutputDirectory = Directory(artifactsDir),
CoverletOutputName = specificCoverageResultsFileName
};
DotNetTest(testFolder + testProject.path + testProject.name, settings, coverletSettings);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -185,24 +187,23 @@ 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);
}
else {
Information($"Bypassing publishing \"{pkg}\" as it is already published.");
}
}
});

Expand All @@ -211,7 +212,7 @@ private bool IsNuGetPublished(FilePath packagePath) {

var latestPublishedVersions = NuGetList(
package.NuspecReader.GetId(),
new NuGetListSettings
new NuGetListSettings
{
Prerelease = true
}
Expand Down
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mode: ContinuousDelivery
next-version: 1.14.0
next-version: 1.15.0
branches: {}
ignore:
sha: []
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# BSN.Commons

ITNOA

| | |
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,20 +69,20 @@ private static void MapAllReadonlyProperties<T>(Microsoft.EntityFrameworkCore.Me
var navigations = entityType.GetNavigations().Select(n => n.Name);
IEnumerable<PropertyInfo> properties = from property in typeof(T).GetProperties()
where property.CanWrite == false
&& property.GetCustomAttribute<NotMappedAttribute>() == null
&& property.GetMethod.GetCustomAttribute<CompilerGeneratedAttribute>() != null
&& property.GetCustomAttribute<NotMappedAttribute>() == null
&& property.GetMethod.GetCustomAttribute<CompilerGeneratedAttribute>() != null
&& !ignores.Any(ignoreProperty => ignoreProperty == property.Name)
&& !navigations.Contains(property.Name)
select property;

// about following condition in above code:
// && property.GetMethod.GetCustomAttribute<CompilerGeneratedAttribute>() != 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.

Expand Down
79 changes: 76 additions & 3 deletions Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,77 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyVersion>1.15.0</AssemblyVersion>
<FileVersion>1.15.0</FileVersion>
<Authors>BSN Developers</Authors>
<Company>BSN Company</Company>
<Description>ORM Helpers for using redis-om in enterprise application</Description>
<Copyright>BSN Co 2019-2024</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/BSVN/Commons</PackageProjectUrl>
<RepositoryUrl>https://github.com/BSVN/Commons.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Please see CHANGELOG.md</PackageReleaseNotes>
<Version>1.15.0</Version>
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Title>BSN.Commons.Orm.Redis</Title>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageIcon>BSN.jpg</PackageIcon>
<PackageTags>BSN;Commons;Onion;Enterprise;Infrastructure;DDD;redis</PackageTags>
<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Optional: Embed source files that are not tracked by the source control manager in the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\BSN.jpg">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Redis.OM" Version="0.6.1" />
</ItemGroup>
Expand All @@ -14,4 +80,11 @@
<ProjectReference Include="..\BSN.Commons\BSN.Commons.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions Source/BSN.Commons.Orm.Redis/DatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Database Factory for Redis
/// </summary>
public class DatabaseFactory<TDbContext> : Disposable, IDatabaseFactory where TDbContext : Redis.DbContext, ICreatable<IOptions<RedisConnectionOptions>, TDbContext>
{
/// <summary>
/// Constructor of Redis Database Factory
/// </summary>
/// <param name="configuration">App configuration</param>
public DatabaseFactory(IConfiguration configuration)
{
redisConnectionOptions = Options.Create(configuration.GetSection("Redis").Get<RedisConnectionOptions>());
}

/// <summary>
/// Constructor of Redis Database Factory
/// </summary>
/// <param name="options"></param>
public DatabaseFactory(IOptions<RedisConnectionOptions> options)
{
redisConnectionOptions = options;
}

/// <inheritdoc/>
public IDbContext Get()
{
return _redisDbContext ?? (_redisDbContext = TDbContext.Create(RedisConnectionOptions));
}

/// <summary>
/// Redis Connection Options
/// </summary>
protected IOptions<RedisConnectionOptions> RedisConnectionOptions => redisConnectionOptions;

private readonly IOptions<RedisConnectionOptions> redisConnectionOptions;
private IDbContext _redisDbContext;
}
}
47 changes: 47 additions & 0 deletions Source/BSN.Commons.Orm.Redis/DbContext.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Redis Database Context
/// </summary>
public class DbContext : RedisConnectionProvider, IDbContext, ICreatable<IOptions<RedisConnectionOptions>, DbContext>
{
/// <summary>
/// Constructor of Redis Database Context
/// </summary>
/// <param name="options">Redis Connection Options</param>
public DbContext(IOptions<RedisConnectionOptions> options)
: base(options.Value.ConnectionString)
{
}

/// <summary>
/// Static factory method to create a new instance of the <see cref="DbContext"/>
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public static DbContext Create(IOptions<RedisConnectionOptions> options)
{
return new DbContext(options);
}

/// <inheritdoc/>
public virtual int SaveChanges()
{
throw new System.NotImplementedException("We don't have a way to save changes on redis om yet.");
}

/// <inheritdoc/>
public void Dispose()
{
// TODO release managed resources here
}
}
}
Loading

0 comments on commit 6a41667

Please sign in to comment.