Skip to content

Commit d17597a

Browse files
Fr-SoltanzadehFarzaneh Soltanzadehmeghasemim1999
authored
Add Redis RepositoryBase (#77)
Add basic functionality --------- Co-authored-by: Farzaneh Soltanzadeh <[email protected]> Co-authored-by: Ebrahim <[email protected]>
1 parent dcef162 commit d17597a

17 files changed

+495
-4
lines changed

BSN.Commons.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BSN.Commons.Orm.EntityFrame
4343
EndProject
4444
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BSN.Commons.Users", "Source\BSN.Commons.Users\BSN.Commons.Users.csproj", "{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}"
4545
EndProject
46+
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}"
47+
EndProject
48+
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}"
49+
EndProject
4650
Global
4751
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4852
Debug|Any CPU = Debug|Any CPU
@@ -81,6 +85,14 @@ Global
8185
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}.Debug|Any CPU.Build.0 = Debug|Any CPU
8286
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}.Release|Any CPU.ActiveCfg = Release|Any CPU
8387
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D}.Release|Any CPU.Build.0 = Release|Any CPU
88+
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
89+
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
90+
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
91+
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A}.Release|Any CPU.Build.0 = Release|Any CPU
8496
EndGlobalSection
8597
GlobalSection(SolutionProperties) = preSolution
8698
HideSolutionNode = FALSE
@@ -94,6 +106,8 @@ Global
94106
{906FEED8-23E0-4EEF-B902-C39325C50480} = {5C6BA7B5-832A-495A-AF5E-C2A74F6A1EF9}
95107
{335F645B-C85F-42C2-9185-A216101F60C7} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096}
96108
{213ABCEF-7E9A-4CE5-A3EF-289C9781344D} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096}
109+
{1A1586E8-46EB-43AC-91EC-F6EDCA5689A9} = {DC377ADC-CC9D-4785-81BE-726DBF5F3096}
110+
{2D1DB295-5181-48D7-8EC0-1147ED2DAD4A} = {5C6BA7B5-832A-495A-AF5E-C2A74F6A1EF9}
97111
EndGlobalSection
98112
GlobalSection(ExtensibilityGlobals) = postSolution
99113
SolutionGuid = {BCAF76D3-AA3C-4D0F-8D10-34065F8FED09}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Redis.OM" Version="0.6.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\BSN.Commons\BSN.Commons.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
3+
ITNOA
4+
5+
All notable changes to this project will be documented in this file.
6+
7+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
9+
10+
## [Unreleased]
11+
12+
### Added
13+
14+
- RepositoryBase
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# BSN.Commons.Orm.Redis
2+
3+
ITNOA
4+
5+
This package contains some facilities for using REDIS OM in Enterprise Way
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
using System.Linq.Expressions;
2+
using Redis.OM.Searching;
3+
using Redis.OM;
4+
using BSN.Commons.Infrastructure;
5+
using Redis.OM.Contracts;
6+
using BSN.Commons.Infrastructure.Redis;
7+
8+
namespace BSN.Commons.Orm.Redis
9+
{
10+
/// <summary>
11+
/// Repository Base for Redis Implementation
12+
/// </summary>
13+
/// <typeparam name="T"></typeparam>
14+
public class RedisRepositoryBase<T> : IRepository<T> where T : class
15+
{
16+
/// <summary>
17+
/// Constructor for Redis Repository Base
18+
/// </summary>
19+
/// <param name="databaseFactory">Database Factory Containing an IRedisDbContext</param>
20+
protected RedisRepositoryBase(IDatabaseFactory databaseFactory)
21+
{
22+
DatabaseFactory = databaseFactory;
23+
// TODO: Check that IndexCreationService is necessary or not.
24+
Provider.Connection.CreateIndex(typeof(T));
25+
}
26+
27+
/// <inheritdoc />
28+
public void Add(T entity)
29+
{
30+
Collection.Insert(entity);
31+
}
32+
33+
/// <inheritdoc />
34+
public void AddRange(IEnumerable<T> entities)
35+
{
36+
foreach (var entity in entities)
37+
{
38+
Add(entity);
39+
}
40+
}
41+
42+
/// <inheritdoc />
43+
public void Update(T entity)
44+
{
45+
Collection.Update(entity);
46+
}
47+
48+
/// <inheritdoc />
49+
public void Update(T entity, Action<IUpdateConfig<T>> configurer)
50+
{
51+
throw new NotImplementedException("We don't have a way to update with a configuration on redis");
52+
}
53+
54+
/// <inheritdoc />
55+
public void UpdateRange(IEnumerable<T> entities)
56+
{
57+
foreach (var entity in entities)
58+
{
59+
Update(entity);
60+
}
61+
}
62+
63+
/// <inheritdoc />
64+
public void UpdateRange(IEnumerable<T> entities, Action<IUpdateConfig<T>> configurer)
65+
{
66+
throw new NotImplementedException("We don't have a way to update range with a configuration on redis");
67+
}
68+
69+
/// <inheritdoc />
70+
public void Delete(T entity)
71+
{
72+
Collection.Delete(entity);
73+
}
74+
75+
/// <inheritdoc />
76+
public void Delete(Expression<Func<T, bool>> where)
77+
{
78+
DeleteRange(Collection.Where(where));
79+
}
80+
81+
/// <inheritdoc />
82+
public void DeleteRange(IEnumerable<T> entities)
83+
{
84+
Collection.Delete(entities);
85+
}
86+
87+
/// <inheritdoc />
88+
public T GetById<KeyType>(KeyType id)
89+
{
90+
if (id is string str_id)
91+
{
92+
T? entity = Collection.FindById(str_id);
93+
if (entity == null)
94+
{
95+
throw new KeyNotFoundException($"entity with key of {id} was not found.");
96+
}
97+
98+
return entity;
99+
}
100+
101+
throw new NotImplementedException($"KeyType of {typeof(KeyType)} is not supported.");
102+
}
103+
104+
/// <inheritdoc />
105+
public T Get(Expression<Func<T, bool>> where)
106+
{
107+
return Collection.Where(where).FirstOrDefault();
108+
}
109+
110+
/// <inheritdoc />
111+
public IEnumerable<T> GetAll()
112+
{
113+
return Collection.Where(entity => true);
114+
}
115+
116+
/// <inheritdoc />
117+
public IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
118+
{
119+
return Collection.Where(where);
120+
}
121+
122+
/// <summary>
123+
/// Redis Collection accosiated with the type of T
124+
/// </summary>
125+
public IRedisCollection<T> Collection
126+
{
127+
get
128+
{
129+
if (_redisCollection == null)
130+
{
131+
if (DatabaseFactory.Get() is IRedisDbContext dbContext)
132+
{
133+
_redisCollection = Provider.RedisCollection<T>();
134+
}
135+
else
136+
{
137+
throw new InvalidCastException("The database factory for redis must return an IRedisDbContext");
138+
}
139+
}
140+
141+
return _redisCollection;
142+
}
143+
}
144+
145+
/// <summary>
146+
/// Redis Connection Provider to access collections
147+
/// </summary>
148+
protected IRedisConnectionProvider Provider
149+
{
150+
get
151+
{
152+
if (_provider == null)
153+
{
154+
if (DatabaseFactory.Get() is IRedisDbContext dbContext)
155+
{
156+
_provider = dbContext.GetConnectionProvider();
157+
}
158+
else
159+
{
160+
throw new InvalidCastException("The database factory for redis must return an IRedisDbContext");
161+
}
162+
}
163+
164+
return _provider;
165+
}
166+
}
167+
168+
protected IDatabaseFactory DatabaseFactory { get; private set; }
169+
protected IRedisConnectionProvider? _provider;
170+
protected IRedisCollection<T>? _redisCollection;
171+
}
172+
}

Source/BSN.Commons/BSN.Commons.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<PackageReference Include="Polly" Version="8.0.0" />
6767
<PackageReference Include="RabbitMQ.Client" Version="6.6.0" />
6868
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
69+
<PackageReference Include="Redis.OM" Version="0.6.1" />
6970
<PackageReference Include="System.Text.Json" Version="6.0.6" />
7071
</ItemGroup>
7172

Source/BSN.Commons/Infrastructure/IDatabaseFactory.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

33
namespace BSN.Commons.Infrastructure
44
{
5+
/// <summary>
6+
/// Interface for Database Factory
7+
/// </summary>
58
public interface IDatabaseFactory : IDisposable
69
{
10+
/// <summary>
11+
/// Get the database context
12+
/// </summary>
13+
/// <returns></returns>
714
IDbContext Get();
815
}
916
}

Source/BSN.Commons/Infrastructure/IDbContext.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

33
namespace BSN.Commons.Infrastructure
44
{
5-
public interface IDbContext: IDisposable
6-
{
7-
int SaveChanges();
8-
}
5+
/// <summary>
6+
/// Interface for Database Context
7+
/// </summary>
8+
public interface IDbContext : IDisposable
9+
{
10+
/// <summary>
11+
/// Save changes to the database
12+
/// </summary>
13+
/// <returns></returns>
14+
int SaveChanges();
15+
}
916
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Redis.OM.Contracts;
2+
3+
namespace BSN.Commons.Infrastructure.Redis
4+
{
5+
/// <summary>
6+
/// Interface for Redis Database Context
7+
/// </summary>
8+
public interface IRedisDbContext : IDbContext
9+
{
10+
/// <summary>
11+
/// Get the connection provider for Redis
12+
/// </summary>
13+
IRedisConnectionProvider GetConnectionProvider();
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BSN.Commons.Infrastructure.Redis
2+
{
3+
/// <summary>
4+
/// Redis Connection Options
5+
/// </summary>
6+
public class RedisConnectionOptions
7+
{
8+
/// <summary>
9+
/// Connection String to connect to Redis for example: localhost:6379
10+
/// </summary>
11+
public string ConnectionString { get; set; }
12+
}
13+
}

0 commit comments

Comments
 (0)