AutomationIoC 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutomationIoC --version 1.1.0                
NuGet\Install-Package AutomationIoC -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutomationIoC" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutomationIoC --version 1.1.0                
#r "nuget: AutomationIoC, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AutomationIoC as a Cake Addin
#addin nuget:?package=AutomationIoC&version=1.1.0

// Install AutomationIoC as a Cake Tool
#tool nuget:?package=AutomationIoC&version=1.1.0                

Nuget Version Nuget Downloads Continuous Integration

AutomationIoC

Dependency Injection Framework for C# PowerShell Cmdlets

Requirements

Getting Started

Installation

dotnet add package AutomationIoC

Development

See Sample Project for a full sample solution

Add the following to your .csproj file

<PropertyGroup>
    
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Add a Startup class

public class Startup : IoCStartup
{
    // Add any configuration sources or data needed
    public override void Configure(IConfigurationBuilder configurationBuilder)
    {
        // AutomationEnvironment class allows retrieving global PowerShell
        // variables during service configuration.
        var environment =
            AutomationEnvironment.GetVariable<string>("AppEnvironment");

        var appSettings = new Dictionary<string, string>()
        {
            ["TestOptionsConfig:TestProperty"] = "This is an example",
            ["AppEnvironment"] = environment
        };

        configurationBuilder.AddInMemoryCollection(appSettings);
    }

    // Configure services/lifetimes
    public override void ConfigureServices(IServiceCollection services)
    {
        services.Configure<TestOptions>(Configuration.GetSection("TestOptionsConfig"));

        services.AddTransient<ITestDependencyOne, TestDependencyOne>();
        services.AddTransient<ITestDependencyTwo, TestDependencyTwo>();
        // ...
        // other services, client, etc.
    }
}

Add a PowerShell Command with Dependencies Injected

Synchronous process example

[Cmdlet(VerbsLifecycle.Submit, "Data")]
public class SubmitData : IoCShell<Startup>
{
    [Parameter(Mandatory = true)]
    public string Id { get; set; }

    [AutomationDependency]
    protected ITestDependencyOne TestDependencyOne { get; set; }

    [AutomationDependency]
    protected ITestDependencyTwo testDependencyTwo { get; set; }

    [AutomationDependency]
    private ILogger<RequestCard> logger { get; set; }

    protected override void ExecuteCmdlet()
    {
        TestDependencyOne.PushTestId(Id);

        TestData testData = testDependencyTwo.GetTestData(Id);

        logger.LogInformation("Auto Generated Data: {Id} - {Name}", Id, testData.Name);

        WriteObject(testData);
    }
}

Asynchronous process example

[Cmdlet(VerbsLifecycle.Submit, "DataAsync")]
public class SubmitDataAsync : IoCShellAsync<Startup>
{
    [Parameter(Mandatory = true)]
    public string Id { get; set; }

    [AutomationDependency]
    protected ITestDependencyOne TestDependencyOne { get; set; }

    [AutomationDependency]
    protected ITestDependencyTwo testDependencyTwo { get; set; }

    [AutomationDependency]
    private ILogger<RequestCard> logger { get; set; }

    protected override async Task ExecuteCmdletAsync()
    {
        await TestDependencyOne.PushTestIdAsync(Id);

        TestData testData = await testDependencyTwo.GetTestDataAsync(Id);

        logger.LogInformation("Auto Generated Data: {Id} - {Name}", Id, testData.Name);

        WriteObject(testData);
    }
}

Execution

In PowerShell terminal run the following:

Import-Module <path-to-your-dll>/<your-assembly>.dll -V

You should see your custom commands listed in the verbose output (signaled by -v).

Now you are ready to run your own custom commands!

The following example is based on command name from above:

Submit-Data -id "ThisIsATestId"

See Project - launch.json for a sample on launching your module through VS Code

Testing

using AutomationIoC.Tools;
using FluentAssertions;
using Xunit;

public class RequestCardTests
{
    [Fact]
    public void ShouldSubmitData()
    {
        var testDependencyOneMock = new Mock<ITestDependencyOne>();
        var testDependencyTwoMock = new Mock<ITestDependencyTwo>();
        var testId = "ThisIsATestId";
        var expectedTestData = new TestData { Id = testId, Name = "ThisIsTestName" };

        testDependencyTwoMock.Setup(dependency =>
            dependency.GetTestDataAsync(testId))
                .Returns(expectedTestData);

        using var submitDataCommand =
            AutomationSandbox.CreateContext<SubmitData, Startup>(services =>
            {
                services.AddTransient(_ => testDependencyOneMock.Object);
                services.AddTransient(_ => testDependencyTwoMock.Object);
            });

        submitDataCommand.AddParameter(command =>
            command.AddParameter("id", testId));

        var actualTestData = submitDataCommand.RunCommand<TestData>().First();

        actualTestData.Should().BeEquivalentTo(expectedTestData);
    }
}

Other Resources

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on AutomationIoC:

Package Downloads
AutomationIoC.Consoles

Dependency Injection Framework for C# Console Applications

AutomationIoC.PSCmdlets

PSCmdlet SDK utilities for AutomationIoC framework development

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0-alpha2 642 8/12/2023
2.1.0-alpha1 663 6/16/2023
2.0.0-preview1 654 6/16/2023
2.0.0-alpha1 665 6/10/2023
1.3.0 916 11/24/2022
1.2.0 939 11/11/2022
1.1.0 1,104 4/4/2022
1.1.0-beta1 704 3/31/2022
1.1.0-alpha2 695 3/26/2022
1.1.0-alpha1 690 3/26/2022
1.0.0 1,081 3/8/2022
1.0.0-preview1 709 3/6/2022
1.0.0-beta1 625 2/27/2022
1.0.0-alpha2 689 2/27/2022