-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathDefaultCosmosItemConfigurationProvider.cs
60 lines (49 loc) · 2.6 KB
/
DefaultCosmosItemConfigurationProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Azure.CosmosRepository.Providers;
class DefaultCosmosItemConfigurationProvider(
ICosmosContainerNameProvider containerNameProvider,
ICosmosPartitionKeyPathProvider cosmosPartitionKeyPathProvider,
ICosmosUniqueKeyPolicyProvider cosmosUniqueKeyPolicyProvider,
ICosmosContainerDefaultTimeToLiveProvider containerDefaultTimeToLiveProvider,
ICosmosContainerSyncContainerPropertiesProvider syncContainerPropertiesProvider,
ICosmosThroughputProvider cosmosThroughputProvider,
ICosmosStrictTypeCheckingProvider cosmosStrictTypeCheckingProvider) : ICosmosItemConfigurationProvider
{
private static readonly ConcurrentDictionary<Type, ItemConfiguration> _itemOptionsMap = new();
public ItemConfiguration GetItemConfiguration<TItem>() where TItem : IItem =>
GetItemConfiguration(typeof(TItem));
public ItemConfiguration GetItemConfiguration(Type itemType) =>
_itemOptionsMap.GetOrAdd(itemType, AddOptions(itemType));
public List<ItemConfiguration> GetAllItemConfigurations(params Assembly[]? assemblies)
{
IEnumerable<Type> itemTypes = (assemblies ?? AppDomain.CurrentDomain.GetAssemblies())
.SelectMany(s => s.GetTypes())
.Where(p => typeof(IItem).IsAssignableFrom(p) && p is {IsInterface: false, IsAbstract: false});
foreach (Type itemType in itemTypes)
{
_itemOptionsMap.GetOrAdd(itemType, AddOptions(itemType));
}
return _itemOptionsMap.Select(i => i.Value).ToList();
}
private ItemConfiguration AddOptions(Type itemType)
{
itemType.IsItem();
var containerName = containerNameProvider.GetContainerName(itemType);
var partitionKeyPaths = cosmosPartitionKeyPathProvider.GetPartitionKeyPaths(itemType);
UniqueKeyPolicy? uniqueKeyPolicy = cosmosUniqueKeyPolicyProvider.GetUniqueKeyPolicy(itemType);
var timeToLive = containerDefaultTimeToLiveProvider.GetDefaultTimeToLive(itemType);
var sync = syncContainerPropertiesProvider.GetWhetherToSyncContainerProperties(itemType);
ThroughputProperties? throughputProperties = cosmosThroughputProvider.GetThroughputProperties(itemType);
var useStrictTypeChecking = cosmosStrictTypeCheckingProvider.UseStrictTypeChecking(itemType);
return new(
itemType,
containerName,
partitionKeyPaths,
uniqueKeyPolicy,
throughputProperties,
timeToLive,
sync,
useStrictTypeChecking: useStrictTypeChecking);
}
}