Skip to content

Commit

Permalink
Fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
tgardner authored and jeremydmiller committed Sep 23, 2024
1 parent 9e54938 commit a70a0f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/Oakton/DependencyInjectionCommandCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public DependencyInjectionCommandCreator(IServiceProvider serviceProvider)

public IOaktonCommand CreateCommand(Type commandType)
{
using var scope = _serviceProvider.CreateScope();
return ActivatorUtilities.CreateInstance(scope.ServiceProvider, commandType) as IOaktonCommand;
return ActivatorUtilities.CreateInstance(_serviceProvider, commandType) as IOaktonCommand;
}

public object CreateModel(Type modelType)
Expand Down
12 changes: 6 additions & 6 deletions src/Oakton/HostedCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static void AddOakton(this IServiceCollection services, Action<OaktonOpti
{
services.Configure(options);

services.TryAddSingleton<ICommandCreator, DependencyInjectionCommandCreator>();
services.TryAddScoped<ICommandCreator, DependencyInjectionCommandCreator>();

services.TryAddSingleton<ICommandFactory>((ctx) =>
services.TryAddScoped<ICommandFactory>((ctx) =>
{
var creator = ctx.GetRequiredService<ICommandCreator>();
var oaktonOptions = ctx.GetRequiredService<IOptions<OaktonOptions>>().Value;
Expand All @@ -36,7 +36,7 @@ public static void AddOakton(this IServiceCollection services, Action<OaktonOpti
return factory;
});

services.TryAddSingleton<CommandExecutor>();
services.TryAddScoped<CommandExecutor>();
}

/// <summary>
Expand All @@ -59,14 +59,14 @@ public static int RunHostedOaktonCommands(this IHost host, string[] args)
/// </summary>
/// <param name="host">An already built IHost</param>
/// <param name="args"></param>
/// <param name="builder">Optionally configure additional command options</param>
/// <returns></returns>
public static Task<int> RunHostedOaktonCommandsAsync(this IHost host, string[] args)
{
var options = host.Services.GetRequiredService<IOptions<OaktonOptions>>().Value;
using var scope = host.Services.CreateScope();
var options = scope.ServiceProvider.GetRequiredService<IOptions<OaktonOptions>>().Value;
args = ApplyArgumentDefaults(args, options);

var executor = host.Services.GetRequiredService<CommandExecutor>();
var executor = scope.ServiceProvider.GetRequiredService<CommandExecutor>();

if (executor.Factory is CommandFactory factory)
{
Expand Down
26 changes: 17 additions & 9 deletions src/Tests/HostedCommandsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void CanInjectServicesIntoCommands()
var builder = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<TestDependency>();
services.AddScoped<TestDependency>();
services.AddOakton(options =>
{
options.Factory = factory =>
Expand All @@ -36,9 +36,23 @@ public class TestInput
{
}

public record TestDependency(int Value = 1);
public class TestDependency : IDisposable
{
public int Value { get; private set; }

public class TestDICommand : OaktonCommand<TestInput>, IDisposable
public TestDependency()
{
Value = 1;
}

public void Dispose()
{
Value = 0;
GC.SuppressFinalize(this);
}
}

public class TestDICommand : OaktonCommand<TestInput>
{
public static int Value { get; set; } = 0;
private readonly TestDependency _dep;
Expand All @@ -52,11 +66,5 @@ public override bool Execute(TestInput input)
Value = _dep.Value;
return true;
}

public void Dispose()
{
Value = 0;
GC.SuppressFinalize(this);
}
}
}

0 comments on commit a70a0f8

Please sign in to comment.