//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.Persistence.EntityFramework;
using Microsoft.Extensions.Logging;
using MUnique.OpenMU.Interfaces;
///
/// A repository provider that does not use caching.
///
internal class RepositoryProvider : BaseRepositoryProvider, IContextAwareRepositoryProvider
{
///
/// Initializes a new instance of the class.
///
/// The logger factory.
/// The change publisher.
/// The context stack.
public RepositoryProvider(ILoggerFactory loggerFactory, IConfigurationChangeListener? changeListener, IContextStack contextStack)
{
this.LoggerFactory = loggerFactory;
this.ChangeListener = changeListener;
this.ContextStack = contextStack;
}
///
/// Gets the context stack. When loading an object, the current context should be pushed onto the stack.
///
public IContextStack ContextStack { get; }
///
/// Gets the logger factory.
///
protected ILoggerFactory LoggerFactory { get; }
///
/// Gets the change publisher.
///
protected IConfigurationChangeListener? ChangeListener { get; }
///
/// Creates the generic repository for the specified type.
///
/// Type of the entity.
/// The repository provider.
/// The created repository.
protected virtual IRepository CreateGenericRepository(Type entityType, IContextAwareRepositoryProvider repositoryProvider)
{
var repositoryType = typeof(GenericRepository<>).MakeGenericType(entityType);
return (IRepository)Activator.CreateInstance(repositoryType, repositoryProvider, this.LoggerFactory, this.ChangeListener)!;
}
///
/// Registers the repository. Adapts the type so that the base type gets registered.
///
/// The generic type that the repository handles.
/// The repository.
protected override void RegisterRepository(Type type, IRepository repository)
{
if (type.Assembly.Equals(this.GetType().Assembly) && type.BaseType != typeof(object))
{
base.RegisterRepository(type.BaseType!, repository);
}
else
{
base.RegisterRepository(type, repository);
}
}
}