-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aymen TROUDI
authored and
Aymen TROUDI
committed
Aug 11, 2023
1 parent
1d18db7
commit d824c43
Showing
19 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/Example04/Example04.App/DependencyInjectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/Example04/Example04.Plugin.Slack/Example04.Plugin.Slack.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/Example04/Example04.Plugin.Slack/SlackNotificationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/Example04/Example04.Plugin.Teams/Example04.Plugin.Teams.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/Example04/Example04.Plugin.Teams/TeamNotificationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'."); | ||
} | ||
} |