Skip to content

Commit bf3506a

Browse files
authored
Merge pull request #127 from neuroglia-io/fix-serialization
Fix the registration of serialization services to make sure not to add already added services
2 parents d5d0b0a + f5f784e commit bf3506a

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/Neuroglia.Data.Infrastructure.Memory/Services/MemoryCacheRepository.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public override Task<TEntity> AddAsync(TEntity entity, CancellationToken cancell
4747
ArgumentNullException.ThrowIfNull(entity);
4848
var collectionKey = this.BuildCacheKey();
4949
var entityKey = this.BuildCacheKey(entity.Id);
50-
if (this.Cache.TryGetValue(collectionKey, out List<string>? keys) && keys != null && keys.Contains(entityKey)) throw new Exception($"An entity with the specified id '{entity.Id}' already exists in the repository");
50+
if (this.Cache.TryGetValue(collectionKey, out List<string>? keys) && keys != null)
51+
{
52+
if (keys.Contains(entityKey)) throw new Exception($"An entity with the specified id '{entity.Id}' already exists in the repository");
53+
}
5154
else keys = [];
5255
keys.Add(entityKey);
5356
this.Cache.Set(collectionKey, keys);

src/Neuroglia.Serialization.Abstractions/Extensions/IServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static IServiceCollection AddSerializer<TSerializer>(this IServiceCollect
4747
where TSerializer : class, ISerializer
4848
{
4949
services.TryAdd(new ServiceDescriptor(typeof(TSerializer), typeof(TSerializer), lifetime));
50-
services.Add(new ServiceDescriptor(typeof(ISerializer), typeof(TSerializer), lifetime));
50+
if (!services.Any(s => s.ServiceType == typeof(ISerializer) && s.ImplementationType == typeof(TSerializer))) services.Add(new ServiceDescriptor(typeof(ISerializer), typeof(TSerializer), lifetime));
5151
return services;
5252
}
5353

@@ -63,7 +63,7 @@ public static IServiceCollection AddJsonSerializer(this IServiceCollection servi
6363
setup ??= JsonSerializer.DefaultOptionsConfiguration!;
6464
services.Configure(setup);
6565
services.AddSerializer<JsonSerializer>(lifetime);
66-
services.Add(new ServiceDescriptor(typeof(IJsonSerializer), typeof(JsonSerializer), lifetime));
66+
services.TryAdd(new ServiceDescriptor(typeof(IJsonSerializer), typeof(JsonSerializer), lifetime));
6767
return services;
6868
}
6969

0 commit comments

Comments
 (0)