Skip to content

Commit

Permalink
Add Redis RepositoryBase (#77)
Browse files Browse the repository at this point in the history
Add basic functionality

---------

Co-authored-by: Farzaneh Soltanzadeh <[email protected]>
Co-authored-by: Ebrahim <[email protected]>
  • Loading branch information
3 people committed Feb 25, 2024
1 parent dcef162 commit d17597a
Show file tree
Hide file tree
Showing 17 changed files with 495 additions and 4 deletions.
14 changes: 14 additions & 0 deletions BSN.Commons.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BSN.Commons.Orm.EntityFrame
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.Users", "Source\BSN.Commons.Users\BSN.Commons.Users.csproj", "{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.Orm.Redis", "Source\BSN.Commons.Orm.Redis\BSN.Commons.Orm.Redis.csproj", "{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.Orm.Redis.Tests", "Test\BSN.Commons.Orm.Redis.Tests\BSN.Commons.Orm.Redis.Tests.csproj", "{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -81,6 +85,14 @@ Global
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}.Release|Any CPU.Build.0 = Release|Any CPU
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Release|Any CPU.Build.0 = Release|Any CPU
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -94,6 +106,8 @@ Global
{906FEED8-23E0-4EEF-B902-C39325C50480} = {5C6BA7B5-832A-495A-AF5E-C2A74F6A1EF9}
{335F645B-C85F-42C2-9185-A216101F60C7} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096}
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096}
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096}
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A} = {5C6BA7B5-832A-495A-AF5E-C2A74F6A1EF9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BCAF76D3-AA3C-4D0F-8D10-34065F8FED09}
Expand Down
17 changes: 17 additions & 0 deletions Source/BSN.Commons.Orm.Redis/BSN.Commons.Orm.Redis.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Redis.OM" Version="0.6.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BSN.Commons\BSN.Commons.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions Source/BSN.Commons.Orm.Redis/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

ITNOA

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- RepositoryBase
5 changes: 5 additions & 0 deletions Source/BSN.Commons.Orm.Redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# BSN.Commons.Orm.Redis

ITNOA

This package contains some facilities for using REDIS OM in Enterprise Way
172 changes: 172 additions & 0 deletions Source/BSN.Commons.Orm.Redis/RedisRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System.Linq.Expressions;
using Redis.OM.Searching;
using Redis.OM;
using BSN.Commons.Infrastructure;
using Redis.OM.Contracts;
using BSN.Commons.Infrastructure.Redis;

namespace BSN.Commons.Orm.Redis
{
/// <summary>
/// Repository Base for Redis Implementation
/// </summary>
/// <typeparam name="T"></typeparam>
public class RedisRepositoryBase<T> : IRepository<T> where T : class
{
/// <summary>
/// Constructor for Redis Repository Base
/// </summary>
/// <param name="databaseFactory">Database Factory Containing an IRedisDbContext</param>
protected RedisRepositoryBase(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
// TODO: Check that IndexCreationService is necessary or not.
Provider.Connection.CreateIndex(typeof(T));
}

/// <inheritdoc />
public void Add(T entity)
{
Collection.Insert(entity);
}

/// <inheritdoc />
public void AddRange(IEnumerable<T> entities)
{
foreach (var entity in entities)
{
Add(entity);
}
}

/// <inheritdoc />
public void Update(T entity)
{
Collection.Update(entity);
}

/// <inheritdoc />
public void Update(T entity, Action<IUpdateConfig<T>> configurer)
{
throw new NotImplementedException("We don't have a way to update with a configuration on redis");
}

/// <inheritdoc />
public void UpdateRange(IEnumerable<T> entities)
{
foreach (var entity in entities)
{
Update(entity);
}
}

/// <inheritdoc />
public void UpdateRange(IEnumerable<T> entities, Action<IUpdateConfig<T>> configurer)
{
throw new NotImplementedException("We don't have a way to update range with a configuration on redis");
}

/// <inheritdoc />
public void Delete(T entity)
{
Collection.Delete(entity);
}

/// <inheritdoc />
public void Delete(Expression<Func<T, bool>> where)
{
DeleteRange(Collection.Where(where));
}

/// <inheritdoc />
public void DeleteRange(IEnumerable<T> entities)
{
Collection.Delete(entities);
}

/// <inheritdoc />
public T GetById<KeyType>(KeyType id)
{
if (id is string str_id)
{
T? entity = Collection.FindById(str_id);
if (entity == null)
{
throw new KeyNotFoundException($"entity with key of {id} was not found.");
}

return entity;
}

throw new NotImplementedException($"KeyType of {typeof(KeyType)} is not supported.");
}

/// <inheritdoc />
public T Get(Expression<Func<T, bool>> where)
{
return Collection.Where(where).FirstOrDefault();
}

/// <inheritdoc />
public IEnumerable<T> GetAll()
{
return Collection.Where(entity => true);
}

/// <inheritdoc />
public IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
return Collection.Where(where);
}

/// <summary>
/// Redis Collection accosiated with the type of T
/// </summary>
public IRedisCollection<T> Collection
{
get
{
if (_redisCollection == null)
{
if (DatabaseFactory.Get() is IRedisDbContext dbContext)
{
_redisCollection = Provider.RedisCollection<T>();
}
else
{
throw new InvalidCastException("The database factory for redis must return an IRedisDbContext");
}
}

return _redisCollection;
}
}

/// <summary>
/// Redis Connection Provider to access collections
/// </summary>
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");
}
}

return _provider;
}
}

protected IDatabaseFactory DatabaseFactory { get; private set; }
protected IRedisConnectionProvider? _provider;
protected IRedisCollection<T>? _redisCollection;
}
}
1 change: 1 addition & 0 deletions Source/BSN.Commons/BSN.Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<PackageReference Include="Polly" Version="8.0.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Redis.OM" Version="0.6.1" />
<PackageReference Include="System.Text.Json" Version="6.0.6" />
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions Source/BSN.Commons/Infrastructure/IDatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace BSN.Commons.Infrastructure
{
/// <summary>
/// Interface for Database Factory
/// </summary>
public interface IDatabaseFactory : IDisposable
{
/// <summary>
/// Get the database context
/// </summary>
/// <returns></returns>
IDbContext Get();
}
}
15 changes: 11 additions & 4 deletions Source/BSN.Commons/Infrastructure/IDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace BSN.Commons.Infrastructure
{
public interface IDbContext: IDisposable
{
int SaveChanges();
}
/// <summary>
/// Interface for Database Context
/// </summary>
public interface IDbContext : IDisposable
{
/// <summary>
/// Save changes to the database
/// </summary>
/// <returns></returns>
int SaveChanges();
}
}
15 changes: 15 additions & 0 deletions Source/BSN.Commons/Infrastructure/Redis/IRedisDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Redis.OM.Contracts;

namespace BSN.Commons.Infrastructure.Redis
{
/// <summary>
/// Interface for Redis Database Context
/// </summary>
public interface IRedisDbContext : IDbContext
{
/// <summary>
/// Get the connection provider for Redis
/// </summary>
IRedisConnectionProvider GetConnectionProvider();
}
}
13 changes: 13 additions & 0 deletions Source/BSN.Commons/Infrastructure/Redis/RedisConnectionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BSN.Commons.Infrastructure.Redis
{
/// <summary>
/// Redis Connection Options
/// </summary>
public class RedisConnectionOptions
{
/// <summary>
/// Connection String to connect to Redis for example: localhost:6379
/// </summary>
public string ConnectionString { get; set; }
}
}
42 changes: 42 additions & 0 deletions Source/BSN.Commons/Infrastructure/Redis/RedisDatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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
{
/// <summary>
/// Database Factory for Redis
/// </summary>
public class RedisDatabaseFactory : IDatabaseFactory
{
/// <summary>
/// Constructor of Redis Database Factory
/// </summary>
/// <param name="redisDbContext">Redis Database Context</param>
public RedisDatabaseFactory(IRedisDbContext redisDbContext)
{
_redisDbContext = redisDbContext;
}

/// <inheritdoc/>
public IDbContext Get()
{
return _redisDbContext;
}

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

private readonly IRedisDbContext _redisDbContext;
}
}
Loading

0 comments on commit d17597a

Please sign in to comment.