Skip to content

Commit

Permalink
Merge pull request #34 from iokode/command-improvements
Browse files Browse the repository at this point in the history
Command improvements
  • Loading branch information
montyclt authored Sep 17, 2024
2 parents 1e72cca + dfee608 commit fa14451
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 233 deletions.
12 changes: 12 additions & 0 deletions src/AspNetCoreIntegration/AspNetCoreIntegration.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../MSBuild/Base.props"/>
<Import Project="../MSBuild/Packable.props"/>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
</ItemGroup>

<ItemGroup Label="Project references">
<ProjectReference Include="..\Foundation\Foundation.csproj"/>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions src/Bootstrapping/Bootstrapping.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<Import Project="../MSBuild/Base.props"/>
<Import Project="../MSBuild/Packable.props"/>

<PropertyGroup>
<TargetType>Library</TargetType>
</PropertyGroup>

<ItemGroup Label="Package references">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0"/>
</ItemGroup>
Expand Down
9 changes: 0 additions & 9 deletions src/Bootstrapping/IOpinionatedServiceCollection.cs

This file was deleted.

8 changes: 7 additions & 1 deletion src/Bootstrapping/OpinionatedServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

namespace IOKode.OpinionatedFramework.Bootstrapping;

public class OpinionatedServiceCollection : ServiceCollection, IOpinionatedServiceCollection
/// <summary>
/// This interface extends IServiceCollection with the intention of use the typing system to enforce that
/// framework services cannot be added into another container.
/// </summary>
public interface IOpinionatedServiceCollection : IServiceCollection;

internal class OpinionatedServiceCollection : ServiceCollection, IOpinionatedServiceCollection
{
public static OpinionatedServiceCollection Copy(IServiceCollection services)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace IOKode.OpinionatedFramework.ContractImplementations.CommandExecutor;
public class CommandExecutor : ICommandExecutor
{
private readonly CommandMiddleware[] middlewares;
private Dictionary<string, object?>? sharedData;
private readonly Dictionary<string, object?> sharedData;

public CommandExecutor(CommandMiddleware[] middlewares,
IEnumerable<KeyValuePair<string, object?>>? sharedData = null)
Expand Down Expand Up @@ -83,7 +83,7 @@ private async Task PrepareAndExecuteAsync<TCommand>(TCommand command, SettableCo
}

context.SetAsExecuted();
this.sharedData = context.SharedData;
// No need to set the shared data again because it uses the same reference.
}

/// <summary>
Expand All @@ -109,7 +109,7 @@ private async Task<TResult> PrepareAndExecuteAsync<TCommand, TResult>(TCommand c

context.SetAsExecuted();
context.SetResult(result);
this.sharedData = context.SharedData;
// No need to set the shared data again because it uses the same reference.

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using IOKode.OpinionatedFramework.Commands;

namespace IOKode.OpinionatedFramework.ContractImplementations.CommandExecutor;

internal class DictionarySharedDataAccessor : ISharedDataAccessor
{
internal readonly Dictionary<string, object?> dict;

public DictionarySharedDataAccessor(Dictionary<string, object?> dict)
{
this.dict = dict;
}

public bool Exists(string key)
{
return this.dict.ContainsKey(key);
}

public object? Get(string key)
{
return this.dict[key];
}

public object? GetOrDefault(string key)
{
return this.dict.GetValueOrDefault(key);
}

public void Set(string key, object? value)
{
this.dict[key] = value;
}

public void Remove(string key)
{
this.dict.Remove(key);
}

public IReadOnlyDictionary<string, object?> ToReadonlyDictionary()
{
return this.dict;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using IOKode.OpinionatedFramework.Commands;

Expand All @@ -10,17 +9,23 @@ internal class SettableCommandContext : ICommandContext
{
private Dictionary<string, object?> sharedData = null!;
private readonly Dictionary<string, object?> pipelineData = new();
private readonly ISharedDataAccessor pipelineDataAccessor;
private ISharedDataAccessor sharedDataAccessor;

internal Dictionary<string, object?> SharedData => this.sharedData;
public ISharedDataAccessor SharedData => sharedDataAccessor;
public ISharedDataAccessor PipelineData => pipelineDataAccessor;

public Type CommandType { get; private init; } = null!;
public CancellationToken CancellationToken { get; set; }
public bool IsExecuted { get; private set; }
public bool HasResult { get; private set; }
public object? Result { get; private set; }

private SettableCommandContext()
private SettableCommandContext(Dictionary<string, object?> initialSharedData)
{
this.sharedData = initialSharedData;
this.sharedDataAccessor = new DictionarySharedDataAccessor(this.sharedData);
this.pipelineDataAccessor = new DictionarySharedDataAccessor(this.pipelineData);
}

public void SetAsExecuted() => IsExecuted = true;
Expand All @@ -31,69 +36,22 @@ public void SetResult(object? result)
Result = result;
}

public static SettableCommandContext Create(Type commandType, IEnumerable<KeyValuePair<string, object?>>? initialSharedData,
public static SettableCommandContext Create(Type commandType, Dictionary<string, object?> initialSharedData,
CancellationToken cancellationToken)
{
var context = new SettableCommandContext
var context = new SettableCommandContext(initialSharedData)
{
CommandType = commandType,
CancellationToken = cancellationToken,
sharedData = initialSharedData?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) ?? new Dictionary<string, object?>()
};

return context;
}

public bool ExistsInSharedData(string key)
{
bool exists = this.sharedData.ContainsKey(key);
return exists;
}

public object? GetFromSharedData(string key)
{
object? value = this.sharedData[key];
return value;
}

public object? GetFromSharedDataOrDefault(string key)
{
object? value = this.sharedData.GetValueOrDefault(key);
return value;
}

public void SetInSharedData(string key, object? value)
{
this.sharedData[key] = value;
}
context.SetSharedDataAccessor();

public void RemoveFromSharedData(string key)
{
this.sharedData.Remove(key);
}

public bool ExistsInPipelineData(string key)
{
return this.pipelineData.ContainsKey(key);
}

public object? GetFromPipelineData(string key)
{
return this.pipelineData[key];
}

public object? GetFromPipelineDataOrDefault(string key)
{
return this.pipelineData.GetValueOrDefault(key);
}

public void SetInPipelineData(string key, object? value)
{
this.pipelineData[key] = value;
return context;
}

public void RemoveFromPipelineData(string key)
private void SetSharedDataAccessor()
{
this.pipelineData.Remove(key);
this.sharedDataAccessor = new DictionarySharedDataAccessor(this.sharedData);
}
}
6 changes: 4 additions & 2 deletions src/ContractImplementations.LoggerEmail/LoggerEmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Threading.Tasks;
using IOKode.OpinionatedFramework.Emailing;
using IOKode.OpinionatedFramework.Facades;
using IOKode.OpinionatedFramework.Logging;
using Microsoft.Extensions.Logging;
using Email = IOKode.OpinionatedFramework.Emailing.Email;

namespace IOKode.OpinionatedFramework.ContractImplementations.LoggerEmail;

public class LoggerEmailSender : IEmailSender
public class LoggerEmailSender(ILogging log) : IEmailSender
{
public Task SendAsync(Email email, CancellationToken cancellationToken)
{
Expand All @@ -20,7 +22,7 @@ public Task SendAsync(Email email, CancellationToken cancellationToken)
{textContent}
""";

Log.Info(logTemplate, email.From, email.To, email.To.Count - 1, email.Subject, email.Attachments.Count, email.TextContent);
log.Info(logTemplate, email.From, email.To, email.To.Count - 1, email.Subject, email.Attachments.Count, email.TextContent);
return Task.CompletedTask;
}
}
10 changes: 2 additions & 8 deletions src/ContractImplementations.MicrosoftLogging/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@

namespace IOKode.OpinionatedFramework.ContractImplementations.MicrosoftLogging;

public class Logging : ILogging
public class Logging(ILoggerFactory loggerFactory) : ILogging
{
private readonly ILoggerFactory loggerFactory;
private ConcurrentDictionary<string, ILogger> loggers = new();

public Logging(ILoggerFactory loggerFactory)
{
this.loggerFactory = loggerFactory;
}

public ILogger FromCategory(string category)
{
return loggers.GetOrAdd(category, cat => loggerFactory.CreateLogger(cat));
return loggers.GetOrAdd(category, loggerFactory.CreateLogger);
}

public ILogger FromCategory(Type categoryType)
Expand Down
4 changes: 1 addition & 3 deletions src/Ensuring/Ensurers/NumberEnsurer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ namespace IOKode.OpinionatedFramework.Ensuring.Ensurers;
/// As part of the Ensure API mechanism, this class is static.
/// </remarks>
[Ensurer]
public static partial class NumberEnsurer
{
}
public static partial class NumberEnsurer;
Loading

0 comments on commit fa14451

Please sign in to comment.