// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.Persistence.InMemory; using MUnique.OpenMU.Persistence.BasicModel; /// /// A repository provider which creates new in-memory repositories on-demand. /// public class InMemoryRepositoryProvider : BaseRepositoryProvider { /// /// Gets all which were added to this manager. /// internal IEnumerable MemoryRepositories => base.Repositories.Values.OfType(); /// /// Gets the memory repository, and creates it if it wasn't created yet. /// /// The type of the business object. /// The memory repository. public new IRepository GetRepository() where T : class { var repository = this.InternalGetRepository(typeof(T)) ?? this.CreateAndRegisterMemoryRepository(typeof(T)); return new InMemoryRepositoryAdapter((IMemoryRepository)repository); } /// /// Gets the repository of the specified type. /// /// Type of the object. /// The repository of the specified type. public new IRepository GetRepository(Type objectType) { var repository = this.InternalGetRepository(objectType) ?? this.CreateAndRegisterMemoryRepository(objectType); return repository; } private IRepository CreateAndRegisterMemoryRepository(Type type) { var baseModelAssembly = typeof(GameConfiguration).Assembly; var persistentType = baseModelAssembly.GetPersistentTypeOf(type) ?? type; var repositoryType = typeof(MemoryRepository<>).MakeGenericType(persistentType); var repository = (IRepository)Activator.CreateInstance(repositoryType)!; var baseType = type.Assembly == baseModelAssembly ? type.BaseType ?? type : type; this.RegisterRepository(baseType, repository!); return repository; } }