/// <summary> /// Initializes all types of the assembly /// </summary> /// <param name="assembly">The assembly for initializing</param> /// <param name="tracker">The tracker for tracking logs</param> public static void Initialize(Assembly assembly, Action <string, Exception> tracker = null) { try { var types = assembly.GetExportedTypes(); tracker?.Invoke($"Initialize the assembly: {assembly.GetName().Name} with {types.Length} exported type(s)", null); if (tracker == null && RepositoryMediator.IsDebugEnabled) { RepositoryMediator.WriteLogs($"Initialize the assembly: {assembly.GetName().Name} with {types.Length} exported type(s)", null); } // repositories types.Where(type => type.IsDefined <RepositoryAttribute>(false)).ForEach(type => { tracker?.Invoke($"Register the repository: {type.GetTypeName()}", null); if (tracker == null && RepositoryMediator.IsDebugEnabled) { RepositoryMediator.WriteLogs($"Register the repository: {type.GetTypeName()}", null); } RepositoryDefinition.Register(type, tracker); }); // entities types.Where(type => type.IsDefined <EntityAttribute>(false)).ForEach(type => { tracker?.Invoke($"Register the repository entity: {type.GetTypeName()}", null); if (tracker == null && RepositoryMediator.IsDebugEnabled) { RepositoryMediator.WriteLogs($"Register the repository entity: {type.GetTypeName()}", null); } EntityDefinition.Register(type); }); // event handlers types.Where(type => type.IsDefined <EventHandlersAttribute>(false)) .Where(type => typeof(IPreCreateHandler).IsAssignableFrom(type) || typeof(IPostCreateHandler).IsAssignableFrom(type) || typeof(IPreGetHandler).IsAssignableFrom(type) || typeof(IPostGetHandler).IsAssignableFrom(type) || typeof(IPreUpdateHandler).IsAssignableFrom(type) || typeof(IPostUpdateHandler).IsAssignableFrom(type) || typeof(IPreDeleteHandler).IsAssignableFrom(type) || typeof(IPostDeleteHandler).IsAssignableFrom(type)) .ForEach(type => { tracker?.Invoke($"Register the event-handler: {type.GetTypeName()}", null); if (tracker == null && RepositoryMediator.IsDebugEnabled) { RepositoryMediator.WriteLogs($"Register the event-handler: {type.GetTypeName()}", null); } RepositoryMediator.EventHandlers.Add(type); }); } catch (ReflectionTypeLoadException ex) { if (ex.LoaderExceptions.FirstOrDefault(e => e is System.IO.FileNotFoundException) == null) { tracker?.Invoke($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex); if (tracker == null) { RepositoryMediator.WriteLogs($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex); } ex.LoaderExceptions.ForEach(exception => tracker?.Invoke(null, exception)); throw; } } catch (Exception ex) { tracker?.Invoke($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex); if (tracker == null) { RepositoryMediator.WriteLogs($"Error occurred while initializing the assembly: {assembly.GetName().Name}", ex); } throw; } }