using System;
using System.Reflection;
using Abp.Auditing;
using Abp.Authorization;
using Abp.Configuration.Startup;
using Abp.Dependency;
using Abp.Dependency.Installers;
using Abp.Domain.Uow;
using Abp.EntityHistory;
using Abp.Modules;
using Abp.PlugIns;
using Abp.Runtime.Validation.Interception;
using Castle.Core.Logging;
using Castle.MicroKernel.Registration;
using JetBrains.Annotations;
namespace Abp
{
///
/// This is the main class that is responsible to start entire ABP system.
/// Prepares dependency injection and registers core components needed for startup.
/// It must be instantiated and initialized (see ) first in an application.
///
public class AbpBootstrapper : IDisposable
{
///
/// Get the startup module of the application which depends on other used modules.
///
public Type StartupModule { get; }
///
/// A list of plug in folders.
///
public PlugInSourceList PlugInSources { get; }
///
/// Gets IIocManager object used by this class.
///
public IIocManager IocManager { get; }
///
/// Is this object disposed before?
///
protected bool IsDisposed;
private AbpModuleManager _moduleManager;
private ILogger _logger;
///
/// Creates a new instance.
///
/// Startup module of the application which depends on other used modules. Should be derived from .
/// An action to set options
private AbpBootstrapper([NotNull] Type startupModule, [CanBeNull] Action optionsAction = null)
{
Check.NotNull(startupModule, nameof(startupModule));
var options = new AbpBootstrapperOptions();
optionsAction?.Invoke(options);
if (!typeof(AbpModule).GetTypeInfo().IsAssignableFrom(startupModule))
{
throw new ArgumentException($"{nameof(startupModule)} should be derived from {nameof(AbpModule)}.");
}
StartupModule = startupModule;
IocManager = options.IocManager;
PlugInSources = options.PlugInSources;
_logger = NullLogger.Instance;
if (!options.DisableAllInterceptors)
{
AddInterceptorRegistrars();
}
}
///
/// Creates a new instance.
///
/// Startup module of the application which depends on other used modules. Should be derived from .
/// An action to set options
public static AbpBootstrapper Create([CanBeNull] Action optionsAction = null)
where TStartupModule : AbpModule
{
return new AbpBootstrapper(typeof(TStartupModule), optionsAction);
}
///
/// Creates a new instance.
///
/// Startup module of the application which depends on other used modules. Should be derived from .
/// An action to set options
public static AbpBootstrapper Create([NotNull] Type startupModule, [CanBeNull] Action optionsAction = null)
{
return new AbpBootstrapper(startupModule, optionsAction);
}
///
/// Creates a new instance.
///
/// Startup module of the application which depends on other used modules. Should be derived from .
/// IIocManager that is used to bootstrap the ABP system
[Obsolete("Use overload with parameter type: Action optionsAction")]
public static AbpBootstrapper Create([NotNull] IIocManager iocManager)
where TStartupModule : AbpModule
{
return new AbpBootstrapper(typeof(TStartupModule), options =>
{
options.IocManager = iocManager;
});
}
///
/// Creates a new instance.
///
/// Startup module of the application which depends on other used modules. Should be derived from .
/// IIocManager that is used to bootstrap the ABP system
[Obsolete("Use overload with parameter type: Action optionsAction")]
public static AbpBootstrapper Create([NotNull] Type startupModule, [NotNull] IIocManager iocManager)
{
return new AbpBootstrapper(startupModule, options =>
{
options.IocManager = iocManager;
});
}
private void AddInterceptorRegistrars()
{
ValidationInterceptorRegistrar.Initialize(IocManager);
AuditingInterceptorRegistrar.Initialize(IocManager);
EntityHistoryInterceptorRegistrar.Initialize(IocManager);
UnitOfWorkRegistrar.Initialize(IocManager);
AuthorizationInterceptorRegistrar.Initialize(IocManager);
}
///
/// Initializes the ABP system.
///
public virtual void Initialize()
{
ResolveLogger();
try
{
RegisterBootstrapper();
IocManager.IocContainer.Install(new AbpCoreInstaller());
IocManager.Resolve().PlugInSources.AddRange(PlugInSources);
IocManager.Resolve().Initialize();
_moduleManager = IocManager.Resolve();
_moduleManager.Initialize(StartupModule);
_moduleManager.StartModules();
}
catch (Exception ex)
{
_logger.Fatal(ex.ToString(), ex);
throw;
}
}
private void ResolveLogger()
{
if (IocManager.IsRegistered())
{
_logger = IocManager.Resolve().Create(typeof(AbpBootstrapper));
}
}
private void RegisterBootstrapper()
{
if (!IocManager.IsRegistered())
{
IocManager.IocContainer.Register(
Component.For().Instance(this)
);
}
}
///
/// Disposes the ABP system.
///
public virtual void Dispose()
{
if (IsDisposed)
{
return;
}
IsDisposed = true;
_moduleManager?.ShutdownModules();
}
}
}