-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic functionality --------- Co-authored-by: Farzaneh Soltanzadeh <[email protected]> Co-authored-by: Ebrahim <[email protected]>
- Loading branch information
1 parent
dcef162
commit d17597a
Showing
17 changed files
with
495 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Source/BSN.Commons/Infrastructure/Redis/IRedisDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
Source/BSN.Commons/Infrastructure/Redis/RedisConnectionOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
Source/BSN.Commons/Infrastructure/Redis/RedisDatabaseFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.