Skip to content

Commit

Permalink
Add Example04
Browse files Browse the repository at this point in the history
  • Loading branch information
Aymen TROUDI authored and Aymen TROUDI committed Aug 11, 2023
1 parent 1d18db7 commit d824c43
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 3 deletions.
30 changes: 30 additions & 0 deletions PluginArchitectureDemo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.github\workflows\ci.yml = .github\workflows\ci.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Example04", "Example04", "{D4352BB2-1417-4E8D-B1C3-89461B0FED9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example04.App", "src\Example04\Example04.App\Example04.App.csproj", "{7D17375E-8C3B-4636-ABF2-8B6583A47501}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example04.Core", "src\Example04\Example04.Core\Example04.Core.csproj", "{3E0532D0-9DC3-4872-80BE-54888E569A13}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example04.Plugin.Slack", "src\Example04\Example04.Plugin.Slack\Example04.Plugin.Slack.csproj", "{A1854573-3314-404F-AC71-DA29CF13E25A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example04.Plugin.Teams", "src\Example04\Example04.Plugin.Teams\Example04.Plugin.Teams.csproj", "{7DFFF413-39CF-4A55-B798-846E34753572}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -93,6 +103,22 @@ Global
{0A10701A-E04F-4F1A-9EC0-A38A21FD8673}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A10701A-E04F-4F1A-9EC0-A38A21FD8673}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A10701A-E04F-4F1A-9EC0-A38A21FD8673}.Release|Any CPU.Build.0 = Release|Any CPU
{7D17375E-8C3B-4636-ABF2-8B6583A47501}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D17375E-8C3B-4636-ABF2-8B6583A47501}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D17375E-8C3B-4636-ABF2-8B6583A47501}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D17375E-8C3B-4636-ABF2-8B6583A47501}.Release|Any CPU.Build.0 = Release|Any CPU
{3E0532D0-9DC3-4872-80BE-54888E569A13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E0532D0-9DC3-4872-80BE-54888E569A13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E0532D0-9DC3-4872-80BE-54888E569A13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E0532D0-9DC3-4872-80BE-54888E569A13}.Release|Any CPU.Build.0 = Release|Any CPU
{A1854573-3314-404F-AC71-DA29CF13E25A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1854573-3314-404F-AC71-DA29CF13E25A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1854573-3314-404F-AC71-DA29CF13E25A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1854573-3314-404F-AC71-DA29CF13E25A}.Release|Any CPU.Build.0 = Release|Any CPU
{7DFFF413-39CF-4A55-B798-846E34753572}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7DFFF413-39CF-4A55-B798-846E34753572}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DFFF413-39CF-4A55-B798-846E34753572}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DFFF413-39CF-4A55-B798-846E34753572}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -110,5 +136,9 @@ Global
{3E6926B7-E6C7-457A-9339-9ACEF6C4C8EE} = {D35A7AF1-3B8D-45BB-96C9-A2C570A568F3}
{D33C62D9-FE58-4A59-94C9-DBB361016219} = {D35A7AF1-3B8D-45BB-96C9-A2C570A568F3}
{0A10701A-E04F-4F1A-9EC0-A38A21FD8673} = {D35A7AF1-3B8D-45BB-96C9-A2C570A568F3}
{7D17375E-8C3B-4636-ABF2-8B6583A47501} = {D4352BB2-1417-4E8D-B1C3-89461B0FED9F}
{3E0532D0-9DC3-4872-80BE-54888E569A13} = {D4352BB2-1417-4E8D-B1C3-89461B0FED9F}
{A1854573-3314-404F-AC71-DA29CF13E25A} = {D4352BB2-1417-4E8D-B1C3-89461B0FED9F}
{7DFFF413-39CF-4A55-B798-846E34753572} = {D4352BB2-1417-4E8D-B1C3-89461B0FED9F}
EndGlobalSection
EndGlobal
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Using various ways to illustrate plugin architecture
>
> :three: `Example03` use [autofac](https://github.com/autofac/Autofac) in order to load plugins
>
> :four: `Example04` use [simple injector](https://github.com/simpleinjector/SimpleInjector) in order to load plugins
>
> :bulb: Plugins are copied on `plugins` folder on post build events and loaded at startup by the application.
>
**`Tools`** : vs22, net 6.0, autofac, scrutor
**`Tools`** : vs22, net 6.0, autofac, simple-injector, scrutor
Binary file added plugins/Example04.Plugin.Slack.dll
Binary file not shown.
Binary file added plugins/Example04.Plugin.Teams.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Example01.App;

public static class ServiceCollectionExtensions
public static class DependencyInjectionExtensions
{
public static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Example02.App;

public static class ServiceCollectionExtensions
public static class DependencyInjectionExtensions
{
public static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration)
{
Expand Down
47 changes: 47 additions & 0 deletions src/Example04/Example04.App/ApplicationPackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Reflection;
using Example04.Core;
using Microsoft.Extensions.Configuration;
using SimpleInjector;
using SimpleInjector.Packaging;

namespace Example04.App;

public class ApplicationPackage : IPackage
{
private readonly IConfiguration _configuration;

public ApplicationPackage(IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}

public void RegisterServices(Container container)
{
var settings = GetSettings();
var assemblies = Directory.EnumerateFiles(settings.PluginsPath, settings.PluginsPattern)
.Select(Assembly.LoadFrom)
.ToList();
container.Collection.Register<INotificationService>(assemblies);
}

private Settings GetSettings()
{
var path = _configuration.GetValue<string>("Settings:PluginsPath");
if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path))
{
throw new ArgumentException($"Path '{path}' is not valid.");
}

var pattern = _configuration.GetValue<string>("Settings:PluginsPattern");
if (string.IsNullOrWhiteSpace(pattern))
{
throw new ArgumentException($"Pattern '{pattern}' is not valid.");
}

return new Settings
{
PluginsPath = path,
PluginsPattern = pattern
};
}
}
13 changes: 13 additions & 0 deletions src/Example04/Example04.App/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Extensions.Configuration;
using SimpleInjector;

namespace Example04.App;

public static class DependencyInjectionExtensions
{
public static void RegisterApplicationPackage(this Container container, IConfiguration configuration)
{
var package = new ApplicationPackage(configuration);
package.RegisterServices(container);
}
}
27 changes: 27 additions & 0 deletions src/Example04/Example04.App/Example04.App.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Example04.Core\Example04.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="SimpleInjector" Version="5.4.1" />
<PackageReference Include="SimpleInjector.Packaging" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/Example04/Example04.App/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Example04.App;
using Example04.Core;
using Microsoft.Extensions.Configuration;
using SimpleInjector;

var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();

var container = new Container
{
Options = { EnableAutoVerification = true }
};

container.RegisterApplicationPackage(configuration);

var user = new User();
var notificationServices = container.GetAllInstances<INotificationService>();
foreach (var notificationService in notificationServices)
{
await notificationService.NotifyAsync(user);
}
7 changes: 7 additions & 0 deletions src/Example04/Example04.App/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Example04.App;

public class Settings
{
public string PluginsPath { get; init; }
public string PluginsPattern { get; init; }
}
12 changes: 12 additions & 0 deletions src/Example04/Example04.App/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning"
}
},
"Settings": {
"PluginsPath": "..\\..\\..\\..\\..\\..\\plugins",
"PluginsPattern": "Example04.Plugin.*.dll"
}
}
8 changes: 8 additions & 0 deletions src/Example04/Example04.Core/Example04.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Example04/Example04.Core/INotificationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Example04.Core;

public interface INotificationService
{
Task NotifyAsync(User user, CancellationToken cancellationToken = default);
}
6 changes: 6 additions & 0 deletions src/Example04/Example04.Core/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Example04.Core;

public class User
{
public Guid Id { get; init; } = Guid.NewGuid();
}
16 changes: 16 additions & 0 deletions src/Example04/Example04.Plugin.Slack/Example04.Plugin.Slack.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Example04.Core\Example04.Core.csproj" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Debug'">
<Exec Command="copy /Y $(TargetDir)$(TargetName).dll $(SolutionDir)plugins" />
</Target>

</Project>
12 changes: 12 additions & 0 deletions src/Example04/Example04.Plugin.Slack/SlackNotificationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Example04.Core;

namespace Example04.Plugin.Slack;

public class SlackNotificationService : INotificationService
{
public async Task NotifyAsync(User user, CancellationToken cancellationToken)
{
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
Console.WriteLine($"Slack notification sent to '{user.Id}'.");
}
}
16 changes: 16 additions & 0 deletions src/Example04/Example04.Plugin.Teams/Example04.Plugin.Teams.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Example04.Core\Example04.Core.csproj" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Debug'">
<Exec Command="copy /Y $(TargetDir)$(TargetName).dll $(SolutionDir)plugins" />
</Target>

</Project>
12 changes: 12 additions & 0 deletions src/Example04/Example04.Plugin.Teams/TeamNotificationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Example04.Core;

namespace Example04.Plugin.Teams;

public class TeamNotificationService : INotificationService
{
public async Task NotifyAsync(User user, CancellationToken cancellationToken)
{
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
Console.WriteLine($"Teams notification sent to '{user.Id}'.");
}
}

0 comments on commit d824c43

Please sign in to comment.