~ruther/NosSmooth.Local

ref: 25299b8f7815d3f72b3486f636b67031a68a788c NosSmooth.Local/src/Extensions/NosSmooth.Extensions.SharedBinding/SharedManager.cs -rw-r--r-- 2.6 KiB
25299b8f — Rutherther Merge pull request #8 from Rutherther/feat/shared-binding 2 years ago
                                                                                
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
//  SharedManager.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NosSmooth.Data.NOSFiles;
using NosSmooth.LocalBinding;
using NosSmooth.PacketSerializer.Packets;

namespace NosSmooth.Extensions.SharedBinding;

/// <summary>
/// Manager for sharing <see cref="NosBindingManager"/>,
/// <see cref="NostaleDataFilesManager"/> and
/// <see cref="IPacketTypesRepository"/>.
/// </summary>
public class SharedManager
{
    private static SharedManager? _instance;
    private Dictionary<Type, object> _sharedData = new();

    /// <summary>
    /// A singleton instance.
    /// One per process.
    /// </summary>
    public static SharedManager Instance
    {
        get
        {
            if (_instance is null)
            {
                _instance = new SharedManager();
            }

            return _instance;
        }
    }

    private SharedManager()
    {
    }

    /// <summary>
    /// Get shared equivalent of the given type.
    /// </summary>
    /// <param name="services">The service provider.</param>
    /// <typeparam name="T">The type to get shared instance of.</typeparam>
    /// <returns>The shared instance.</returns>
    /// <exception cref="InvalidOperationException">Thrown in case the type is not shared.</exception>
    public T GetShared<T>(IServiceProvider services)
        where T : class
    {
        if (!_sharedData.ContainsKey(typeof(T)))
        {
            _sharedData[typeof(T)] = CreateShared<T>(services);
        }

        return (T)_sharedData[typeof(T)];
    }

    private T CreateShared<T>(IServiceProvider services)
        where T : class
    {
        var options = services.GetRequiredService<IOptions<SharedOptions>>();
        var descriptor = options.Value.GetDescriptor(typeof(T));

        if (descriptor is null)
        {
            throw new InvalidOperationException
                ($"Could not find {typeof(T)} in the service provider when trying to make a shared instance.");
        }

        if (descriptor.ImplementationInstance is not null)
        {
            return (T)descriptor.ImplementationInstance;
        }

        if (descriptor.ImplementationFactory is not null)
        {
            return (T)descriptor.ImplementationFactory(services);
        }

        if (descriptor.ImplementationType is not null)
        {
            return (T)ActivatorUtilities.CreateInstance(services, descriptor.ImplementationType);
        }

        return ActivatorUtilities.CreateInstance<T>(services);
    }
}
Do not follow this link