mirror of https://github.com/abpframework/abp.git
22 changed files with 189 additions and 118 deletions
@ -1,23 +0,0 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Builder |
|||
{ |
|||
public class AspNetConfigurationContext |
|||
{ |
|||
public IApplicationBuilder App { get; } |
|||
|
|||
public IHostingEnvironment Environment { get; } |
|||
|
|||
public ILoggerFactory LoggerFactory { get; } |
|||
|
|||
public AspNetConfigurationContext(IApplicationBuilder app) |
|||
{ |
|||
App = app; |
|||
Environment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>(); |
|||
LoggerFactory = app.ApplicationServices.GetRequiredService<ILoggerFactory>(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace Volo.Abp.AspNetCore.Builder |
|||
{ |
|||
public interface IConfigureAspNet |
|||
{ |
|||
void Configure(AspNetConfigurationContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Modularity |
|||
{ |
|||
public static class ApplicationInitializationContextExtensions |
|||
{ |
|||
public static IApplicationBuilder GetApplicationBuilder(this ApplicationInitializationContext context) |
|||
{ |
|||
return context.ServiceProvider.GetRequiredService<IObjectAccessor<IApplicationBuilder>>().Object; |
|||
} |
|||
|
|||
public static IHostingEnvironment GetEnvironment(this ApplicationInitializationContext context) |
|||
{ |
|||
return context.ServiceProvider.GetRequiredService<IHostingEnvironment>(); |
|||
} |
|||
|
|||
public static ILoggerFactory GetLoggerFactory(this ApplicationInitializationContext context) |
|||
{ |
|||
return context.ServiceProvider.GetRequiredService<ILoggerFactory>(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Volo.Abp.AspNetCore.Builder; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Modularity |
|||
{ |
|||
public class AspNetCoreModuleInitializer : IModuleInitializer |
|||
{ |
|||
private readonly AspNetConfigurationContext _configurationContext; |
|||
|
|||
public AspNetCoreModuleInitializer(ApplicationBuilderAccessor appAccessor) |
|||
{ |
|||
_configurationContext = new AspNetConfigurationContext(appAccessor.App); |
|||
} |
|||
|
|||
public void Initialize(IAbpModule module) |
|||
{ |
|||
(module as IConfigureAspNet)?.Configure(_configurationContext); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Modularity |
|||
{ |
|||
public class ApplicationInitializationContext |
|||
{ |
|||
public IServiceProvider ServiceProvider { get; set; } |
|||
|
|||
public ApplicationInitializationContext(IServiceProvider serviceProvider) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
} |
|||
} |
|||
} |
|||
@ -1,10 +1,23 @@ |
|||
namespace Volo.Abp.Modularity |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Modularity |
|||
{ |
|||
public class DefaultModuleInitializer : IModuleInitializer |
|||
{ |
|||
private readonly IServiceProvider _serviceProvider; |
|||
|
|||
public DefaultModuleInitializer(IServiceProvider serviceProvider) |
|||
{ |
|||
_serviceProvider = serviceProvider; |
|||
} |
|||
|
|||
public void Initialize(IAbpModule module) |
|||
{ |
|||
(module as IOnApplicationInitialize)?.OnApplicationInitialize(); |
|||
var context = new ApplicationInitializationContext( |
|||
_serviceProvider |
|||
); |
|||
|
|||
(module as IOnApplicationInitialization)?.OnApplicationInitialization(context); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Modularity |
|||
{ |
|||
public interface IOnApplicationInitialization |
|||
{ |
|||
void OnApplicationInitialization(ApplicationInitializationContext context); |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace Volo.Abp.Modularity |
|||
{ |
|||
public interface IOnApplicationInitialize |
|||
{ |
|||
void OnApplicationInitialize(); |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Volo.DependencyInjection |
|||
{ |
|||
public static class CommonServiceCollectionExtensions |
|||
{ |
|||
public static T GetSingletonInstanceOrNull<T>(this IServiceCollection services) |
|||
{ |
|||
return (T)services |
|||
.FirstOrDefault(d => d.ServiceType == typeof(T)) |
|||
?.ImplementationInstance; |
|||
} |
|||
|
|||
public static T GetSingletonInstance<T>(this IServiceCollection services) |
|||
{ |
|||
var service = services.GetSingletonInstanceOrNull<T>(); |
|||
if (service == null) |
|||
{ |
|||
throw new InvalidOperationException("Could not find singleton service: " + typeof(T).AssemblyQualifiedName); |
|||
} |
|||
|
|||
return service; |
|||
} |
|||
|
|||
public static ObjectAccessor<T> AddObjectAccessor<T>(this IServiceCollection services) |
|||
{ |
|||
return services.AddObjectAccessor(new ObjectAccessor<T>()); |
|||
} |
|||
|
|||
public static ObjectAccessor<T> AddObjectAccessor<T>(this IServiceCollection services, T obj) |
|||
{ |
|||
return services.AddObjectAccessor(new ObjectAccessor<T>(obj)); |
|||
} |
|||
|
|||
public static ObjectAccessor<T> AddObjectAccessor<T>(this IServiceCollection services, ObjectAccessor<T> accessor) |
|||
{ |
|||
services.AddSingleton(typeof(IObjectAccessor<T>), accessor); |
|||
services.AddSingleton(typeof(ObjectAccessor<T>), accessor); |
|||
|
|||
return accessor; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.DependencyInjection |
|||
{ |
|||
public interface IObjectAccessor<T> |
|||
{ |
|||
T Object { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
namespace Volo.DependencyInjection |
|||
{ |
|||
public class ObjectAccessor<T> : IObjectAccessor<T> |
|||
{ |
|||
public T Object { get; set; } |
|||
|
|||
public ObjectAccessor() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public ObjectAccessor(T obj) |
|||
{ |
|||
Object = obj; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue