-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathAddStorageAction.cs
More file actions
49 lines (42 loc) · 1.84 KB
/
AddStorageAction.cs
File metadata and controls
49 lines (42 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright © 2017 Dmitry Sikorsky. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Reflection;
using ExtCore.Data.Abstractions;
using ExtCore.Infrastructure;
using ExtCore.Infrastructure.Actions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace ExtCore.Data.Actions;
/// <summary>
/// Implements the <see cref="IConfigureServicesAction">IConfigureServicesAction</see> interface and
/// registers found implementation of the <see cref="IStorage">IStorage</see> interface inside the DI.
/// </summary>
public class AddStorageAction : IConfigureServicesAction
{
/// <summary>
/// Priority of the action. The actions will be executed in the order specified by the priority (from smallest to largest).
/// </summary>
public int Priority => 1000;
/// <summary>
/// Registers found implementation of the <see cref="IStorage">IStorage</see> interface inside the DI.
/// </summary>
/// <param name="services">
/// Will be provided by the ExtCore and might be used to register any service inside the DI.
/// </param>
/// <param name="serviceProvider">
/// Will be provided by the ExtCore and might be used to get any service that is registered inside the DI at this moment.
/// </param>
public void Execute(IServiceCollection services, IServiceProvider serviceProvider)
{
Type type = ExtensionManager.GetImplementations<IStorage>()?.FirstOrDefault(t => !t.GetTypeInfo().IsAbstract);
if (type == null)
{
ILogger logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger("ExtCore.Data");
logger.LogError("Implementation of ExtCore.Data.Abstractions.IStorage not found");
return;
}
services.AddScoped(typeof(IStorage), type);
}
}