Skip to content

Commit

Permalink
Sinks and sources (#1)
Browse files Browse the repository at this point in the history
* support config from appsettings

* .

* make BaseEmailService.Send abstract (implemented by sink)

---------

Co-authored-by: Simone Ronzoni <[email protected]>
  • Loading branch information
fbognini and ronzoni-s authored Nov 14, 2023
1 parent 2d1330a commit 65124ea
Show file tree
Hide file tree
Showing 49 changed files with 1,072 additions and 421 deletions.
18 changes: 18 additions & 0 deletions examples/fbognini.Notifications.WorkerSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using fbognini.Notifications;
using fbognini.Notifications.Sinks.Email;
using fbognini.Notifications.Source.AppSettings;
using fbognini.Notifications.WorkerSample;
using Microsoft.Extensions.Configuration;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((ctx, services) =>
{
services.AddHostedService<Worker>();
services.AddNotifications()
.AddEmailService("SUPPORT")
.FromAppSettings(ctx.Configuration);
})
.Build();

host.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"fbognini.Notifications.WorkerSample": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
27 changes: 27 additions & 0 deletions examples/fbognini.Notifications.WorkerSample/Worker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using fbognini.Notifications.Interfaces;

namespace fbognini.Notifications.WorkerSample
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IEmailService emailService;

public Worker(ILogger<Worker> logger, IEmailService emailService)
{
_logger = logger;
this.emailService = emailService;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await emailService.SendAsync("[email protected]", "test", "body", cancellationToken: stoppingToken);

_logger.LogInformation("Email sent at: {time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
22 changes: 22 additions & 0 deletions examples/fbognini.Notifications.WorkerSample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"fbognini.Notifications": {
"EmailConfigs": [
{
"Id": "SUPPORT",
"UseSsl": false,
"SmtpHost": "localhost",
"SmtpPort": 25,
"UseAuthentication": false,
"SmtpUsername": "",
"SmtpPassword": "",
"FromEmail": "[email protected]"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\fbognini.Notifications.Sinks.Email\fbognini.Notifications.Sinks.Email.csproj" />
<ProjectReference Include="..\..\src\fbognini.Notifications.Source.AppSettings\fbognini.Notifications.Source.AppSettings.csproj" />
<ProjectReference Include="..\..\src\fbognini.Notifications\fbognini.Notifications.csproj" />
</ItemGroup>
</Project>
32 changes: 31 additions & 1 deletion fbognini.Notifications.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A70D62BA-CDE
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "fbognini.Notifications", "src\fbognini.Notifications\fbognini.Notifications.csproj", "{828551C1-CB3D-4032-B596-7BC3F814F7A2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "fbognini.Notifications.MTarget", "src\fbognini.Notifications.MTarget\fbognini.Notifications.MTarget.csproj", "{2466E5A9-CA7D-4659-B799-02C00DD44DE4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "fbognini.Notifications.Sinks.MTarget", "src\fbognini.Notifications.MTarget\fbognini.Notifications.Sinks.MTarget.csproj", "{2466E5A9-CA7D-4659-B799-02C00DD44DE4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "fbognini.Notifications.Sinks.Email", "src\fbognini.Notifications.Sinks.Email\fbognini.Notifications.Sinks.Email.csproj", "{B5B12F2C-096F-4BC6-8D37-883A22B0FB8A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "fbognini.Notifications.Source.SqlServer", "src\fbognini.Notifications.Source.SqlServer\fbognini.Notifications.Source.SqlServer.csproj", "{4BCC196D-D952-495D-BA8A-D29458BB48BE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "fbognini.Notifications.Source.AppSettings", "src\fbognini.Notifications.Source.AppSettings\fbognini.Notifications.Source.AppSettings.csproj", "{760135E9-879B-4F6D-BA1D-B9A7D9E60AF5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{83BCB33B-B121-43EC-868A-F9E3AF8F196E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fbognini.Notifications.WorkerSample", "examples\fbognini.Notifications.WorkerSample\fbognini.Notifications.WorkerSample.csproj", "{F2C621A6-5F0B-40BB-BE34-8367054E4436}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,13 +33,33 @@ Global
{2466E5A9-CA7D-4659-B799-02C00DD44DE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2466E5A9-CA7D-4659-B799-02C00DD44DE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2466E5A9-CA7D-4659-B799-02C00DD44DE4}.Release|Any CPU.Build.0 = Release|Any CPU
{B5B12F2C-096F-4BC6-8D37-883A22B0FB8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5B12F2C-096F-4BC6-8D37-883A22B0FB8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5B12F2C-096F-4BC6-8D37-883A22B0FB8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5B12F2C-096F-4BC6-8D37-883A22B0FB8A}.Release|Any CPU.Build.0 = Release|Any CPU
{4BCC196D-D952-495D-BA8A-D29458BB48BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BCC196D-D952-495D-BA8A-D29458BB48BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BCC196D-D952-495D-BA8A-D29458BB48BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BCC196D-D952-495D-BA8A-D29458BB48BE}.Release|Any CPU.Build.0 = Release|Any CPU
{760135E9-879B-4F6D-BA1D-B9A7D9E60AF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{760135E9-879B-4F6D-BA1D-B9A7D9E60AF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{760135E9-879B-4F6D-BA1D-B9A7D9E60AF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{760135E9-879B-4F6D-BA1D-B9A7D9E60AF5}.Release|Any CPU.Build.0 = Release|Any CPU
{F2C621A6-5F0B-40BB-BE34-8367054E4436}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2C621A6-5F0B-40BB-BE34-8367054E4436}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2C621A6-5F0B-40BB-BE34-8367054E4436}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2C621A6-5F0B-40BB-BE34-8367054E4436}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{828551C1-CB3D-4032-B596-7BC3F814F7A2} = {A70D62BA-CDE1-42A0-A3A4-8012641BD6EB}
{2466E5A9-CA7D-4659-B799-02C00DD44DE4} = {A70D62BA-CDE1-42A0-A3A4-8012641BD6EB}
{B5B12F2C-096F-4BC6-8D37-883A22B0FB8A} = {A70D62BA-CDE1-42A0-A3A4-8012641BD6EB}
{4BCC196D-D952-495D-BA8A-D29458BB48BE} = {A70D62BA-CDE1-42A0-A3A4-8012641BD6EB}
{760135E9-879B-4F6D-BA1D-B9A7D9E60AF5} = {A70D62BA-CDE1-42A0-A3A4-8012641BD6EB}
{F2C621A6-5F0B-40BB-BE34-8367054E4436} = {83BCB33B-B121-43EC-868A-F9E3AF8F196E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DD97CDD4-F62D-451F-BBB9-E82EADB17D24}
Expand Down
36 changes: 36 additions & 0 deletions src/fbognini.Notifications.MTarget/MTargetSinkBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using fbognini.Notifications.Interfaces;
using fbognini.Notifications.Services;
using fbognini.Notifications.Sinks.MTarget.Sdk;
using fbognini.Notifications.Sinks.MTarget.Services;
using Microsoft.Extensions.DependencyInjection;

namespace fbognini.Notifications.Sinks.MTarget
{
public class MTargetSinkBuilder : SinkBuilder
{
internal MTargetSinkBuilder(IServiceCollection services)
: base(services)
{
}

internal MTargetSinkBuilder AddMTargetService()
{
Services
.AddTransient<ISmsService, MTargetSmsService>();

return this;
}

internal MTargetSinkBuilder AddMTargetService(string id)
{
Services.AddHttpClient<IMTargetService, MTargetService>((sp, cl) =>
{
});

Services
.AddTransient<ISmsService, MTargetSmsService>((provider) => new MTargetSmsService(id, provider.GetRequiredService<ISettingsProvider>(), provider.GetRequiredService<IMTargetService>()));
return this;
}
}
}
4 changes: 2 additions & 2 deletions src/fbognini.Notifications.MTarget/Sdk/MTargetService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using fbognini.Notifications.MTarget.Sdk.Models;
using fbognini.Notifications.Sinks.MTarget.Sdk.Models;
using fbognini.Sdk;
using fbognini.Sdk.Interfaces;
using Microsoft.Extensions.Logging;
Expand All @@ -9,7 +9,7 @@
using System.Text.Json;
using System.Threading.Tasks;

namespace fbognini.Notifications.MTarget.Sdk
namespace fbognini.Notifications.Sinks.MTarget.Sdk
{

public interface IMTargetService
Expand Down
2 changes: 1 addition & 1 deletion src/fbognini.Notifications.MTarget/Sdk/MTargetSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace fbognini.Notifications.MTarget.Sdk
namespace fbognini.Notifications.Sinks.MTarget.Sdk
{
public enum MTargetEnvironment
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace fbognini.Notifications.MTarget.Sdk.Models
namespace fbognini.Notifications.Sinks.MTarget.Sdk.Models
{
public class MessageResponse
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace fbognini.Notifications.MTarget.Sdk.Models
namespace fbognini.Notifications.Sinks.MTarget.Sdk.Models
{
public class SendMessagesResponse
{
Expand Down
12 changes: 6 additions & 6 deletions src/fbognini.Notifications.MTarget/Services/MTargetSmsService.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using fbognini.Notifications.Interfaces;
using fbognini.Notifications.Models;
using fbognini.Notifications.Models.Sms;
using fbognini.Notifications.MTarget.Sdk;
using fbognini.Notifications.Sinks.MTarget.Sdk;
using fbognini.Notifications.Services;
using fbognini.Notifications.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace fbognini.Notifications.MTarget.Services
namespace fbognini.Notifications.Sinks.MTarget.Services
{
public class MTargetSmsService : BaseSmsService
{
private readonly IMTargetService mTargetService;

public MTargetSmsService(DatabaseSettings settings, IMTargetService mTargetService)
: this(null, settings, mTargetService)
public MTargetSmsService(ISettingsProvider settingsProvider, IMTargetService mTargetService)
: this(null, settingsProvider, mTargetService)
{
}


public MTargetSmsService(string id, DatabaseSettings settings, IMTargetService mTargetService)
: base(id, settings.ConnectionString, settings.Schema)
public MTargetSmsService(string id, ISettingsProvider settingsProvider, IMTargetService mTargetService)
: base(id, settingsProvider)
{
this.mTargetService = mTargetService;
this.mTargetService.ChangeSettings(new MTargetSettings()
Expand Down
31 changes: 8 additions & 23 deletions src/fbognini.Notifications.MTarget/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
using fbognini.Notifications.Interfaces;
using fbognini.Notifications.MTarget.Sdk;
using fbognini.Notifications.MTarget.Services;
using fbognini.Notifications.Services;
using fbognini.Notifications.Settings;
using Microsoft.Extensions.DependencyInjection;
using System;
using fbognini.Notifications.Services;

namespace fbognini.Notifications.MTarget
namespace fbognini.Notifications.Sinks.MTarget
{
public static class Startup
{
public static NotificationsBuilder AddMTargetService(this NotificationsBuilder builder)
public static MTargetSinkBuilder AddMTargetService(this SinkBuilder builder)
{
builder.Services
.AddTransient<ISmsService, MTargetSmsService>();

return builder;
return new MTargetSinkBuilder(builder.Services)
.AddMTargetService();
}

public static NotificationsBuilder AddMTargetService(this NotificationsBuilder builder, string id)
public static MTargetSinkBuilder AddMTargetService(this SinkBuilder builder, string id)
{
builder.Services.AddHttpClient<IMTargetService, MTargetService>((sp, cl) =>
{
});

builder.Services
.AddTransient<ISmsService, MTargetSmsService>((provider) => new MTargetSmsService(id, provider.GetRequiredService<DatabaseSettings>(), provider.GetRequiredService<IMTargetService>()));

return builder;
return new MTargetSinkBuilder(builder.Services)
.AddMTargetService(id);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<Version>2.0</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.ServiceModel.Duplex" Version="4.10.0" />
<PackageReference Include="System.ServiceModel.Http" Version="4.10.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.10.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\fbognini.Notifications\fbognini.Notifications.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="fbognini.Sdk" Version="2.2.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 65124ea

Please sign in to comment.