From 75118537403a3ec05cb7ddac38dc646ff26b2ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 26 Jul 2017 14:44:09 +0300 Subject: [PATCH] Refactored. --- .../AbpDesk/ConsoleDemo/Program.cs | 17 +++------ src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs | 3 ++ .../Volo/Abp/AbpApplicationFactory.cs | 30 ++++++++------- ...pApplicationWithInternalServiceProvider.cs | 9 ++--- src/Volo.Abp/Volo/Abp/IAbpApplication.cs | 20 ++++++++++ .../Abp/AbpApplication_Initialize_Tests.cs | 37 +++++++------------ 6 files changed, 64 insertions(+), 52 deletions(-) diff --git a/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/Program.cs b/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/Program.cs index f64bc5719a..c5be1cd0a7 100644 --- a/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/Program.cs +++ b/src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/Program.cs @@ -23,25 +23,20 @@ namespace AbpDesk.ConsoleDemo private static void RunDemo() { - var services = new ServiceCollection(); - - using (var application = services.AddApplication(options => + using (var application = AbpApplicationFactory.Create(options => { options.UseAutofac(); AddPlugIns(options); })) { - using (var scope = services.BuildServiceProviderFromFactory().CreateScope()) - { - application.Initialize(scope.ServiceProvider); + application.Initialize(); - RunListers(application); + RunListers(application); - Console.WriteLine("Press ENTER to exit..."); - Console.ReadLine(); + Console.WriteLine("Press ENTER to exit..."); + Console.ReadLine(); - application.Shutdown(); - } + application.Shutdown(); } } diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs index a220c53535..ad4941451f 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationBase.cs @@ -13,6 +13,8 @@ namespace Volo.Abp public IServiceProvider ServiceProvider { get; protected set; } + public IServiceCollection Services { get; } + [NotNull] public AbpModuleDescriptor[] Modules { get; } @@ -25,6 +27,7 @@ namespace Volo.Abp Check.NotNull(services, nameof(services)); StartupModuleType = startupModuleType; + Services = services; var options = new AbpApplicationCreationOptions(services); optionsAction?.Invoke(options); diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationFactory.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationFactory.cs index f77cb136b2..9cfd7e56fa 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationFactory.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationFactory.cs @@ -7,32 +7,36 @@ namespace Volo.Abp { public static class AbpApplicationFactory { - public static IAbpApplicationWithExternalServiceProvider Create( - [NotNull] IServiceCollection services) + // IAbpApplicationWithExternalServiceProvider ////////////////////////////////////// + + public static IAbpApplicationWithInternalServiceProvider Create( + [CanBeNull] Action optionsAction = null) where TStartupModule : IAbpModule { - return Create(services, null); + return Create(typeof(TStartupModule), optionsAction); } - public static IAbpApplicationWithExternalServiceProvider Create( - [NotNull] IServiceCollection services, - [CanBeNull] Action optionsAction) - where TStartupModule : IAbpModule + public static IAbpApplicationWithInternalServiceProvider Create( + [NotNull] Type startupModuleType, + [CanBeNull] Action optionsAction = null) { - return new AbpApplicationWithExternalServiceProvider(typeof(TStartupModule), services, optionsAction); + return new AbpApplicationWithInternalServiceProvider(startupModuleType, optionsAction); } - public static IAbpApplicationWithExternalServiceProvider Create( - [NotNull] Type startupModuleType, - [NotNull] IServiceCollection services) + // IAbpApplicationWithExternalServiceProvider ////////////////////////////////////// + + public static IAbpApplicationWithExternalServiceProvider Create( + [NotNull] IServiceCollection services, + [CanBeNull] Action optionsAction = null) + where TStartupModule : IAbpModule { - return Create(startupModuleType, services, null); + return Create(typeof(TStartupModule), services, optionsAction); } public static IAbpApplicationWithExternalServiceProvider Create( [NotNull] Type startupModuleType, [NotNull] IServiceCollection services, - [CanBeNull] Action optionsAction) + [CanBeNull] Action optionsAction = null) { return new AbpApplicationWithExternalServiceProvider(startupModuleType, services, optionsAction); } diff --git a/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs b/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs index bf34158eaf..afcf18c419 100644 --- a/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs +++ b/src/Volo.Abp/Volo/Abp/AbpApplicationWithInternalServiceProvider.cs @@ -7,7 +7,7 @@ namespace Volo.Abp { internal class AbpApplicationWithInternalServiceProvider : AbpApplicationBase, IAbpApplicationWithInternalServiceProvider { - public IServiceScope ServiceScope { get; } + public IServiceScope ServiceScope { get; private set; } public AbpApplicationWithInternalServiceProvider( [NotNull] Type startupModuleType, @@ -29,14 +29,13 @@ namespace Volo.Abp services, optionsAction) { - services.AddSingleton(_ => this); - - ServiceScope = services.BuildServiceProviderFromFactory().CreateScope(); - ServiceProvider = ServiceScope.ServiceProvider; + Services.AddSingleton(_ => this); } public void Initialize() { + ServiceScope = Services.BuildServiceProviderFromFactory().CreateScope(); + ServiceProvider = ServiceScope.ServiceProvider; ServiceProvider .GetRequiredService() .InitializeModules(new ApplicationInitializationContext(ServiceProvider)); diff --git a/src/Volo.Abp/Volo/Abp/IAbpApplication.cs b/src/Volo.Abp/Volo/Abp/IAbpApplication.cs index 501a6c5efe..a5b961a409 100644 --- a/src/Volo.Abp/Volo/Abp/IAbpApplication.cs +++ b/src/Volo.Abp/Volo/Abp/IAbpApplication.cs @@ -1,16 +1,36 @@ using System; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; namespace Volo.Abp { public interface IAbpApplication : IDisposable { + /// + /// Type of the startup (entrance) module of the application. + /// Type StartupModuleType { get; } + /// + /// List of services registered to this application. + /// Can not add new services to this collection after application initialize. + /// + IServiceCollection Services { get; } + + /// + /// Reference to the root service provider used by the application. + /// This can not be used before initialize the application. + /// IServiceProvider ServiceProvider { get; } + /// + /// List of modules loaded into this application. + /// AbpModuleDescriptor[] Modules { get; } + /// + /// Used to gracefully shutdown the application and all modules. + /// void Shutdown(); } } \ No newline at end of file diff --git a/test/Volo.Abp.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs b/test/Volo.Abp.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs index 404aedf588..9737a9a53e 100644 --- a/test/Volo.Abp.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs +++ b/test/Volo.Abp.Tests/Volo/Abp/AbpApplication_Initialize_Tests.cs @@ -11,54 +11,45 @@ namespace Volo.Abp [Fact] public void Should_Initialize_Single_Module() { - //Arrange - var services = new ServiceCollection(); - using (var application = services.AddApplication()) + using (var application = AbpApplicationFactory.Create()) { //Assert - var module = services.GetSingletonInstance(); + var module = application.Services.GetSingletonInstance(); module.PreConfigureServicesIsCalled.ShouldBeTrue(); module.ConfigureServicesIsCalled.ShouldBeTrue(); module.PostConfigureServicesIsCalled.ShouldBeTrue(); - using (var scope = services.BuildServiceProvider().CreateScope()) - { - //Act - application.Initialize(scope.ServiceProvider); + //Act + application.Initialize(); - //Assert - application.ServiceProvider.GetRequiredService().ShouldBeSameAs(module); - module.OnApplicationInitializeIsCalled.ShouldBeTrue(); + //Assert + application.ServiceProvider.GetRequiredService().ShouldBeSameAs(module); + module.OnApplicationInitializeIsCalled.ShouldBeTrue(); - //Act - application.Shutdown(); + //Act + application.Shutdown(); - //Assert - module.OnApplicationShutdownIsCalled.ShouldBeTrue(); - } + //Assert + module.OnApplicationShutdownIsCalled.ShouldBeTrue(); } } [Fact] public void Should_Initialize_PlugIn() { - //Arrange - var services = new ServiceCollection(); - - //Act - using (var application = services.AddApplication(options => + using (var application = AbpApplicationFactory.Create(options => { options.PlugInSources.AddTypes(typeof(IndependentEmptyPlugInModule)); })) { //Assert - var plugInModule = services.GetSingletonInstance(); + var plugInModule = application.Services.GetSingletonInstance(); plugInModule.PreConfigureServicesIsCalled.ShouldBeTrue(); plugInModule.ConfigureServicesIsCalled.ShouldBeTrue(); plugInModule.PostConfigureServicesIsCalled.ShouldBeTrue(); //Act - application.Initialize(services.BuildServiceProvider()); + application.Initialize(); //Assert application.ServiceProvider.GetRequiredService().ShouldBeSameAs(plugInModule);