Skip to content

Commit

Permalink
Add publishing code to template API where new subscriber option is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
chullybun committed Feb 21, 2024
1 parent 06df96f commit fd142fd
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
</Content>
</ItemGroup>

<ItemGroup>
<None Remove="content\Company.AppName.Test\appsettings.unittest.json" />
</ItemGroup>

<Import Project="..\..\Common.targets" />

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@
"defaultValue": "SqlServer",
"description": "The data source implementation option."
},
"subscriber": {
"type": "parameter",
"datatype": "choice",
"choices": [
{
"choice": "AzFunction",
"description": "Indicates that the Service Bus subscriber is to be implemented using an Azure Function."
},
{
"choice": "None",
"description": "Indicates that no Service Bus subscriber is to be implemented (default)."
}
],
"defaultValue": "None",
"description": "The Service Bus subscriber implementation option."
},
"coreex_version": {
"type": "generated",
"generator": "constant",
Expand Down Expand Up @@ -113,6 +129,14 @@
"type": "computed",
"value": "(datasource == \"None\")"
},
"implement_subscriber": {
"type": "computed",
"value": "(subscriber == \"AzFunction\")"
},
"implement_subscriber_azfunction": {
"type": "computed",
"value": "(subscriber == \"AzFunction\")"
},
"created_date": {
"type": "generated",
"generator": "now",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CoreEx.AspNetCore" Version="CoreExVersion" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.1.0" />
<PackageReference Include="CoreEx.AspNetCore" Version="CoreExVersion" />
<!--#if (implement_subscriber) -->
<PackageReference Include="CoreEx.Azure" Version="CoreExVersion" />
<!--#endif -->
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Company.AppName.Business\Company.AppName.Business.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
global using Azure.Monitor.OpenTelemetry.AspNetCore;
global using CoreEx;
global using CoreEx.AspNetCore.WebApis;
#if (implement_subscriber)
#if (implement_database || implement_sqlserver)
global using CoreEx.Azure.ServiceBus;
global using CoreEx.Azure.Storage;
global using CoreEx.Database;
#endif
#endif
global using CoreEx.Entities;
global using CoreEx.Events;
#if (implement_subscriber)
#if (implement_database || implement_sqlserver)
global using CoreEx.Hosting;
#endif
#endif
global using CoreEx.Http;
global using CoreEx.RefData;
global using CoreEx.Validation;
Expand All @@ -29,4 +41,10 @@
global using RefDataNamespace = Company.AppName.Business.Entities;
#if (implement_cosmos)
global using AzCosmos = Microsoft.Azure.Cosmos;
#endif
#if (implement_subscriber)
global using AzServiceBus = Azure.Messaging.ServiceBus;
#if (implement_database || implement_sqlserver)
global using AzBlobs = Azure.Storage.Blobs;
#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,37 @@ public void ConfigureServices(IServiceCollection services)
.AddGeneratedDataSvcServices()
.AddGeneratedDataServices();

#if (implement_subscriber)
// Add event publishing services.
services.AddEventDataFormatter()
.AddCloudEventSerializer()
.AddEventPublisher();

#if (implement_database || implement_sqlserver)
// Add sql server transactional event outbox enqueue services.
services.AddScoped<IEventSender, EventOutboxEnqueue>();

// Add transactional event outbox dequeue dependencies.
services.AddSingleton(sp => new AzServiceBus.ServiceBusClient(sp.GetRequiredService<AppNameSettings>().ServiceBusConnectionString))
.AddSingleton<IServiceSynchronizer>(sp => new BlobLeaseSynchronizer(new AzBlobs.BlobContainerClient(sp.GetRequiredService<AppNameSettings>().StorageConnectionString, "event-synchronizer")));

// Add transactional event outbox dequeue hosted service (_must_ be explicit with the IServiceBusSender).
services.AddScoped<IServiceBusSender, ServiceBusSender>()
.AddSqlServerEventOutboxHostedService(sp =>
{
return new EventOutboxDequeue(sp.GetRequiredService<IDatabase>(), sp.GetRequiredService<IServiceBusSender>(), sp.GetRequiredService<ILogger<EventOutboxDequeue>>());
});
#else
// Add service bus event sender.
services.AddSingleton(sp => new AzServiceBus.ServiceBusClient(sp.GetRequiredService<AppNameSettings>().ServiceBusConnectionString))
.AddScoped<IEventSender, ServiceBusSender>();
#endif
#else
// Add the event publishing; this will need to be updated from the null publisher to the actual as appropriate.
services.AddEventDataFormatter()
.AddCloudEventSerializer()
.AddNullEventPublisher();
#endif

// Add additional services.
services.AddControllers();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@
//#if (implement_database || implement_sqlserver)
// Set using environment variable: 'AppName_ConnectionStrings__Database'
"ConnectionStrings": {
//#if (implement_subscriber)
"ServiceBus": "Endpoint=sb://top-secret.servicebus.windows.net/;SharedAccessKeyName=top-secret;SharedAccessKey=top-encrypted-secret;",
"Storage": "DefaultEndpointsProtocol=https;AccountName=top-secret;AccountKey=top-secret;EndpointSuffix=core.windows.net",
//#endif
"Database": "Data Source=.;Initial Catalog=Company.AppName;Integrated Security=True;TrustServerCertificate=true"
},
//#endif
//#if (implement_mysql)
// Set using environment variable: 'AppName_ConnectionStrings__Database'
"ConnectionStrings": {
//#if (implement_subscriber)
"ServiceBus": "Endpoint=sb://top-secret.servicebus.windows.net/;SharedAccessKeyName=top-secret;SharedAccessKey=top-encrypted-secret;",
//#endif
"Database": "Server=localhost; Port=3306; Database=Company.AppName; Uid=dbuser; Pwd=dbpassword; AllowUserVariables=true; UseAffectedRows=false"
},
//#endif
//#if (implement_postgres)
// Set using environment variable: 'AppName_ConnectionStrings__Database'
"ConnectionStrings": {
//#if (implement_subscriber)
"ServiceBus": "Endpoint=sb://top-secret.servicebus.windows.net/;SharedAccessKeyName=top-secret;SharedAccessKey=top-encrypted-secret;",
//#endif
"Database": "Server=localhost; Database=Company.AppName; Username=postgres; Password=dbpassword;"
},
//#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ public class AppNameSettings : SettingsBase
/// </summary>
public string XxxAgentUrl => GetRequiredValue<string>();
#endif
#if (implement_subscriber)

/// <summary>
/// Gets the Azure Service Bus connection string.
/// </summary>
public string ServiceBusConnectionString => GetRequiredValue<string>("ConnectionStrings__ServiceBus");
#if (implement_database || implement_sqlserver)

/// <summary>
/// Gets the Azure Blob Storage connection string.
/// </summary>
public string StorageConnectionString => GetRequiredValue<string>("ConnectionStrings__Storage");
#endif
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public static Task<int> Main(string[] args) => PostgresMigrationConsole
/// </summary>
/// <param name="args">The <see cref="MigrationArgs"/>.</param>
/// <returns>The <see cref="MigrationArgs"/>.</returns>
/// <remarks>This is also invoked from the tests to ensure consistency of execution.</remarks>
#if (implement_database || implement_sqlserver)
/// <remarks>This is also invoked from the unit tests to ensure consistency of execution.</remarks>
#if (implement_database)
public static MigrationArgs ConfigureMigrationArgs(MigrationArgs args) => args.AddAssembly<Program>().UseBeefSchema();
#endif
#if (implement_mysql || implement_postgres)
#if (implement_sqlserver || implement_mysql || implement_postgres)
public static MigrationArgs ConfigureMigrationArgs(MigrationArgs args) => args.AddAssembly<Program>();
#endif
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//#if (implement_database)
schema: AppName
//#if (implement_subscriber)
outbox: true
//#endif
tables:
# Reference data stored procedures
- { name: Gender, getAll: true }
Expand All @@ -23,6 +26,9 @@ tables:
//#endif
//#if (implement_sqlserver)
schema: AppName
//#if (implement_subscriber)
outbox: true
//#endif
efModel: true
tables:
# Reference data tables/models.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<Content Include="appsettings.unittest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="CoreEx.UnitTesting.NUnit" Version="CoreExVersion" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
//#if (implement_subscriber)
//#if (implement_database || implement_sqlserver)
"EventOutboxHostedService": {
"Enabled": false
}
//#endif
//#endif
}
11 changes: 9 additions & 2 deletions tests/Beef.Template.Solution.UnitTest/TemplateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ public void SqlServer()
SolutionCreateGenerateTest("Foo.Ef", "Bar", "SqlServer");
}

[Test]
public void SqlServer_WithSubscriber()
{
OneTimeSetUp();
SolutionCreateGenerateTest("Foo.EfWs", "Bar", "SqlServer", "AzFunction");
}

[Test]
public void MySQL()
{
Expand Down Expand Up @@ -147,12 +154,12 @@ public void HttpAgent()
SolutionCreateGenerateTest("Foo.Ha", "Bar", "HttpAgent");
}

private static void SolutionCreateGenerateTest(string company, string appName, string datasource)
private static void SolutionCreateGenerateTest(string company, string appName, string datasource, string subscriber = null)
{
// Mkdir and create solution from template.
var dir = Path.Combine(_unitTests.FullName, $"{company}.{appName}");
Directory.CreateDirectory(dir);
Assert.That(ExecuteCommand("dotnet", $"new beef --company {company} --appname {appName} --datasource {datasource}", dir).exitCode, Is.Zero, "dotnet new beef");
Assert.That(ExecuteCommand("dotnet", $"new beef --company {company} --appname {appName} --datasource {datasource} {(string.IsNullOrEmpty(subscriber) ? "" : $"--subscriber {subscriber}")}", dir).exitCode, Is.Zero, "dotnet new beef");

// Restore nuget packages from our repository.
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "nuget-publish");
Expand Down

0 comments on commit fd142fd

Please sign in to comment.