Skip to content

Commit 9ea9b9a

Browse files
committed
Dependency injection sample
1 parent 916cec9 commit 9ea9b9a

File tree

9 files changed

+209
-0
lines changed

9 files changed

+209
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using WorkflowCore.Interface;
6+
using WorkflowCore.Models;
7+
using WorkflowCore.Sample15.Steps;
8+
9+
namespace WorkflowCore.Sample15
10+
{
11+
public class HelloWorldWorkflow : IWorkflow
12+
{
13+
public void Build(IWorkflowBuilder<object> builder)
14+
{
15+
builder
16+
.StartWith<HelloWorld>()
17+
.Then<DoSomething>();
18+
}
19+
20+
public string Id => "HelloWorld";
21+
22+
public int Version => 1;
23+
24+
}
25+
}

WorkflowCore.Sample15/Program.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
4+
using WorkflowCore.Interface;
5+
using WorkflowCore.Sample15.Steps;
6+
using WorkflowCore.Sample15.Services;
7+
8+
namespace WorkflowCore.Sample15
9+
{
10+
class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
IServiceProvider serviceProvider = ConfigureServices();
15+
16+
//start the workflow host
17+
var host = serviceProvider.GetService<IWorkflowHost>();
18+
host.RegisterWorkflow<HelloWorldWorkflow>();
19+
host.Start();
20+
21+
host.StartWorkflow("HelloWorld", 1, null);
22+
23+
Console.ReadLine();
24+
host.Stop();
25+
}
26+
27+
private static IServiceProvider ConfigureServices()
28+
{
29+
//setup dependency injection
30+
IServiceCollection services = new ServiceCollection();
31+
services.AddLogging();
32+
services.AddWorkflow();
33+
34+
services.AddTransient<DoSomething>();
35+
services.AddTransient<IMyService, MyService>();
36+
37+
var serviceProvider = services.BuildServiceProvider();
38+
39+
return serviceProvider;
40+
}
41+
42+
}
43+
}

WorkflowCore.Sample15/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Dependency Injection Sample
2+
3+
Illustrates the use of dependency injection for workflow steps.
4+
5+
Consider the following service
6+
7+
```C#
8+
public interface IMyService
9+
{
10+
void DoTheThings();
11+
}
12+
...
13+
public class MyService : IMyService
14+
{
15+
public void DoTheThings()
16+
{
17+
Console.WriteLine("Doing stuff...");
18+
}
19+
}
20+
```
21+
22+
Which is consumed by a workflow step as follows
23+
24+
```C#
25+
public class DoSomething : StepBody
26+
{
27+
private IMyService _myService;
28+
29+
public DoSomething(IMyService myService)
30+
{
31+
_myService = myService;
32+
}
33+
34+
public override ExecutionResult Run(IStepExecutionContext context)
35+
{
36+
_myService.DoTheThings();
37+
return ExecutionResult.Next();
38+
}
39+
}
40+
```
41+
42+
Simply add both the service and the workflow step as transients to the service collection when setting up your IoC container.
43+
44+
```C#
45+
IServiceCollection services = new ServiceCollection();
46+
services.AddLogging();
47+
services.AddWorkflow();
48+
49+
services.AddTransient<DoSomething>();
50+
services.AddTransient<IMyService, MyService>();
51+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace WorkflowCore.Sample15.Services
2+
{
3+
public interface IMyService
4+
{
5+
void DoTheThings();
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace WorkflowCore.Sample15.Services
6+
{
7+
public class MyService : IMyService
8+
{
9+
public void DoTheThings()
10+
{
11+
Console.WriteLine("Doing stuff...");
12+
}
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using WorkflowCore.Interface;
7+
using WorkflowCore.Models;
8+
using WorkflowCore.Sample15.Services;
9+
10+
namespace WorkflowCore.Sample15.Steps
11+
{
12+
public class DoSomething : StepBody
13+
{
14+
private IMyService _myService;
15+
16+
public DoSomething(IMyService myService)
17+
{
18+
_myService = myService;
19+
}
20+
21+
public override ExecutionResult Run(IStepExecutionContext context)
22+
{
23+
_myService.DoTheThings();
24+
return ExecutionResult.Next();
25+
}
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using WorkflowCore.Interface;
6+
using WorkflowCore.Models;
7+
8+
namespace WorkflowCore.Sample15.Steps
9+
{
10+
public class HelloWorld : StepBody
11+
{
12+
public override ExecutionResult Run(IStepExecutionContext context)
13+
{
14+
Console.WriteLine("Hello world");
15+
return ExecutionResult.Next();
16+
}
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
10+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\src\WorkflowCore\WorkflowCore.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

WorkflowCore.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkflowCore.TestSample01",
9999
EndProject
100100
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Docker.Testify", "test\Docker.Testify\Docker.Testify.csproj", "{EC497168-5347-4E70-9D9E-9C2F826C1CDF}"
101101
EndProject
102+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowCore.Sample15", "WorkflowCore.Sample15\WorkflowCore.Sample15.csproj", "{F9FD8357-C299-4CF4-B0D4-D3D5E45AAAA3}"
103+
EndProject
102104
Global
103105
GlobalSection(SolutionConfigurationPlatforms) = preSolution
104106
Debug|Any CPU = Debug|Any CPU
@@ -253,6 +255,10 @@ Global
253255
{EC497168-5347-4E70-9D9E-9C2F826C1CDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
254256
{EC497168-5347-4E70-9D9E-9C2F826C1CDF}.Release|Any CPU.ActiveCfg = Release|Any CPU
255257
{EC497168-5347-4E70-9D9E-9C2F826C1CDF}.Release|Any CPU.Build.0 = Release|Any CPU
258+
{F9FD8357-C299-4CF4-B0D4-D3D5E45AAAA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
259+
{F9FD8357-C299-4CF4-B0D4-D3D5E45AAAA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
260+
{F9FD8357-C299-4CF4-B0D4-D3D5E45AAAA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
261+
{F9FD8357-C299-4CF4-B0D4-D3D5E45AAAA3}.Release|Any CPU.Build.0 = Release|Any CPU
256262
EndGlobalSection
257263
GlobalSection(SolutionProperties) = preSolution
258264
HideSolutionNode = FALSE
@@ -298,6 +304,7 @@ Global
298304
{62A9709E-27DA-42EE-B94F-5AF431D86354} = {E6CEAD8D-F565-471E-A0DC-676F54EAEDEB}
299305
{0E3C1496-8E7C-411A-A536-C7C9CE4EED4E} = {5080DB09-CBE8-4C45-9957-C3BB7651755E}
300306
{EC497168-5347-4E70-9D9E-9C2F826C1CDF} = {E6CEAD8D-F565-471E-A0DC-676F54EAEDEB}
307+
{F9FD8357-C299-4CF4-B0D4-D3D5E45AAAA3} = {5080DB09-CBE8-4C45-9957-C3BB7651755E}
301308
EndGlobalSection
302309
GlobalSection(ExtensibilityGlobals) = postSolution
303310
SolutionGuid = {DC0FA8D3-6449-4FDA-BB46-ECF58FAD23B4}

0 commit comments

Comments
 (0)