|
1 | 1 | --- |
2 | 2 | uid: InjectingServicesArticle |
3 | 3 | --- |
4 | | - |
5 | 4 | # Injecting services |
6 | 5 |
|
7 | | -There are a few techniques in which injecting dependencies is relevant. These are summarised below. |
8 | | -All but one of these techniques provide access to [the services which are added to the container]. |
9 | | - |
10 | | -[the services which are added to the container]: InjectableServices.md |
11 | | - |
12 | | -## Into test logic |
13 | | - |
14 | | -When using automated test logic based upon Screenplay, the use of dependency injection typically takes one of two forms. |
15 | | -Which of these depends upon the nature and paradigm of the test framework. |
16 | | - |
17 | | -For frameworks which are based on **test methods** such as [NUnit], services are typically injected via _method parameter injection_ into the test methods. |
18 | | -[If Screenplay were to be extended] to work with frameworks such as xUnit or MSTest then this is likely to be the technique used. |
19 | | - |
20 | | -For frameworks which are based on **binding classes** such as [Reqnroll], services are constructor-injected into binding classes. |
21 | | - |
22 | | -Use dependencies injected in this way to get access to [commonly-used Screenplay services] and anything else required at the root level of your test logic. |
23 | | - |
24 | | -[NUnit]: https://nunit.org/ |
25 | | -[If Screenplay were to be extended]: ../extendingScreenplay/TestIntegrations.md |
26 | | -[Reqnroll]: https://reqnroll.net/ |
27 | | -[commonly-used Screenplay services]: InjectableServices.md |
28 | | - |
29 | | -## Into standalone performance logic |
| 6 | +The table below summarises how to get services, from various contexts. In the first column are contexts/situations in which a developer could require services from DI. The second column indicates and links to the appropriate technique for that scenario. |
30 | 7 |
|
31 | | -If you are [using Screenplay standalone] then the [`Screenplay.ExecuteAsPerformanceAsync`] permits resolution of dependencies via its parameter. |
32 | | -That parameter is a `Func<IServiceProvider,CancellationToken,Task<bool?>>`. |
33 | | -The service provider may be used to resolve dependency services for the performance's logic. |
| 8 | +All but one of these techniques (the last) provides full access to [the services which are added to the container]. |
34 | 9 |
|
35 | | -Developers are urged to consider encapsulating their performance logic in implementations of [`IHostsPerformance`]. |
36 | | -Through an overload (extension method) named [`ExecuteAsPerformanceAsync<T>`], developers may specify the concrete implementation of that interface. |
37 | | -This extension method will resolve that implementation type along with any of its constructor-injected dependencies. |
38 | | -This avoids the service locator anti-pattern and provides a convenient pattern by which to write performance logic. |
| 10 | +| Context | Technique | |
| 11 | +| ---------------------------------------------------- | -------------------------------------------------------- | |
| 12 | +| NUnit style test method | [Parameter inject into the test] method | |
| 13 | +| Reqnroll style binding class | [Constructor inject into the binding] class | |
| 14 | +| Standalone Screenplay: [`ExecuteAsPerformanceAsync`] | [Create a performance host, or use the service provider] | |
| 15 | +| Persona class | [Constructor inject into the Persona] class | |
| 16 | +| Performable class, such as a Task | [Use the actor's abilities] | |
39 | 17 |
|
40 | | -Use services resolved from the service provider, or injected into your [`IHostsPerformance`] implementation, to get access to [commonly-used Screenplay services] and anything else required at the root level of your performance logic. |
41 | | - |
42 | | -[using Screenplay standalone]: ../gettingStarted/nonTesting/index.md |
43 | | -[`Screenplay.ExecuteAsPerformanceAsync`]: xref:CSF.Screenplay.Screenplay.ExecuteAsPerformanceAsync(System.Func{System.IServiceProvider,System.Threading.CancellationToken,System.Threading.Tasks.Task{System.Nullable{System.Boolean}}},System.Collections.Generic.IList{CSF.Screenplay.Performances.IdentifierAndName},System.Threading.CancellationToken) |
44 | | -[`IHostsPerformance`]: xref:CSF.Screenplay.IHostsPerformance |
45 | | -[`ExecuteAsPerformanceAsync<T>`]: xref:CSF.Screenplay.ScreenplayExtensions.ExecuteAsPerformanceAsync``1(CSF.Screenplay.Screenplay,System.Collections.Generic.IList{CSF.Screenplay.Performances.IdentifierAndName},System.Threading.CancellationToken) |
46 | | - |
47 | | -## Into personas |
48 | | - |
49 | | -Types which derive from [`IPersona`] support constructor-injected dependencies. |
50 | | -Personas are typically used by either [the cast] or [the stage] to get an [Actor]. |
51 | | -The technique in which they are used means that they are resolved, along with their constructor-injected dependencies, from DI. |
52 | | - |
53 | | -Use constructor-injected dependencies in persona classes to provide access to the APIs required to resolve [Abilities] that the actor is to be granted. |
54 | | - |
55 | | -[`IPersona`]: xref:CSF.Screenplay.IPersona |
56 | | -[the cast]: xref:CSF.Screenplay.ICast |
57 | | -[the stage]: xref:CSF.Screenplay.IStage |
58 | | -[Actor]: xref:CSF.Screenplay.Actor |
59 | | -[Abilities]: ../../glossary/Ability.md |
60 | | - |
61 | | -## Into performables |
62 | | - |
63 | | -_See the article explaining [how performables get their dependencies]_. |
64 | | - |
65 | | -[how performables get their dependencies]: Performables.md |
| 18 | +[the services which are added to the container]: InjectableServices.md |
| 19 | +[Parameter inject into the test]: ParameterInjectionForTests.md |
| 20 | +[Constructor inject into the binding]: ConstructorInjectionForBindings.md |
| 21 | +[Create a performance host, or use the service provider]: InjectionForStandaloneScreenplay.md |
| 22 | +[Constructor inject into the Persona]: ConstructorInjectIntoThePersona.md |
| 23 | +[`ExecuteAsPerformanceAsync`]: xref:CSF.Screenplay.Screenplay.ExecuteAsPerformanceAsync(System.Func{System.IServiceProvider,System.Threading.CancellationToken,System.Threading.Tasks.Task{System.Nullable{System.Boolean}}},System.Collections.Generic.IList{CSF.Screenplay.Performances.IdentifierAndName},System.Threading.CancellationToken) |
| 24 | +[Use the actor's abilities]: Performables.md |
0 commit comments