-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDependencyInjectionLoader.cs
More file actions
79 lines (64 loc) · 2.76 KB
/
DependencyInjectionLoader.cs
File metadata and controls
79 lines (64 loc) · 2.76 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Reflection;
using Autofac;
using ByteSync.Common.Interfaces.Hub;
using ByteSync.ServerCommon.Interfaces.Factories;
using ByteSync.ServerCommon.Interfaces.Repositories;
using ByteSync.ServerCommon.Storage;
using Microsoft.Azure.SignalR.Management;
namespace ByteSync.Functions.Helpers.Loaders;
public static class DependencyInjectionLoader
{
public static void LoadDependencyInjection(this ContainerBuilder builder)
{
RegisterCurrentAssembly(builder);
RegisterServerCommonAssembly(builder);
}
private static void RegisterServerCommonAssembly(ContainerBuilder builder)
{
var executingAssembly = typeof(IClientsRepository).Assembly;
builder.RegisterAssemblyTypes(executingAssembly)
.Where(t => t.Name.EndsWith("Repository"))
.InstancePerLifetimeScope()
.AsImplementedInterfaces();
var genericRepositoryTypes = executingAssembly.GetTypes()
.Where(t => t.Name.Contains("Repository`") && t.IsGenericTypeDefinition && !t.IsInterface);
foreach (var genericType in genericRepositoryTypes)
{
builder.RegisterGeneric(genericType)
.AsImplementedInterfaces()
.AsSelf()
.InstancePerLifetimeScope();
}
builder.RegisterAssemblyTypes(executingAssembly)
.Where(t => t.Name.EndsWith("Service"))
.InstancePerLifetimeScope()
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(executingAssembly)
.Where(t => t.Name.EndsWith("Factory"))
.InstancePerLifetimeScope()
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(executingAssembly)
.Where(t => t.Name.EndsWith("Loader"))
.InstancePerLifetimeScope()
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(executingAssembly)
.Where(t => t.Name.EndsWith("Mapper"))
.InstancePerLifetimeScope()
.AsImplementedInterfaces();
builder.RegisterType<BlobContainerProvider>()
.InstancePerLifetimeScope()
.AsImplementedInterfaces();
builder.Register(c => {
var factory = c.Resolve<IHubContextFactory>();
return factory.CreateHubContext();
}).As<ServiceHubContext<IHubByteSyncPush>>().SingleInstance().AsImplementedInterfaces();
}
private static void RegisterCurrentAssembly(ContainerBuilder builder)
{
var executingAssembly = Assembly.GetExecutingAssembly();
builder.RegisterAssemblyTypes(executingAssembly)
.Where(t => t.Name.EndsWith("Service"))
.InstancePerLifetimeScope()
.AsImplementedInterfaces();
}
}