From 0c46ae922c355f3c9f21f08ad489e629611b5ecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sun, 26 Feb 2017 23:07:38 +0300 Subject: [PATCH] added seed data for mongodb. --- .../Blogging/AbpDeskMongoBlogModule.cs | 54 ++++++++++++++++++- .../AbpDesk/Blogging/BlogPostComment.cs | 2 +- .../Volo/Abp/Modularity/ModuleLoader.cs | 20 +++---- .../Volo/Abp/Modularity/ModuleManager.cs | 20 ++++++- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs index a1426378a4..d41acc9a87 100644 --- a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs +++ b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs @@ -1,9 +1,14 @@ -using System.Reflection; +using System.Linq; +using System.Reflection; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.Domain.Repositories; using Volo.Abp.EmbeddedFiles; +using Volo.Abp.Guids; using Volo.Abp.Modularity; using Volo.Abp.MongoDB; +using Volo.Abp.Uow; namespace AbpDesk.Blogging { @@ -31,5 +36,52 @@ namespace AbpDesk.Blogging services.AddAssemblyOf(); } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + CreateSeedData(context); + } + + private static void CreateSeedData(ApplicationInitializationContext context) + { + using (var scope = context.ServiceProvider.CreateScope()) + { + using (var uow = scope.ServiceProvider.GetRequiredService().Begin()) + { + var blogPostRepository = scope.ServiceProvider.GetRequiredService>(); + if (blogPostRepository.Any()) + { + return; + } + + var guidGenerator = scope.ServiceProvider.GetRequiredService(); + + blogPostRepository.Insert( + new BlogPost( + guidGenerator.Create(), + "Demo blog post title one!", + "Sample body text for the first blog post" + ) + ); + + blogPostRepository.Insert( + new BlogPost( + guidGenerator.Create(), + "Demo blog post title second!", + "Sample body text for the second blog post" + ) + { + Comments = + { + new BlogPostComment("John", "Hi, this is a good post! Thank you :)"), + new BlogPostComment("Adam", "You are adam! :s") + } + } + ); + + uow.Complete(); + } + } + } } } diff --git a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPostComment.cs b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPostComment.cs index 33c72de16c..f8298acc4d 100644 --- a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPostComment.cs +++ b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/BlogPostComment.cs @@ -16,7 +16,7 @@ namespace AbpDesk.Blogging [NotNull] public virtual string Message { get; protected set; } - public BlogPostComment() + protected BlogPostComment() { } diff --git a/src/Volo.Abp/Volo/Abp/Modularity/ModuleLoader.cs b/src/Volo.Abp/Volo/Abp/Modularity/ModuleLoader.cs index a50dbd94b8..9d56703f77 100644 --- a/src/Volo.Abp/Volo/Abp/Modularity/ModuleLoader.cs +++ b/src/Volo.Abp/Volo/Abp/Modularity/ModuleLoader.cs @@ -10,8 +10,8 @@ namespace Volo.Abp.Modularity public class ModuleLoader : IModuleLoader { public AbpModuleDescriptor[] LoadModules( - IServiceCollection services, - Type startupModuleType, + IServiceCollection services, + Type startupModuleType, PlugInSourceList plugInSources) { Check.NotNull(services, nameof(services)); @@ -31,9 +31,9 @@ namespace Volo.Abp.Modularity } protected virtual void FillModules( - List modules, - IServiceCollection services, - Type startupModuleType, + List modules, + IServiceCollection services, + Type startupModuleType, PlugInSourceList plugInSources) { //All modules starting from the startup module @@ -45,7 +45,7 @@ namespace Volo.Abp.Modularity //Plugin modules foreach (var moduleType in plugInSources.GetAllModules()) { - if(modules.Any(m => m.Type == moduleType)) + if (modules.Any(m => m.Type == moduleType)) { continue; } @@ -61,7 +61,7 @@ namespace Volo.Abp.Modularity SetModuleDependencies(modules, module); } } - + protected virtual List SortByDependency(List modules, Type startupModuleType) { var sortedModules = modules.SortByDependencies(m => m.Dependencies); @@ -77,7 +77,7 @@ namespace Volo.Abp.Modularity protected virtual IAbpModule CreateAndRegisterModule(IServiceCollection services, Type moduleType) { - var module = (IAbpModule) Activator.CreateInstance(moduleType); + var module = (IAbpModule)Activator.CreateInstance(moduleType); services.AddSingleton(moduleType, module); return module; } @@ -87,7 +87,7 @@ namespace Volo.Abp.Modularity //PreConfigureServices foreach (var module in modules.Where(m => m.Instance is IPreConfigureServices)) { - ((IPreConfigureServices) module.Instance).PreConfigureServices(services); + ((IPreConfigureServices)module.Instance).PreConfigureServices(services); } //ConfigureServices @@ -102,7 +102,7 @@ namespace Volo.Abp.Modularity ((IPostConfigureServices)module.Instance).PostConfigureServices(services); } } - + protected virtual void SetModuleDependencies(List modules, AbpModuleDescriptor module) { foreach (var dependedModuleType in AbpModuleHelper.FindDependedModuleTypes(module.Type)) diff --git a/src/Volo.Abp/Volo/Abp/Modularity/ModuleManager.cs b/src/Volo.Abp/Volo/Abp/Modularity/ModuleManager.cs index ab273507bb..bbb629d8d7 100644 --- a/src/Volo.Abp/Volo/Abp/Modularity/ModuleManager.cs +++ b/src/Volo.Abp/Volo/Abp/Modularity/ModuleManager.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using Microsoft.Extensions.Logging; using Volo.DependencyInjection; namespace Volo.Abp.Modularity @@ -11,17 +12,22 @@ namespace Volo.Abp.Modularity private readonly List _modules; private readonly IEnumerable _lifecycleContributers; + private readonly ILogger _logger; public ModuleManager( AbpApplication application, - IEnumerable lifecycleContributers) + IEnumerable lifecycleContributers, + ILogger logger) { _lifecycleContributers = lifecycleContributers; + _logger = logger; _modules = application.Modules.ToList(); } public void InitializeModules(ApplicationInitializationContext context) { + LogListOfModules(); + foreach (var contributer in _lifecycleContributers) { foreach (var module in Modules) @@ -29,6 +35,18 @@ namespace Volo.Abp.Modularity contributer.Initialize(context, module.Instance); } } + + _logger.LogInformation("Initialized all modules."); + } + + private void LogListOfModules() + { + _logger.LogInformation("Loaded modules:"); + + foreach (var module in Modules) + { + _logger.LogInformation("- " + module.Type.FullName); + } } public void ShutdownModules(ApplicationShutdownContext context)