Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DataArc.EntityFrameworkCore.Demo\DataArc.EntityFrameworkCore.Demo.csproj" />
<ProjectReference Include="..\DataArc.EntityFrameworkCore.Demo.Persistence\DataArc.EntityFrameworkCore.Demo.Persistence.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Microsoft.Extensions.Hosting;

using DataArc.EntityFrameworkCore.Demo.Application.Modules.Finance.Features.SalaryAdjustments.Services;

namespace DataArc.EntityFrameworkCore.Demo.Application.Workers
{
internal sealed class DemoWorkflowWorker : BackgroundService
{
private const int BatchSize = 100_000;

private readonly ISalaryAdjustmentService _salaryAdjustmentService;
private readonly IEmployeePerformanceService _employeePerformanceService;
private readonly IHostApplicationLifetime _hostApplicationLifetime;

public DemoWorkflowWorker(
ISalaryAdjustmentService salaryAdjustmentService,
IEmployeePerformanceService employeePerformanceService,
IHostApplicationLifetime hostApplicationLifetime)
{
_salaryAdjustmentService = salaryAdjustmentService;
_employeePerformanceService = employeePerformanceService;
_hostApplicationLifetime = hostApplicationLifetime;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
decimal salaryAdjustmentBaseRate = 0.05m;
decimal salaryThreshold = 10_000m;
double ratingThreshold = 4.5;

var processedCount = await _salaryAdjustmentService
.ProcessEmployeeSalaryAdjustmentsAsync(
salaryAdjustmentBaseRate,
salaryThreshold,
BatchSize);

Console.WriteLine($"Processed salary adjustment records: {processedCount:N0}");

if (processedCount > 0)
{
var topRatedEmployees = await _employeePerformanceService
.GetTopRatedEmployeesAsync(ratingThreshold);

var topRatedEmployee = topRatedEmployees
.OrderByDescending(employee => employee.Salary)
.FirstOrDefault();

Console.WriteLine();

Console.WriteLine(topRatedEmployee is null
? "No top rated employees found."
: $"Top Rated Employee: {topRatedEmployee.Name} {topRatedEmployee.Surname}, " +
$"Salary: {topRatedEmployee.Salary:N2}, " +
$"Number of top rated employees: {topRatedEmployees.Count:N0}");
}

Console.WriteLine();
Console.WriteLine("Demo workflow completed.");
}
catch (Exception exception)
{
Console.WriteLine();
Console.WriteLine($"Demo workflow failed: {exception.Message}");
throw;
}
finally
{
_hostApplicationLifetime.StopApplication();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@
</None>
</ItemGroup>

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

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

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DataArc.EntityFrameworkCore.Demo.Persistence\DataArc.EntityFrameworkCore.Demo.Persistence.csproj" />
</ItemGroup>
Expand Down
62 changes: 20 additions & 42 deletions DataArc.EntityFrameworkCore.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,42 @@
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using DataArc.EntityFrameworkCore.Demo.Application.Modules.Finance.Features.SalaryAdjustments.Services;
using DataArc.EntityFrameworkCore.Demo.Application.Modules.Finance.Registration;
using DataArc.EntityFrameworkCore.Demo.Application.Workers;
using DataArc.EntityFrameworkCore.Demo.Persistence.Database.Creator;
using DataArc.EntityFrameworkCore.Demo.Persistence.Database.Seeder;

const int batchSize = 100_000;

var serviceProvider = new ServiceCollection()
.AddFinanceModule()
.BuildServiceProvider();
var host = Host
.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services
.AddFinanceModule()
.AddHostedService<DemoWorkflowWorker>();
})
.Build();

using var scope = host.Services.CreateScope();

var databaseCreators = new IDatabaseCreator[]
{
serviceProvider.GetRequiredService<IFinanceDbCreator>(),
serviceProvider.GetRequiredService<IHrDbCreator>(),
serviceProvider.GetRequiredService<IItDbCreator>(),
serviceProvider.GetRequiredService<IOperationsDbCreator>()
scope.ServiceProvider.GetRequiredService<IFinanceDbCreator>(),
scope.ServiceProvider.GetRequiredService<IHrDbCreator>(),
scope.ServiceProvider.GetRequiredService<IItDbCreator>(),
scope.ServiceProvider.GetRequiredService<IOperationsDbCreator>()
};

var databaseSeeders = new IDatabaseSeeder[]
{
serviceProvider.GetRequiredService<IHrDbSeeder>(),
scope.ServiceProvider.GetRequiredService<IHrDbSeeder>()
};

var salaryAdjustmentService = serviceProvider.GetRequiredService<ISalaryAdjustmentService>();
var employeePerformanceService = serviceProvider.GetRequiredService<IEmployeePerformanceService>();

await ResetDatabasesAsync(databaseCreators, databaseSeeders, batchSize);

decimal salaryAdjustmentBaseRate = 0.05m;
decimal salaryThreshold = 10_000m;
double ratingThreshold = 4.5;

var processedCount = await salaryAdjustmentService
.ProcessEmployeeSalaryAdjustmentsAsync(
salaryAdjustmentBaseRate,
salaryThreshold,
batchSize);

Console.WriteLine($"Processed salary adjustment records: {processedCount:N0}");

if (processedCount > 0)
{
var topRatedEmployees = await employeePerformanceService
.GetTopRatedEmployeesAsync(ratingThreshold);

var topRatedEmployee = topRatedEmployees
.OrderByDescending(employee => employee.Salary)
.FirstOrDefault();

Console.WriteLine();

Console.WriteLine(topRatedEmployee is null
? "No top rated employees found."
: $"Top Rated Employee: {topRatedEmployee.Name} {topRatedEmployee.Surname}, " +
$"Salary: {topRatedEmployee.Salary:N2}, " +
$"Number of top rated employees: {topRatedEmployees.Count:N0}");
}
await host.RunAsync();

Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Expand Down Expand Up @@ -87,7 +65,7 @@ static async Task ResetDatabasesAsync(

foreach (var databaseSeeder in databaseSeeders)
{
if(!await databaseSeeder.SeedDatabaseAsync(batchSize))
if (!await databaseSeeder.SeedDatabaseAsync(batchSize))
throw new InvalidOperationException("Failed to seed the demo databases.");
}

Expand Down
Loading
Loading