|
1 | 1 | # Creating a new service |
2 | 2 |
|
3 | | -We will be using [this PR](https://github.com/microsoft/component-detection/pull/12) following `EnvironmentVariableService` as a model for adding a new service. |
4 | | -The following steps are distilled and organized from that PR. |
| 3 | +Component Detection uses standard .NET dependency injection for service registration. This guide shows how to add a new service to the system. |
5 | 4 |
|
6 | | -1. Create your new service interface in `src/Microsoft.ComponentDetection.Contracts/IMyNewService.cs` |
7 | | -2. Create your new service implementation in `src/Microsoft.ComponentDetection.Common/MyNewService.cs` implementing and exporting `IMyNewService`. |
| 5 | +## Steps to create a new service |
8 | 6 |
|
9 | | -```c# |
10 | | -using System; |
11 | | -using System.Composition; |
12 | | -using Microsoft.ComponentDetection.Contracts; |
| 7 | +1. **Create your service interface** in `src/Microsoft.ComponentDetection.Contracts/IMyNewService.cs` |
13 | 8 |
|
14 | | -namespace Microsoft.ComponentDetection.Common |
| 9 | +```c# |
| 10 | +namespace Microsoft.ComponentDetection.Contracts |
15 | 11 | { |
16 | | - [Export(typeof(IMyNewService))] |
17 | | - public class MyNewService : IMyNewService |
| 12 | + public interface IMyNewService |
18 | 13 | { |
19 | | - ... |
| 14 | + // Define your service methods |
| 15 | + string DoSomething(); |
20 | 16 | } |
21 | 17 | } |
22 | 18 | ``` |
23 | 19 |
|
24 | | -3. Add your new service to `src/Microsoft.ComponentDetection.Contracts/IDetectorDependencies.cs` |
| 20 | +2. **Create your service implementation** in `src/Microsoft.ComponentDetection.Common/MyNewService.cs` |
25 | 21 |
|
26 | 22 | ```c# |
27 | | -namespace Microsoft.ComponentDetection.Contracts |
| 23 | +using Microsoft.ComponentDetection.Contracts; |
| 24 | + |
| 25 | +namespace Microsoft.ComponentDetection.Common |
28 | 26 | { |
29 | | - public interface IDetectorDependencies |
| 27 | + public class MyNewService : IMyNewService |
30 | 28 | { |
31 | | - ... |
32 | | - IMyNewService MyNewService { get; set; } |
| 29 | + // Inject any dependencies your service needs |
| 30 | + public MyNewService(ILogger<MyNewService> logger) |
| 31 | + { |
| 32 | + // Constructor injection |
| 33 | + } |
| 34 | + |
| 35 | + public string DoSomething() |
| 36 | + { |
| 37 | + // Implementation |
| 38 | + } |
33 | 39 | } |
34 | 40 | } |
35 | 41 | ``` |
36 | 42 |
|
37 | | -4. Add your new service to `src/Microsoft.ComponentDetection.Common/DetectorDependencies.cs` |
| 43 | +3. **Register your service** in `src/Microsoft.ComponentDetection.Orchestrator/Extensions/ServiceCollectionExtensions.cs` |
| 44 | + |
| 45 | +Add your service registration to the `AddComponentDetection` method: |
38 | 46 |
|
39 | 47 | ```c# |
40 | | -namespace Microsoft.ComponentDetection.Common |
| 48 | +public static IServiceCollection AddComponentDetection(this IServiceCollection services) |
41 | 49 | { |
42 | | - [Export(typeof(IDetectorDependencies))] |
43 | | - public class DetectorDependencies : IDetectorDependencies |
44 | | - { |
45 | | - ... |
46 | | - [Import] |
47 | | - public IMyNewService MyNewService { get; set; } |
48 | | - } |
| 50 | + // ... existing registrations ... |
| 51 | +
|
| 52 | + // Your new service |
| 53 | + services.AddSingleton<IMyNewService, MyNewService>(); |
| 54 | + |
| 55 | + // ... more registrations ... |
| 56 | + return services; |
49 | 57 | } |
50 | 58 | ``` |
51 | 59 |
|
52 | | -5. Add your new service to `src/Microsoft.ComponentDetection.Contracts/Internal/InjectionParameters.cs` |
| 60 | +4. **Use your service** in detectors or other services via constructor injection: |
53 | 61 |
|
54 | 62 | ```c# |
55 | | -namespace Microsoft.ComponentDetection.Contracts.Internal |
| 63 | +public class MyDetector : FileComponentDetector |
56 | 64 | { |
57 | | - internal class InjectionParameters |
| 65 | + private readonly IMyNewService myNewService; |
| 66 | + |
| 67 | + public MyDetector( |
| 68 | + IComponentStreamEnumerableFactory componentStreamEnumerableFactory, |
| 69 | + IObservableDirectoryWalkerFactory walkerFactory, |
| 70 | + ILogger<MyDetector> logger, |
| 71 | + IMyNewService myNewService) // Inject your service |
58 | 72 | { |
59 | | - internal InjectionParameters(IDetectorDependencies detectorDependencies) |
60 | | - { |
61 | | - ... |
62 | | - myNewServiceStatic = detectorDependencies.MyNewService; |
63 | | - } |
| 73 | + this.ComponentStreamEnumerableFactory = componentStreamEnumerableFactory; |
| 74 | + this.Scanner = walkerFactory; |
| 75 | + this.Logger = logger; |
| 76 | + this.myNewService = myNewService; |
64 | 77 | } |
65 | | - |
66 | | - ... |
67 | | - private static IMyNewService myNewServiceStatic; |
68 | | - |
69 | | - ... |
70 | | - [Export(typeof(IMyNewService))] |
71 | | - public IMyNewService MyNewService => myNewServiceStatic; |
72 | 78 | } |
73 | 79 | ``` |
| 80 | + |
| 81 | +## Service Lifetimes |
| 82 | + |
| 83 | +- Use `AddSingleton` for stateless services or services that should be reused across the application lifetime |
| 84 | +- Use `AddScoped` for services that should be created once per scan operation (rare in this codebase) |
| 85 | +- Use `AddTransient` for lightweight, stateless services that should be created each time they're requested |
| 86 | + |
| 87 | +Most services in Component Detection are registered as singletons. |
0 commit comments