Skip to content

Commit

Permalink
Avoid exposing the method called ConfigureWebApplicationBuilder to th…
Browse files Browse the repository at this point in the history
…e plugins

* Replace the PluginStartup interface with IDependencyServicesRegisterer (it is a more descriptive name)

* Replace the ConfigureWebApplicationBuilder method with RegisterServices(IServiceCollection, IConfiguration).

The consumer should not be able to use the type called WebApplicationBuilder in its plugins, because it has the absolute freedom to build the web application through the Build() method and this does not make sense, since it is the responsibility of the host application.
  • Loading branch information
MrDave1999 committed Mar 6, 2024
1 parent aa77129 commit b879c7a
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 63 deletions.
8 changes: 4 additions & 4 deletions src/HostApplication/PluginStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
public static class PluginStartup
{
/// <summary>
/// Configures the application plug-ins.
/// Configures the plugins.
/// </summary>
public static void Configure(WebApplicationBuilder builder)
{
var envConfiguration = new CPluginEnvConfiguration();
PluginLoader.Load(envConfiguration);
var startups = TypeFinder.FindSubtypesOf<IPluginStartup>();
foreach (var pluginStartup in startups)
pluginStartup.ConfigureWebApplicationBuilder(builder);
var dependencyServicesRegisterers = TypeFinder.FindSubtypesOf<IDependencyServicesRegisterer>();
foreach (var dependencyServicesRegisterer in dependencyServicesRegisterers)
dependencyServicesRegisterer.RegisterServices(builder.Services, builder.Configuration);

var modelCreatings = TypeFinder.FindSubtypesOf<IModelCreating>();
builder.Services.AddSingleton(modelCreatings);
Expand Down
11 changes: 11 additions & 0 deletions src/Plugins/AppointmentReminders/DependencyServicesRegisterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[assembly: Plugin(typeof(DependencyServicesRegisterer))]

namespace Plugin.AppointmentReminders;

public class DependencyServicesRegisterer : IDependencyServicesRegisterer
{
public void RegisterServices(IServiceCollection services, IConfiguration configuration)
{
services.AddReminderServices();
}
}
11 changes: 0 additions & 11 deletions src/Plugins/AppointmentReminders/PluginStartup.cs

This file was deleted.

11 changes: 11 additions & 0 deletions src/Plugins/ChatBot/DependencyServicesRegisterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[assembly: Plugin(typeof(DependencyServicesRegisterer))]

namespace Plugin.ChatBot;

public class DependencyServicesRegisterer : IDependencyServicesRegisterer
{
public void RegisterServices(IServiceCollection services, IConfiguration configuration)
{
services.AddBotServices(configuration);
}
}
2 changes: 1 addition & 1 deletion src/Plugins/ChatBot/Helpers/AdaptiveCardsLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static AdaptiveCardsLoader()
s_basePath = Path.Combine(AppContext.BaseDirectory, "AdaptiveCards");
if (Directory.Exists(s_basePath)) return;

var rootNamespace = typeof(PluginStartup).Namespace;
var rootNamespace = typeof(DependencyServicesRegisterer).Namespace;
// This path is used by the Host Application.
var path2 = $"plugins/{rootNamespace}/AdaptiveCards";
s_basePath = Path.Combine(AppContext.BaseDirectory, path2);
Expand Down
11 changes: 0 additions & 11 deletions src/Plugins/ChatBot/PluginStartup.cs

This file was deleted.

14 changes: 14 additions & 0 deletions src/Plugins/SendGrid/DependencyServicesRegisterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[assembly: Plugin(typeof(DependencyServicesRegisterer))]

namespace Plugin.SendGrid;

public class DependencyServicesRegisterer : IDependencyServicesRegisterer
{
public void RegisterServices(IServiceCollection services, IConfiguration configuration)
{
var settings = new EnvBinder().Bind<SendGridSettings>();
services.AddSendGrid(options => options.ApiKey = settings.SendGridApiKey);
services.AddScoped<IEmailService, EmailService>();
services.AddSingleton(settings);
}
}
14 changes: 0 additions & 14 deletions src/Plugins/SendGrid/PluginStartup.cs

This file was deleted.

13 changes: 13 additions & 0 deletions src/Plugins/TwilioWhatsApp/DependencyServicesRegisterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[assembly: Plugin(typeof(DependencyServicesRegisterer))]

namespace Plugin.Twilio.WhatsApp;

public class DependencyServicesRegisterer : IDependencyServicesRegisterer
{
public void RegisterServices(IServiceCollection services, IConfiguration configuration)
{
var settings = new EnvBinder().Bind<TwilioSettings>();
services.AddSingleton<IInstantMessaging, WhatsAppMessaging>();
services.AddSingleton(settings);
}
}
13 changes: 0 additions & 13 deletions src/Plugins/TwilioWhatsApp/PluginStartup.cs

This file was deleted.

18 changes: 18 additions & 0 deletions src/Shared/Interfaces/IDependencyServicesRegisterer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace DentallApp.Shared.Interfaces;

/// <summary>
/// Base type for initializing services used by a plugin.
/// </summary>
public interface IDependencyServicesRegisterer
{
/// <summary>
/// Register services into the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">
/// The <see cref="IServiceCollection"/> to add the services to.
/// </param>
/// <param name="configuration">
/// The set of key/value configuration properties.
/// </param>
void RegisterServices(IServiceCollection services, IConfiguration configuration);
}
9 changes: 0 additions & 9 deletions src/Shared/Interfaces/IPluginStartup.cs

This file was deleted.

0 comments on commit b879c7a

Please sign in to comment.