using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using Microsoft.EntityFrameworkCore; using xCommons.Extensions; using xDataService.Interfaces; namespace xDataService.Providers { public class XEntityRegisterarHandlerService { /// /// a Collection for Extract Registerars Services ... /// private readonly List registerars = []; /// /// a Collection for Holding all Entity Types ... /// public IReadOnlyList AllEntityTypes { get; private set; } /// /// Retrieve Specified Registerars Entity Types ... /// /// /// public IReadOnlyList GetEntityTypes(string[] names = null) { // var items = new List(); // if (!names.IsNull() && names.HasChild()) { // items = [.. registerars .Where(r => names.Contains(r.Name)) .SelectMany(r => r.GetEntityTypes())]; } // return items.AsReadOnly(); } /// /// Load all Dynamically Registered Entities ... /// public void HandleRegisteredEntities() { // var entityTypes = new List(); // path = Path.Combine (Directory.GetCurrentDirectory ()); var path = Path.Combine (AppContext.BaseDirectory); var files = Directory.GetFiles(path, "*.dll"); foreach (var dll in files) { // // Retrieve DLL Assembly ... var assembly = Assembly.LoadFrom(dll); // // Extract Registerar Types ... var registerarTypes = assembly .GetExportedTypes() .Where(t => !t.IsAbstract && typeof(IXEntityRegisterar).IsAssignableFrom(t)); foreach (var registerarType in registerarTypes) { // // Create Instance of Registerar Type ... var registerar = (IXEntityRegisterar)Activator.CreateInstance(registerarType); if (!registerar.IsNull()) { // // Add Registerar Instance to Collection ... registerars.Add(registerar); // // Add Registerar Provided Entities to Collection ... entityTypes.AddRange(registerar.GetEntityTypes()); } } // // Converts to Readonly List ... AllEntityTypes = entityTypes.AsReadOnly(); } } /// /// Configure all Dynamically Registered Entities ... /// /// /// public void HandleApplyRegisteredEntitiesConfigurations( ModelBuilder modelBuilder, string[] names = null ) { // var selectedRegisterars = !names.IsNull() && names.HasChild() ? registerars .Where(r => names.Contains(r.Name)) : registerars; // // Loop Through Extracted Registerars ... foreach(var registerar in selectedRegisterars) { // // Configure Specified Registerars Entities ... registerar.ConfigureEntities(modelBuilder); } } } }