-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalloutManagerBuilder.cs
52 lines (45 loc) · 1.93 KB
/
CalloutManagerBuilder.cs
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
using System;
using System.Collections.Generic;
using CitizenFX.Core;
using Microsoft.Extensions.DependencyInjection;
using PoliceMP.Client.Services.Interfaces;
using PoliceMP.Core.Server.Commands.Interfaces;
using PoliceMP.Core.Server.Communications.Interfaces;
using PoliceMP.Core.Server.Interfaces.Services;
using PoliceMP.Core.Shared;
namespace PoliceMP.Server.Services.Callouts
{
public interface ICalloutManagerBuilder
{
ICalloutManagerBuilder AddCallout<T>() where T : ICallout;
ICalloutManagerBuilder AddCallout<T>(Action<CalloutOptions> configure);
IServiceCollection Build();
}
public class CalloutManagerBuilder : ICalloutManagerBuilder
{
private readonly IServiceCollection _serviceCollection;
private readonly Dictionary<Type, CalloutOptions> _calloutList = new ();
public CalloutManagerBuilder(IServiceCollection serviceCollection)
{
_serviceCollection = serviceCollection;
}
public ICalloutManagerBuilder AddCallout<T>() where T : ICallout
=> AddCallout<T>(null);
public ICalloutManagerBuilder AddCallout<T>(Action<CalloutOptions> configure)
{
var options = new CalloutOptions();
configure?.Invoke(options);
_calloutList.Add(typeof(T), options);
return this;
}
public IServiceCollection Build()
{
foreach (var calloutType in _calloutList.Keys)
{
_serviceCollection.AddTransient(calloutType);
}
_serviceCollection.AddSingleton<ICalloutManager>(sp => new CalloutManager(sp, sp.GetRequiredService<ILogger<CalloutManager>>(), _calloutList, sp.GetRequiredService<INotificationService>(), sp.GetRequiredService<IServerCommunicationsManager>(), sp.GetRequiredService<PlayerList>(), sp.GetRequiredService<IPermissionService>()));
return _serviceCollection;
}
}
}