Skip to content

Commit

Permalink
feat: Mongo Logger Factory, Conditional LinqProviderV2/V3, fixes, pac…
Browse files Browse the repository at this point in the history
…kage updates (#317)
  • Loading branch information
JacobAtchley authored Apr 25, 2024
1 parent 51602f0 commit b78051d
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Firebend.AutoCrud.Core/Firebend.AutoCrud.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.3.4" />
<PackageReference Include="AsyncKeyedLock" Version="6.4.2" />
<PackageReference Include="Firebend.JsonPatchGenerator" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private class QueuedTransaction
private readonly IServiceProvider _serviceProvider;

private readonly ConcurrentDictionary<string, Lazy<Task<IEntityTransaction>>> _sharedTransactions = new();
private readonly ConcurrentBag<QueuedTransaction> _transactions = new();
private readonly ConcurrentBag<QueuedTransaction> _transactions = [];

public bool TransactionStarted { get; private set; }
public ImmutableList<Guid> TransactionIds => GetTransactionsInOrder(true).Select(x => x.Id).ToImmutableList();
Expand Down
2 changes: 1 addition & 1 deletion Firebend.AutoCrud.Core/Models/Entities/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Result<TModel> : Result

public class ModelStateResult<TModel> : Result<TModel>
{
private readonly List<ModelError> _errors = new List<ModelError>();
private readonly List<ModelError> _errors = [];
public IReadOnlyList<ModelError> Errors => _errors;

public ModelStateResult<TModel> AddError(string path, string error)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Firebend.AutoCrud.Core.Extensions;
Expand Down Expand Up @@ -72,7 +73,14 @@ public async Task<CustomFieldsEntity<TKey>> UpdateAsync(TKey rootEntityKey,
& Builders<TEntity>.Filter.ElemMatch(x => x.CustomFields, cf => cf.Id == customField.Id);

var mongoCollection = await GetCollectionAsync();
var updateDefinition = Builders<TEntity>.Update.Set(x => x.CustomFields.FirstMatchingElement(), customField);

var linqVersion = LinqProvider.GetValueOrDefault(MongoDB.Driver.Linq.LinqProvider.V3);

Expression<Func<TEntity, CustomFieldsEntity<TKey>>> filterExpression = linqVersion == MongoDB.Driver.Linq.LinqProvider.V2
? x => x.CustomFields[-1]
: x => x.CustomFields.FirstMatchingElement();

var updateDefinition = Builders<TEntity>.Update.Set(filterExpression, customField);

if (typeof(IModifiedEntity).IsAssignableFrom(typeof(TEntity)))
{
Expand Down Expand Up @@ -178,7 +186,7 @@ public async Task<CustomFieldsEntity<TKey>> PatchAsync(TKey rootEntityKey,
new FindOneAndUpdateOptions<TEntity>
{
ReturnDocument = ReturnDocument.Before,
ArrayFilters = new[] { arrayFilters },
ArrayFilters = [arrayFilters],
},
cancellationToken);
}
Expand All @@ -189,7 +197,7 @@ public async Task<CustomFieldsEntity<TKey>> PatchAsync(TKey rootEntityKey,
new FindOneAndUpdateOptions<TEntity>
{
ReturnDocument = ReturnDocument.Before,
ArrayFilters = new[] { arrayFilters },
ArrayFilters = [arrayFilters],
},
cancellationToken);
}
Expand Down Expand Up @@ -221,7 +229,7 @@ public async Task<CustomFieldsEntity<TKey>> PatchAsync(TKey rootEntityKey,
return entity.CustomFields?.FirstOrDefault(x => x.Id == key);
}

private static string FixMongoPatchPath(Operation<CustomFieldsEntity<TKey>> operation)
private static string FixMongoPatchPath(OperationBase operation)
=> operation.path[1..].FirstCharToLower();

private Task PublishUpdatedDomainEventAsync(TEntity previous,
Expand All @@ -242,6 +250,5 @@ private Task PublishUpdatedDomainEventAsync(TEntity previous,
};

return _domainEventPublisher.PublishEntityUpdatedEventAsync(domainEvent, entityTransaction, cancellationToken);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using MassTransit;

namespace Firebend.AutoCrud.DomainEvents.MassTransit.Extensions;

public record AutoCrudMassTransitConfigureReceiveEndpointContext(
IReceiveEndpointConfigurator EndpointConfigurator,
string QueueName,
Type ConsumerType,
Type MessageType
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

namespace Firebend.AutoCrud.DomainEvents.MassTransit.Extensions;

public class AutoCrudMassTransitConsumerInfo
public record AutoCrudMassTransitConsumerInfo
{
public Type EntityType { get; set; }
public Type EntityType { get; }

public Type DomainEventType { get; set; }
public Type DomainEventType { get; }

public Type ConsumerType { get; set; }
public Type ConsumerType { get; }

public string EntityActionDescription { get; set; }
public string EntityActionDescription { get; }

public ServiceDescriptor ServiceDescriptor { get; set; }
public ServiceDescriptor ServiceDescriptor { get; }

public Type MessageType => ServiceDescriptor?.ServiceType.GenericTypeArguments.FirstOrDefault() ?? ServiceDescriptor?.ServiceType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace Firebend.AutoCrud.DomainEvents.MassTransit.Extensions;

public static class MassTransitExtensions
{
private const string QueuePrefix = "FB_AC_DV_";
private const string QueuePrefix = "FB_AC_DE_";

private static List<AutoCrudMassTransitConsumerInfo> _listeners;

private static List<AutoCrudMassTransitConsumerInfo> GetListeners(IServiceCollection serviceCollection)
public static List<AutoCrudMassTransitConsumerInfo> GetListeners(IServiceCollection serviceCollection)
{
if (_listeners != null && _listeners.Count != 0)
{
Expand Down Expand Up @@ -48,7 +48,7 @@ public static void RegisterFirebendAutoCrudDomainEventHandlerEndPoints(
IBusFactoryConfigurator bus,
AutoCrudMassTransitQueueMode queueMode,
string receiveEndpointPrefix = null,
Action<IReceiveEndpointConfigurator> configureReceiveEndpoint = null)
Action<AutoCrudMassTransitConfigureReceiveEndpointContext> configureReceiveEndpoint = null)
{
var queues = GetQueues(queueMode, receiveEndpointPrefix, _listeners);

Expand All @@ -59,12 +59,28 @@ public static void RegisterFirebendAutoCrudDomainEventHandlerEndPoints(
foreach (var consumerInfo in consumerInfos)
{
busRegistrationContext.ConfigureConsumer(consumerInfo.ConsumerType, re);

if (configureReceiveEndpoint is null)
{
continue;
}

var ctx = new AutoCrudMassTransitConfigureReceiveEndpointContext(re,
queueName,
consumerInfo.ConsumerType,
consumerInfo.MessageType);

configureReceiveEndpoint.Invoke(ctx);
}
configureReceiveEndpoint?.Invoke(re);
});
}

_listeners.Clear();
ClearListeners();
}

public static void ClearListeners()
{
_listeners?.Clear();
_listeners = null;
}

Expand Down
2 changes: 1 addition & 1 deletion Firebend.AutoCrud.IntegrationTests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class BaseTest<
{
protected abstract string Url { get; }

protected List<IFlurlResponse> Responses { get; } = new();
protected List<IFlurlResponse> Responses { get; } = [];

private TKey CreatedKey { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Bogus" Version="35.5.0" />
<PackageReference Include="CsvHelper" Version="31.0.3" />
<PackageReference Include="CsvHelper" Version="31.0.4" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="Flurl.Http.Newtonsoft" Version="0.9.1" />
Expand Down
2 changes: 1 addition & 1 deletion Firebend.AutoCrud.IntegrationTests/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class TransactionTests
public async Task SeedData()
{
_efPerson = await CreatePersonAsync(EfPersonUrl);
_efPerson.CustomFields = new List<CustomFieldsEntity<Guid>>();
_efPerson.CustomFields = [];
_mongoPerson = await CreatePersonAsync(MongoPersonUrl);
_requestModel = new SessionTransactionRequestModel { EfPersonId = _efPerson.Id, MongoPersonId = _mongoPerson.Id };
}
Expand Down
2 changes: 1 addition & 1 deletion Firebend.AutoCrud.Io/Firebend.AutoCrud.Io.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.102.2" />
<PackageReference Include="CsvHelper" Version="31.0.3" />
<PackageReference Include="CsvHelper" Version="31.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Firebend.AutoCrud.Mongo/Client/Crud/MongoUpdateClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public virtual async Task<List<TEntity>> UpsertManyAsync(List<EntityUpdate<TEnti
return updatedEntities;
}

return new List<TEntity>();
return [];
}

public virtual Task<List<TOut>> UpsertManyAsync<TOut>(List<EntityUpdate<TEntity>> entities,
Expand Down Expand Up @@ -111,7 +111,7 @@ public virtual async Task<List<TOut>> UpsertManyAsync<TOut>(List<EntityUpdate<TE
return updatedEntities;
}

return new List<TOut>();
return [];
}

public virtual Task<TEntity> UpdateAsync(TKey id,
Expand Down
3 changes: 3 additions & 0 deletions Firebend.AutoCrud.Mongo/Client/MongoClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Firebend.AutoCrud.Mongo.Interfaces;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace Firebend.AutoCrud.Mongo.Client;

Expand All @@ -15,6 +16,8 @@ public abstract class MongoClientBase<TKey, TEntity> : BaseDisposable
private readonly IMongoClientFactory<TKey, TEntity> _mongoClientFactory;
private IMongoClient _mongoClient;

public LinqProvider? LinqProvider => _mongoClient?.Settings?.LinqProvider;

protected MongoClientBase(IMongoClientFactory<TKey, TEntity> clientFactory,
ILogger logger,
IMongoRetryService mongoRetryService)
Expand Down
1 change: 1 addition & 0 deletions Firebend.AutoCrud.Mongo/Client/MongoClientBaseDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public static class MongoClientBaseDefaults
#pragma warning disable CA2211, IDE1006
// ReSharper disable once FieldCanBeMadeReadOnly.Global
// ReSharper disable once InconsistentNaming
// ReSharper disable once ConvertToConstant.Global
public static int NumberOfRetries = 7;
#pragma warning restore CA2211, IDE1006
}
30 changes: 8 additions & 22 deletions Firebend.AutoCrud.Mongo/Client/MongoClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Events;
using MongoDB.Driver.Linq;

namespace Firebend.AutoCrud.Mongo.Client;
Expand All @@ -13,25 +12,24 @@ public class MongoClientFactory<TKey, TEntity> : IMongoClientFactory<TKey, TEnti
where TKey : struct
where TEntity : class, IEntity<TKey>
{
private record MongoClientCacheFactoryContext(ILogger Logger,
private record MongoClientCacheFactoryContext(ILoggerFactory LoggerFactory,
MongoClientSettings Settings,
bool EnableLogging,
IMongoClientSettingsConfigurator SettingsConfigurator);

private readonly ILogger _logger;
private readonly IMongoConnectionStringProvider<TKey, TEntity> _connectionStringProvider;
private readonly IMongoClientSettingsConfigurator _settingsConfigurator;
private readonly ILoggerFactory _loggerFactory;

public MongoClientFactory(ILogger<MongoClientFactory<TKey, TEntity>> logger,
IMongoConnectionStringProvider<TKey, TEntity> connectionStringProvider,
public MongoClientFactory(IMongoConnectionStringProvider<TKey, TEntity> connectionStringProvider,
ILoggerFactory loggerFactory,
IMongoClientSettingsConfigurator settingsConfigurator = null)
{
_logger = logger;
_connectionStringProvider = connectionStringProvider;
_loggerFactory = loggerFactory;
_settingsConfigurator = settingsConfigurator;
}

public async Task<IMongoClient> CreateClientAsync(string overrideShardKey = null, bool enableLogging = false)
public async Task<IMongoClient> CreateClientAsync(string overrideShardKey = null)
{
var connectionString = await _connectionStringProvider.GetConnectionStringAsync(overrideShardKey);

Expand All @@ -40,7 +38,7 @@ public async Task<IMongoClient> CreateClientAsync(string overrideShardKey = null
var client = MongoClientFactoryCache.MongoClients.GetOrAdd(
mongoClientSettings.Server.ToString(),
CreateClientForCache,
new MongoClientCacheFactoryContext(_logger, mongoClientSettings, enableLogging, _settingsConfigurator)
new MongoClientCacheFactoryContext(_loggerFactory, mongoClientSettings, _settingsConfigurator)
);

return client;
Expand All @@ -50,24 +48,12 @@ private static IMongoClient CreateClientForCache(string server, MongoClientCache
{
context.Settings.LinqProvider = LinqProvider.V3;

if (context.EnableLogging)
{
context.Settings.ClusterConfigurator = cb => Configurator(cb, context);
}
context.Settings.LoggingSettings ??= new LoggingSettings(context.LoggerFactory);

var settings = context.SettingsConfigurator is null
? context.Settings
: context.SettingsConfigurator.Configure(server, context.Settings);

return new MongoClient(settings);
}

private static void Configurator(ClusterBuilder cb, MongoClientCacheFactoryContext context)
{
cb.Subscribe<CommandStartedEvent>(e => MongoClientFactoryLogger.Started(context.Logger, e.CommandName, e.Command));

cb.Subscribe<CommandSucceededEvent>(e => MongoClientFactoryLogger.Success(context.Logger, e.CommandName, e.Duration, e.Reply));

cb.Subscribe<CommandFailedEvent>(e => MongoClientFactoryLogger.Failed(context.Logger, e.CommandName, e.Duration));
}
}
17 changes: 0 additions & 17 deletions Firebend.AutoCrud.Mongo/Client/MongoClientFactoryLogger.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Firebend.AutoCrud.Mongo/Interfaces/IMongoClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IMongoClientFactory<TKey, TEntity>
where TKey : struct
where TEntity : class, IEntity<TKey>
{
Task<IMongoClient> CreateClientAsync(string overrideShardKey = null, bool enableLogging = false);
Task<IMongoClient> CreateClientAsync(string overrideShardKey = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ClientRequestTransactionManagerTests
public void TestSetup()
{
_fixture = new Fixture().Customize(new AutoMoqCustomization());
_transactions = new List<Mock<TestTransaction>>();
_transactions = [];

_efTransactionFactory = _fixture.Create<Mock<IEntityTransactionFactory<Guid, TestClassEf>>>();
_mongoTransactionFactory = _fixture.Create<Mock<IEntityTransactionFactory<Guid, TestClassMongo>>>();
Expand Down
1 change: 1 addition & 0 deletions Firebend.AutoCrud.Web.Sample/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"LogLevel": {
"Default": "Warning",
"MongoDB": "Warning",
"MongoDb.Command": "Warning",
"Microsoft": "Warning",
"Microsoft.AspNetCore.Hosting.Diagnostics": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CopyOnPatchPropertyAccessor<TEntity, TVersion, TViewModel> : ICopyO

public CopyOnPatchPropertyAccessor(List<string> copyOnPatchPropertyNames)
{
copyOnPatchPropertyNames ??= new List<string>();
copyOnPatchPropertyNames ??= [];
copyOnPatchPropertyNames.Add(nameof(ICustomFieldsEntity<Guid>.CustomFields));
_copyOnPatchPropertyNames = copyOnPatchPropertyNames;
}
Expand Down

0 comments on commit b78051d

Please sign in to comment.