/// <summary> /// Create module instance. /// </summary> /// <param name="host"></param> protected Module(IModuleHost host) { Host = host; Services = host.Services; Logger = Services.GetRequiredService <ILogger <Module> >(); _manifest = new Lazy <ModuleManifest>(() => Host.GetManifest(GetType())); }
/// <summary> /// Get option that belongs to the module. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="host"></param> /// <param name="provider"></param> /// <param name="moduleType"></param> /// <returns></returns> public static T GetOption <T>(this IModuleHost host, IServiceProvider provider, Type moduleType) where T : class { var manifest = host.GetManifest(moduleType); var type = typeof(T); return(manifest.Options.Any(x => x == type) ? provider.GetRequiredService <IOptionsSnapshot <T> >().Value : throw new ModuleNotFoundException($"No such option for the module {moduleType.FullName}: {type.FullName}.")); }
/// <summary> /// Get service that belongs to the module. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="host"></param> /// <param name="provider"></param> /// <param name="moduleType"></param> /// <returns></returns> public static T GetService <T>(this IModuleHost host, IServiceProvider provider, Type moduleType) where T : notnull { var manifest = host.GetManifest(moduleType); var type = typeof(T); return(manifest.Services.Any(x => x.ServiceType == type) ? provider.GetRequiredService <T>() : throw new ModuleNotFoundException($"No such service for the module {moduleType.FullName}: {type.FullName}.")); }
public static IModuleHost HasServices <T>(this IModuleHost host) where T : IModule { host.HasModule <T>(); var manifest = host.GetManifest <T>(); foreach (var item in manifest.Services) { host.HasService(item.ServiceType); } return(host); }
public static IModuleHost HasDependencies <T>(this IModuleHost host) where T : IModule { host.HasModule <T>(); var manifest = host.GetManifest <T>(); foreach (var item in manifest.Dependencies) { host.HasModule(item); } return(host); }
/// <summary> /// Get manifest for the module. /// </summary> /// <typeparam name="TModule"></typeparam> /// <param name="host"></param> /// <returns></returns> public static ModuleManifest GetManifest <TModule>(this IModuleHost host) where TModule : IModule => host.GetManifest(typeof(TModule));