-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Reflection Insert config bson from Basket infra
- Loading branch information
1 parent
fea8da0
commit 7d1774b
Showing
13 changed files
with
151 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
crs/Services/Basket/Basket.Persistence/DbContexts/Abstractions/IMongoDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
60 changes: 60 additions & 0 deletions
60
crs/Services/Basket/Basket.Persistence/DbContexts/MongoDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>); | ||
} |
7 changes: 7 additions & 0 deletions
7
crs/Services/Basket/Basket.Persistence/DbContexts/MongoDbContextOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
8 changes: 8 additions & 0 deletions
8
crs/Services/Basket/Basket.Persistence/MapConfigurations/Abstractions/IMapConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
29 changes: 29 additions & 0 deletions
29
crs/Services/Basket/Basket.Persistence/MapConfigurations/CatalogBasketMapConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
5 changes: 0 additions & 5 deletions
5
crs/Services/Basket/Basket.Persistence/Services/BasketCachedService.cs
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
crs/Services/Basket/Basket.Persistence/Services/ICachedService.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |