core bootstrap overview - grecosoft/NetFusion GitHub Wiki
Bootstrap Overview
This section explains the steps a microservice follows when NetFusion is used to bootstrap the executing host. NetFusion allows a host process to be configured from a set of plugins whose services are added to IServiceCollection. The IServiceCollection instance is available when Microsoft's WebApplicationBuilder is created and initialized during host startup.
Key Benefits
- The role of a plugin is to encapsulate the creation of services added to IServiceCollection. This has advantages when building a microservice based solution consisting of several reusable service components.
- Initialization and configuration provided by plugins allow all microservices to be consistently configured without having to understand the internal details.
- Since plugins encapsulate the setup and configuration of services, they can be easily shared between microservices.
- Each reusable plugin is deployed as a NuGet package allowing each microservice to be individually updated and deployed.
- Core plugins, containing reusable infrastructure logic, can be shared between microservices greatly reducing the code that must be written.
- Since all plugins follow the same bootstrap process and are structured similarly, code becomes easier to understand and maintain.
Components
The below diagram shows the components from which the bootstrap process is built. Each of these components and their responsibilities will be discussed in the remaining topic sections. View the Design section for a description of each class contained with the diagram.
The diagram shows the main components that are responsible for bootstrapping the microservice from a registered set of Plugins. The main objective of the plugin design is to allow easy configuration and sharing of Plugins between microservices. This allows the consuming Host application to simply register the plugin and consume the plugin's provided services. The host does not need to understand the internal implementation or how the services should be registered. Since all plugins consist of modules that are bootstrapped the same, sharing and understanding plugins becomes much easier. The below discussed classes are within the following NuGet Packages:
- NetFusion.Core.Bootstrap
- NetFusion.Core.Builder
Plugin
Plugins are responsible for the following:
-
Contains metadata about the plugin such as Name, ID, Description, Source Url, and Documentation Url.
-
Registers one or more Modules.
-
Registers zero or more Plugin Configurations.
-
Defines Extension method used to add Plugin to the CompositeBuilder.
-
Plugins are defined as one of the following types:
- Host: There can only be one. This is either a WebApi or Console executable.
- Application: One or more plugins containing application specific code.
- Core: A plugin reusable across applications. Usually based on a specific technology.
Module
Contains code implemented by the Plugin executed at specific stages during the bootstrap process. Modules preform the following:
- Registers services within IServiceCollection.
- Scan for types to register with IServiceCollection.
- Preform initialization logic based on found concrete types implementing an abstract interface defined by the plugin. This is how one plugin integrates with another. (i.e.: Plugin defines IWidget and the bootstrap process finds all concrete implementations, defined by plugins, and creates instances.
CompositeContainerBuilder
The CompositeContainerBuilder is responsible for the following:
- Called by the Host application.
- Provides an API for adding Plugins from which the Microservice is built.
- Provides an API allowing plugin configurations to be overridden.
- Creates an instance of ComponentContainer associated with the Microsoft's ServiceCollection and Configuration abstractions.
- Provides Compose method to initialize the bootstrap process.
- All method calls delegate to the CompositeContainer instance.
CompositeContainer
The CompositeContainer is responsible for the following:
- Responsible for managing the collection of registered Plugins and Configurations.
- Creates an instance of CompositeAppBuilder to which it delegates to build the CompositeApp instance from provided Plugins.
CompositeAppBuilder
The CompositeAppBuilder is responsible for implementing the bootstrap workflow and preforms the following:
- Validates all plugins.
- Composes all plugin Module properties of the following types:
(think of this as a very simplified version Microsoft MEF specifically designed for bootstrapping a microservice)
- IPluginModuleService: For all properties derived from IPluginModuleService, finds the corresponding Module implementing the type of the defined property. This allows one plugin module to reference another.
- IPluginKnownType: For all properties that are an enumerable of a IPluginKnownType derived type, finds all concrete classes from all plugins and creates a collection of instances. This allows a plugin to define abstract interfaces that are implemented by concrete types contained in other plugins.
CompositeApp
The CompositeApp represents the created composite-application and is provides the following:
- Provides Start and Stop methods used to start and stop all plugin modules.
- Provides a root object that can be used to create new lifetime scopes (only used if outside of a web-request)
- Provides access to a log with details of all the registered plugins.
- Provides central method for determining the overall health of the composite-application.