diff --git a/docs/en/Dependency-Injection.md b/docs/en/Dependency-Injection.md index f347dc1dd7..019366b3be 100644 --- a/docs/en/Dependency-Injection.md +++ b/docs/en/Dependency-Injection.md @@ -49,7 +49,7 @@ public class BlogModule : AbpModule } ```` -The section below explains the conventions and configurations. +The sections below explain the conventions and configurations. ### Inherently Registered Types @@ -266,6 +266,102 @@ public class MyService : ITransientDependency } ```` +### Dealing with multiple implementations + +You can register multiple implementations of the same service interface. Assume that you have an `IExternalLogger` interface with two implementations: + +````csharp +public interface IExternalLogger +{ + Task LogAsync(string logText); +} + +public class ElasticsearchExternalLogger : IExternalLogger +{ + public async Task LogAsync(string logText) + { + //TODO... + } +} + +public class AzureExternalLogger : IExternalLogger +{ + public Task LogAsync(string logText) + { + throw new System.NotImplementedException(); + } +} +```` + +In this example, we haven't registered any of the implementation classes to the dependency injection system yet. So, if we try to inject the `IExternalLogger` interface, we get an error indicating that no implementation found. + +If we register both of the `ElasticsearchExternalLogger` and `AzureExternalLogger` services for the `IExternalLogger` interface, and then try to inject the `IExternalLogger` interface, then the last registered implementation will be used. + +An example service injecting the `IExternalLogger` interface: + +````csharp +public class MyService : ITransientDependency +{ + private readonly IExternalLogger _externalLogger; + + public MyService(IExternalLogger externalLogger) + { + _externalLogger = externalLogger; + } + + public async Task DemoAsync() + { + await _externalLogger.LogAsync("Example log message..."); + } +} +```` + +Here, as said before, we get the last registered implementation. However, how to determine the last registered implementation? + +If we implement one of the dependency interfaces (e.g. `ITransientDependency`), then the registration order will be uncertain (it may depend on the namespaces of the classes). The *last registered implementation* can be different than you expect. So, it is not suggested to use the dependency interfaces to register multiple implementations. + +You can register your services in the `ConfigureServices` method of your module: + +````csharp +public override void ConfigureServices(ServiceConfigurationContext context) +{ + context.Services.AddTransient(); + context.Services.AddTransient(); +} +```` + +In this case, you get an `AzureExternalLogger` instance when you inject the `IExternalLogger` interface, because the last registered implementation is the `AzureExternalLogger` class. + +When you have multiple implementation of an interface, you may want to work with all these implementations. Assume that you want to write log to all the external loggers. We can change the `MyService` implementation as the following: + +````csharp +public class MyService : ITransientDependency +{ + private readonly IEnumerable _externalLoggers; + + public MyService(IEnumerable externalLoggers) + { + _externalLoggers = externalLoggers; + } + + public async Task DemoAsync() + { + foreach (var externalLogger in _externalLoggers) + { + await externalLogger.LogAsync("Example log message..."); + } + } +} +```` + +In this example, we are injecting `IEnumerable` instead of `IExternalLogger`, so we have a collection of the `IExternalLogger` implementations. Then we are using a `foreach` loop to write the same log text to all the `IExternalLogger` implementations. + +If you are using `IServiceProvider` to resolve dependencies, then use its `GetServices` method to obtain a collection of the service implementations: + +````csharp +IEnumerable services = _serviceProvider.GetServices(); +```` + ### Releasing/Disposing Services If you used a constructor or property injection, you don't need to be concerned about releasing the service's resources. However, if you have resolved a service from ``IServiceProvider``, you might, in some cases, need to take care about releasing the service resources.