//
// 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;
///
/// Repository holding all the stateful entities for various NosTale clients.
///
internal class StatefulRepository
{
private readonly Dictionary> _statefulEntities;
///
/// Initializes a new instance of the class.
///
public StatefulRepository()
{
_statefulEntities = new Dictionary>();
}
///
/// Get an entity for the given client and type.
///
/// The service provider.
/// The NosTale client.
/// The type of the stateful entity to obtain.
/// The obtained entity.
public object GetEntity(IServiceProvider services, INostaleClient client, Type statefulEntityType)
{
if (!_statefulEntities.ContainsKey(client))
{
_statefulEntities.Add(client, new Dictionary());
}
var value = _statefulEntities[client];
if (!value.ContainsKey(statefulEntityType))
{
value.Add(statefulEntityType, ActivatorUtilities.CreateInstance(services, statefulEntityType));
}
return value[statefulEntityType];
}
}