Skip to content

Commit a3e5eee

Browse files
authoredOct 4, 2024··
feat: added the Echo Store. (#807)
1 parent a221f31 commit a3e5eee

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
 

‎docs/Stores.md

+18
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,21 @@ services.AddMultiTenant<TenantInfo>()
272272
services.AddMultiTenant<TenantInfo>()
273273
.WithDistributedCacheStore(TimeSpan.FromMinutes(5));
274274
```
275+
276+
## Echo Store
277+
278+
> NuGet package: Finbuckle.MultiTenant
279+
280+
The Echo Store serves as a simple, read-only store that directly returns a new tenant instance based on the given identifier
281+
without any additional settings. It's particularly suited for applications that require a simple, immediate method for tenant identification without the need for persistence, such as during testing phases or in environments where tenant information is static and predefined elsewhere.
282+
283+
284+
This store is read-only and calls to `TryAddAsync`, `TryUpdateAsync`, and `TryRemoveAsync` will throw
285+
a `NotImplementedException`. Because no stores are saved, a call to `GetAllAsync` will also throw an Exception.
286+
287+
Configure by calling `WithEchoStore` after `AddMultiTenant<TTenantInfo>`.
288+
289+
```csharp
290+
services.AddMultiTenant<TenantInfo>()
291+
.WithEchoStore();
292+
```

‎src/Finbuckle.MultiTenant/DependencyInjection/MultiTenantBuilderExtensions.cs

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Finbuckle.MultiTenant.Internal;
66
using Finbuckle.MultiTenant.Stores.ConfigurationStore;
77
using Finbuckle.MultiTenant.Stores.DistributedCacheStore;
8+
using Finbuckle.MultiTenant.Stores.EchoStore;
89
using Finbuckle.MultiTenant.Stores.HttpRemoteStore;
910
using Finbuckle.MultiTenant.Stores.InMemoryStore;
1011
using Finbuckle.MultiTenant.Strategies;
@@ -116,6 +117,14 @@ public static MultiTenantBuilder<TTenantInfo> WithInMemoryStore<TTenantInfo>(thi
116117

117118
return builder.WithStore<InMemoryStore<TTenantInfo>>(ServiceLifetime.Singleton);
118119
}
120+
121+
/// <summary>
122+
/// Adds an EchoStore to the application.
123+
/// </summary>
124+
/// <param name="builder">The builder instance.</param>
125+
public static MultiTenantBuilder<TTenantInfo> WithEchoStore<TTenantInfo>(this MultiTenantBuilder<TTenantInfo> builder)
126+
where TTenantInfo : class, ITenantInfo, new()
127+
=> builder.WithStore<EchoStore<TTenantInfo>>(ServiceLifetime.Singleton);
119128

120129
/// <summary>
121130
/// Adds and configures a StaticStrategy to the application.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright Finbuckle LLC, Andrew White, and Contributors.
2+
// Refer to the solution LICENSE file for more information.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
using Finbuckle.MultiTenant.Abstractions;
7+
8+
namespace Finbuckle.MultiTenant.Stores.EchoStore;
9+
10+
/// <summary>
11+
/// Basic store that simply returns a tenant based on the identifier without any additional settings.
12+
/// Note that add, update, and remove functionality is not implemented.
13+
/// If underlying configuration supports reload-on-change then this store will reflect such changes.
14+
/// </summary>
15+
/// <typeparam name="TTenantInfo">The ITenantInfo implementation type.</typeparam>
16+
public class EchoStore<TTenantInfo> : IMultiTenantStore<TTenantInfo> where TTenantInfo : class, ITenantInfo, new()
17+
{
18+
/// <inheritdoc />
19+
public async Task<TTenantInfo?> TryGetByIdentifierAsync(string identifier)
20+
{
21+
return await Task.FromResult(new TTenantInfo { Id = identifier, Identifier = identifier });
22+
}
23+
24+
/// <inheritdoc />
25+
public async Task<TTenantInfo?> TryGetAsync(string id)
26+
{
27+
return await Task.FromResult(new TTenantInfo { Id = id, Identifier = id });
28+
}
29+
30+
/// <summary>
31+
/// Not implemented in this implementation.
32+
/// </summary>
33+
/// <exception cref="NotImplementedException"></exception>
34+
public Task<bool> TryAddAsync(TTenantInfo tenantInfo)
35+
{
36+
throw new NotImplementedException();
37+
}
38+
39+
/// <summary>
40+
/// Not implemented in this implementation.
41+
/// </summary>
42+
/// <exception cref="NotImplementedException"></exception>
43+
public Task<bool> TryUpdateAsync(TTenantInfo tenantInfo)
44+
{
45+
throw new NotImplementedException();
46+
}
47+
48+
/// <summary>
49+
/// Not implemented in this implementation.
50+
/// </summary>
51+
/// <exception cref="NotImplementedException"></exception>
52+
public Task<bool> TryRemoveAsync(string identifier)
53+
{
54+
throw new NotImplementedException();
55+
}
56+
57+
/// <summary>
58+
/// Not implemented in this implementation.
59+
/// </summary>
60+
/// <exception cref="NotImplementedException"></exception>
61+
public Task<IEnumerable<TTenantInfo>> GetAllAsync()
62+
{
63+
throw new NotImplementedException();
64+
}
65+
}

‎test/Finbuckle.MultiTenant.Test/DependencyInjection/MultiTenantBuilderExtensionsShould.cs

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Finbuckle.MultiTenant.Abstractions;
55
using Finbuckle.MultiTenant.Stores.ConfigurationStore;
66
using Finbuckle.MultiTenant.Stores.DistributedCacheStore;
7+
using Finbuckle.MultiTenant.Stores.EchoStore;
78
using Finbuckle.MultiTenant.Stores.HttpRemoteStore;
89
using Finbuckle.MultiTenant.Stores.InMemoryStore;
910
using Finbuckle.MultiTenant.Strategies;
@@ -157,6 +158,26 @@ public async Task AddInMemoryStoreWithCaseSensitivity()
157158
Assert.Null(tc);
158159
}
159160

161+
[Fact]
162+
public async Task AddEchoStore()
163+
{
164+
var services = new ServiceCollection();
165+
var builder = new MultiTenantBuilder<TenantInfo>(services);
166+
builder.WithEchoStore();
167+
var sp = services.BuildServiceProvider();
168+
169+
var store = sp.GetRequiredService<IMultiTenantStore<TenantInfo>>();
170+
Assert.IsType<EchoStore<TenantInfo>>(store);
171+
172+
var tc = await store.TryGetByIdentifierAsync("initech");
173+
Assert.Equal("initech", tc!.Id);
174+
Assert.Equal("initech", tc.Identifier);
175+
176+
tc = await store.TryGetByIdentifierAsync("lol");
177+
Assert.Equal("lol", tc!.Id);
178+
Assert.Equal("lol", tc.Identifier);
179+
}
180+
160181
[Fact]
161182
public void AddDelegateStrategy()
162183
{

0 commit comments

Comments
 (0)
Please sign in to comment.