Skip to content

Commit

Permalink
Add Reflection Insert config bson from Basket infra
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sharifov committed Feb 28, 2024
1 parent fea8da0 commit 7d1774b
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 66 deletions.
20 changes: 20 additions & 0 deletions crs/Docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,23 @@ services:
networks:
web_services_network:
driver: bridge


# eventstore:
# container_name: eventstore
# image: eventstore/eventstore:21.2.0-buster-slim
# restart: unless-stopped
# environment:
# - EVENTSTORE_CLUSTER_SIZE=1
# - EVENTSTORE_RUN_PROJECTIONS=All
# - EVENTSTORE_START_STANDARD_PROJECTIONS=true
# - EVENTSTORE_EXT_TCP_PORT=1113
# - EVENTSTORE_EXT_HTTP_PORT=2113
# - EVENTSTORE_INSECURE=true
# - EVENTSTORE_ENABLE_EXTERNAL_TCP=true
# - EVENTSTORE_ENABLE_ATOM_PUB_OVER_HTTP=true
# ports:
# - '1113:1113'
# - '2113:2113'
# networks:
# - booking
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public Result SetQuantity(int quantity)

var quantityResult = Quantity.Create(quantity);

if(quantityResult.IsFailure)
if (quantityResult.IsFailure)
{
return Result.Failure(quantityResult.Error);
}

return Result.Success();
}

public Result AddQuantity(int quantity) =>
SetQuantity(Quantity.Value + quantity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Folder Include="Abstractions\" />
<Folder Include="Configurations\" />
<Folder Include="DbContext\" />
<Folder Include="Options\" />
<Folder Include="Services\Abstractions\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="7.0.0-preview.1" />
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 0 additions & 24 deletions crs/Services/Basket/Basket.Persistence/BasketDbContext.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Basket.Persistence.DbContexts.Abstractions;

public interface IMongoDbContext
{
void AddCommand(Func<Task> func);
Task<int> CommitAsync(CancellationToken cancellationToken = default);
IMongoCollection<T> GetCollection<T>(string name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Basket.Persistence.DbContexts.Abstractions;
using Common.Extensions;
using MongoDB.Bson.Serialization;

namespace Basket.Persistence.DbContexts;

public class MongoDbContext : IMongoDbContext
{
private readonly IMongoDatabase _database;
private readonly MongoClient _mongoClient;
private readonly List<Func<Task>> _commands;

public MongoDbContext(IOptions<MongoDbContextOptions> options)
{
var mongoDbContextOptions = options.Value;

_mongoClient = new MongoClient(mongoDbContextOptions.ConnectionString);
_database = _mongoClient.GetDatabase(mongoDbContextOptions.ConnectionString);
_commands = [];
}

public async Task<int> CommitAsync(CancellationToken cancellationToken = default)
{
using var session = await _mongoClient.StartSessionAsync(cancellationToken: cancellationToken);
session.StartTransaction();

var commandTasks = _commands.Select(c => c());

await Task.WhenAll(commandTasks);
await session.CommitTransactionAsync(cancellationToken);

return _commands.Count;
}

public IMongoCollection<T> GetCollection<T>(string name) => _database.GetCollection<T>(name);

public void AddCommand(Func<Task> func) => _commands.Add(func);


public static void ConfigureMapFromAssembly(Assembly assembly) =>
assembly.DefinedTypes.Where(IsMapConfiguration)
.Foreach(mapConfigurationType => mapConfigurationType.GetInterfaces()
.Where(IsMapConfigurationGeneric)
.Foreach(interfaceMapConfigurationType =>
{
var interfaceMapConfigurationTypeGenericArgument = interfaceMapConfigurationType.GetGenericArguments()[0];
var bsonClassMapType = typeof(BsonClassMap<>).MakeGenericType(interfaceMapConfigurationTypeGenericArgument);
var bsonClassMapObject = Activator.CreateInstance(bsonClassMapType);
var configureMethod = interfaceMapConfigurationType.GetMethod("Configure");
configureMethod!.Invoke(Activator.CreateInstance(mapConfigurationType), [bsonClassMapObject]);
}));

private static bool IsMapConfiguration(Type type) =>
!type.IsInterface &&
!type.IsAbstract &&
type.GetInterfaces().Any(i => i.GetGenericTypeDefinition() == typeof(IMapConfiguration<>));

private static bool IsMapConfigurationGeneric(Type type) =>
type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IMapConfiguration<>);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Basket.Persistence.DbContexts;

public sealed class MongoDbContextOptions
{
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
14 changes: 6 additions & 8 deletions crs/Services/Basket/Basket.Persistence/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
global using Common.Domain.Primitives;
global using System.Reflection;
global using Common.Extensions;
global using System.Reflection;
global using Basket.Domain.CatalogBasketAggregate.Repositories;
global using Basket.Domain.CatalogBasketAggregate;
global using Common.Domain.Primitives.Events;
global using Basket.Domain.CatalogBasketAggregate.Ids;
global using Microsoft.EntityFrameworkCore;
global using Basket.Domain.CatalogBasketAggregate.Entities;
global using MongoDB.EntityFrameworkCore.Extensions;
global using MongoDB.Driver;
global using Microsoft.Extensions.Options;
global using Common.Domain.Primitives;
global using Basket.Persistence.MapConfigurations.Abstractions;
global using Basket.Domain.CatalogBasketAggregate;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MongoDB.Bson.Serialization;

namespace Basket.Persistence.MapConfigurations.Abstractions;

public interface IMapConfiguration<TEntity>
{
public void Configure(BsonClassMap<TEntity> bsonClassMap);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using MongoDB.Bson.Serialization;

namespace Basket.Persistence.MapConfigurations;

internal sealed class CatalogBasketMapConfiguration : IMapConfiguration<CatalogBasket>
{
//public void Configure()
//{
// BsonClassMap.RegisterClassMap<CatalogBasket>(map =>
// {
// map.AutoMap();
// map.SetIgnoreExtraElements(true);
// map.MapIdProperty(x => x.Id);
// map.MapProperty(x => x.BasketId).SetElementName("BasketId");
// map.MapProperty(x => x.ProductId).SetElementName("ProductId");
// map.MapProperty(x => x.Quantity).SetElementName("Quantity");
// });
//}

public void Configure(BsonClassMap<CatalogBasket> map)
{
map.AutoMap();
map.SetIgnoreExtraElements(true);
map.MapIdProperty(x => x.Id);
map.MapProperty(x => x.BasketId).SetElementName("BasketId");
map.MapProperty(x => x.ProductId).SetElementName("ProductId");
map.MapProperty(x => x.Quantity).SetElementName("Quantity");
}
}

This file was deleted.

12 changes: 0 additions & 12 deletions crs/Services/Basket/Basket.Persistence/Services/ICachedService.cs

This file was deleted.

16 changes: 9 additions & 7 deletions crs/Services/Basket/Basket.Persistence/UnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace Basket.Persistence;
using Basket.Persistence.DbContexts.Abstractions;

internal sealed class UnitOfWork(BasketDbContext dbContext) : IUnitOfWork
namespace Basket.Persistence;

public sealed class UnitOfWork(IMongoDbContext mongoDbContext) : IUnitOfWork
{
private readonly BasketDbContext _dbContext = dbContext;
private readonly IMongoDbContext _mongoDbContext = mongoDbContext;

public int Commit() =>
_dbContext.SaveChanges();
public int Commit() =>
_mongoDbContext.CommitAsync().Result;

public async Task<int> CommitAsync(CancellationToken cancellationToken = default) =>
await _dbContext.SaveChangesAsync(cancellationToken);
public async Task<int> CommitAsync(CancellationToken cancellationToken = default) =>
await _mongoDbContext.CommitAsync(cancellationToken);
}

0 comments on commit 7d1774b

Please sign in to comment.