Geranium.Modules
1.0.32
dotnet add package Geranium.Modules --version 1.0.32
NuGet\Install-Package Geranium.Modules -Version 1.0.32
<PackageReference Include="Geranium.Modules" Version="1.0.32" />
paket add Geranium.Modules --version 1.0.32
#r "nuget: Geranium.Modules, 1.0.32"
// Install Geranium.Modules as a Cake Addin #addin nuget:?package=Geranium.Modules&version=1.0.32 // Install Geranium.Modules as a Cake Tool #tool nuget:?package=Geranium.Modules&version=1.0.32
Geranium.Modules
Modules abstraction for unification of the bridge between other module systems and the registration abstraction.
Usage example
Define one or multiple classes in assemlby inherit from BaseModule
or BaseModule<T>
in second case T
is configuration type.
Define modules
// assembly "data.dll"
public class DataModule : BaseModule
{
public override void Install()
{
this.RegisterScoped<IRepository, MemoryRepository>(); // register implementation
}
}
// assembly "app.dll"
public class AppModule : BaseModule
{
protected override void SetDependencies()
{
this.DependsOn<DataModule>(); // setting dependency of DataModule
}
}
Installing modules
// assembly "app.dll"
public class Program
{
private static void Main(string[] args)
{
// prepare bridge and others
var builder = WebApplication.CreateBuilder(args);
// installing modules
// <--------------------- main part ------------------------>
var installinfo = ModuleInstaller.Install(/* optional bridge */);
// modules installed, register other components
builder.Services.AddTransient<AppService,AppService>();
}
}
Result
// assembly "app.dll"
public class AppService
{
public AppService(IRepository repository)
{
// repository here is 'MemoryRepository' from "data.dll", injected by your IoC container
}
}
How it works
The main idea of these modules is the unification of the bridge between other module systems and the abstraction of registration.
Installing
There is three main steps in module intalling:
- Getting assemblies: by default, all assemblies in folder parse by
Mono.Cecil
and check isIModule
implementations exists in assembly. - Extract
IModule
s from assemblies: by default, all classes derived fromBaseModule
/BaseModule<T>
returns from assembly, this means you can have multiple modules in one assembly. - Install modules: sorting modules by
Toposort
, creating cfg classes, bind toIModule
instances, and callIModule.Install()
.
Bridge
You can pass IServiceBridge
implementation into ModuleInstaller.Install
class (and all others in ModulesInstallSettings
), which do main 'magic trick':
- Extracted
IModule
s registering in DI byIServiceBridge.RegisterSingleton
method - For installing,
IModule
s getting from DI byIServiceBridge.ResolveService(moduleType)
. By this step, you can use full power of your DI library. - When you call
this.Register***
methods inInstall
void of module, it call'sIServiceBridge.Register***
implementation.
Extensions
ModulesInstallSettings
contains six options which provide ability controlling module installing and registering components in modules. You can pass settings to ModuleInstaller.Install
.
IServiceBridge
Core abstraction of library: this "bridge" between your DI system/library and Geranium.Modules
:
Register***
methods calling inModule.Install()
and using for registering components implementationsResolveService's
methods calling for gettingIModule
instances with fulfilled dependencies from your DI
You can pass IServiceBridge
directly into ModuleInstaller.Install
overloading.
ConfigurationProviderFunc
You can pass your own delegate object ConfigurationProviderFunc(Type moduleType, Type configType)
for provide config instances.
AssmyblyProviderFunc and AssemblyLoadContext
You can pass your own delegate Assembly[] AssmyblyProviderFunc(AssemblyLoadContext loadContext)
for provide Assembly[]
from modules will be extracted, additionaly, if you want unload assemblies, you can pass your own AssemblyLoadContext
.
ExtractModulesFunc
You can pass your own delegate ModuleInfo[] ExtractModulesFunc(Assembly[] assemblies)
for getting module information from assemblies.
InstallModulesFunc
You can pass your own delegate void InstallModulesFunc(ModuleInfo[] modules, IServiceBridge services, ConfigurationProviderFunc cfgProvider)
for installing modules, this is most important part.
Localization
Inside DefaultModuleInstaller.InstallModules
function couple of times calling ILogger.Log
with success and (possibly) error messages. You can localize this messages by passing custom INStringLocalizer<ModuleInstallingLoggerMessages>
.
When ILogger.Log
will call, strings will be requested to IStringLocalizer
, by key of ModuleInstallingLoggerMessages
properties, for example: IStringLocalizer["DependenciesNotInstalled"]
, if there is no value for key, will be request whole phrase : IStringLocalizer["Module dependencies not installed"]
.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Geranium.Reflection (>= 2.0.1)
- Geranium.Toposort (>= 1.0.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.4)
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- Microsoft.Extensions.Localization (>= 7.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0)
- Mono.Cecil (>= 0.11.5)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Geranium.Modules:
Package | Downloads |
---|---|
Geranium.Modules.CastleWindsor
CastleWindsor bridge for Geranium.Modules |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.32 | 178 | 5/17/2023 |