~ruther/NosSmooth

ref: 18ab9b7ef6ddbe8d1f3212bc4078a59544f888d5 NosSmooth/Core/NosSmooth.Core/Stateful/StatefulRepository.cs -rw-r--r-- 1.7 KiB
18ab9b7e — Rutherther chore: make README.md be copied to root of package 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
//
//  StatefulRepository.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 System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NosSmooth.Core.Client;

namespace NosSmooth.Core.Stateful;

/// <summary>
/// Repository holding all the stateful entities for various NosTale clients.
/// </summary>
internal class StatefulRepository
{
    private readonly Dictionary<INostaleClient, Dictionary<Type, object>> _statefulEntities;

    /// <summary>
    /// Initializes a new instance of the <see cref="StatefulRepository"/> class.
    /// </summary>
    public StatefulRepository()
    {
        _statefulEntities = new Dictionary<INostaleClient, Dictionary<Type, object>>();
    }

    /// <summary>
    /// Get an entity for the given client and type.
    /// </summary>
    /// <param name="services">The service provider.</param>
    /// <param name="client">The NosTale client.</param>
    /// <param name="statefulEntityType">The type of the stateful entity to obtain.</param>
    /// <returns>The obtained entity.</returns>
    public object GetEntity(IServiceProvider services, INostaleClient client, Type statefulEntityType)
    {
        if (!_statefulEntities.ContainsKey(client))
        {
            _statefulEntities.Add(client, new Dictionary<Type, object>());
        }

        var value = _statefulEntities[client];
        if (!value.ContainsKey(statefulEntityType))
        {
            value.Add(statefulEntityType, ActivatorUtilities.CreateInstance(services, statefulEntityType));
        }

        return value[statefulEntityType];
    }
}
Do not follow this link