Skip to content

Commit

Permalink
refactor: use DI container instead of service locator
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis committed Feb 26, 2025
1 parent 4f22630 commit d9c4c7d
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 223 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Backbone.AdminCli.Commands.BaseClasses;
using System.CommandLine;

namespace Backbone.AdminCli.Commands.Announcements;

public class AnnouncementCommand : AdminCliCommand
public class AnnouncementCommand : Command
{
public AnnouncementCommand(ServiceLocator serviceLocator) : base("announcement", serviceLocator)
public AnnouncementCommand(SendAnnouncementCommand sendAnnouncementCommand) : base("announcement")
{
AddCommand(new SendAnnouncementCommand(serviceLocator));
AddCommand(sendAnnouncementCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Backbone.AdminCli.Commands.Announcements;

public class SendAnnouncementCommand : AdminCliDbCommand
public class SendAnnouncementCommand : AdminCliCommand
{
public SendAnnouncementCommand(ServiceLocator serviceLocator) : base("send", serviceLocator)
public SendAnnouncementCommand(IMediator mediator) : base(mediator, "send")
{
var expiresAt = new Option<string?>("--expiration")
{
Expand All @@ -26,10 +26,10 @@ public SendAnnouncementCommand(ServiceLocator serviceLocator) : base("send", ser
AddOption(expiresAt);
AddOption(severity);

this.SetHandler(SendAnnouncement, DB_PROVIDER_OPTION, DB_CONNECTION_STRING_OPTION, severity, expiresAt);
this.SetHandler(SendAnnouncement, severity, expiresAt);
}

private async Task SendAnnouncement(string dbProvider, string dbConnectionString, string? severityInput, string? expiresAtInput)
private async Task SendAnnouncement(string? severityInput, string? expiresAtInput)
{
try
{
Expand Down Expand Up @@ -60,17 +60,23 @@ _ when DateTime.TryParse(expiresAtInput, out var parsedDateTime) => parsedDateTi

Console.WriteLine(@"Sending announcement...");

var mediator = _serviceLocator.GetService<IMediator>(dbProvider, dbConnectionString);

var response = await mediator.Send(new CreateAnnouncementCommand
try
{
Texts = texts,
Severity = severity,
ExpiresAt = expiresAt
}, CancellationToken.None);

Console.WriteLine(@"Announcement sent successfully");
Console.WriteLine(JsonSerializer.Serialize(response, JSON_SERIALIZER_OPTIONS));
var response = await _mediator.Send(new CreateAnnouncementCommand
{
Texts = texts,
Severity = severity,
ExpiresAt = expiresAt
}, CancellationToken.None);

Console.WriteLine(@"Announcement sent successfully");
Console.WriteLine(JsonSerializer.Serialize(response, JSON_SERIALIZER_OPTIONS));
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System.CommandLine;
using System.Text.Json;
using MediatR;

namespace Backbone.AdminCli.Commands.BaseClasses;

public abstract class AdminCliCommand : Command
{
protected readonly IMediator _mediator;

protected static readonly JsonSerializerOptions JSON_SERIALIZER_OPTIONS =
new() { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

protected readonly ServiceLocator _serviceLocator;

protected AdminCliCommand(string name, ServiceLocator serviceLocator, string? description = null) : base(name, description)
protected AdminCliCommand(IMediator mediator, string name, string? description = null) : base(name, description)
{
_serviceLocator = serviceLocator;
_mediator = mediator;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Backbone.AdminCli.Commands.BaseClasses;
using System.CommandLine;

namespace Backbone.AdminCli.Commands.Clients;

public class ClientCommand : AdminCliCommand
public class ClientCommand : Command
{
public ClientCommand(ServiceLocator serviceLocator) : base("client", serviceLocator)
public ClientCommand(CreateClientCommand createClientCommand, ListClientsCommand listClientsCommand, DeleteClientsCommand deleteClientsCommand) : base("client")
{
AddCommand(new CreateClientCommand(serviceLocator));
AddCommand(new ListClientsCommand(serviceLocator));
AddCommand(new DeleteClientsCommand(serviceLocator));
AddCommand(createClientCommand);
AddCommand(listClientsCommand);
AddCommand(deleteClientsCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace Backbone.AdminCli.Commands.Clients;

public class CreateClientCommand : AdminCliDbCommand
public class CreateClientCommand : AdminCliCommand
{
public CreateClientCommand(ServiceLocator serviceLocator) : base("create", serviceLocator, "Create an OAuth client")
public CreateClientCommand(IMediator mediator) : base(mediator, "create", "Create an OAuth client")
{
var clientId = new Option<string>("--clientId")
{
Expand Down Expand Up @@ -44,16 +44,13 @@ public CreateClientCommand(ServiceLocator serviceLocator) : base("create", servi
AddOption(defaultTierId);
AddOption(maxIdentities);

this.SetHandler(CreateClient, DB_PROVIDER_OPTION, DB_CONNECTION_STRING_OPTION, clientId, displayName, clientSecret,
defaultTierId, maxIdentities);
this.SetHandler(CreateClient, clientId, displayName, clientSecret, defaultTierId, maxIdentities);
}

private async Task CreateClient(string dbProvider, string dbConnectionString, string? clientId,
private async Task CreateClient(string? clientId,
string? displayName, string? clientSecret, string defaultTier, int? maxIdentities)
{
var mediator = _serviceLocator.GetService<IMediator>(dbProvider, dbConnectionString);

var response = await mediator.Send(new Modules.Devices.Application.Clients.Commands.CreateClient.CreateClientCommand(clientId, displayName, clientSecret, defaultTier, maxIdentities),
var response = await _mediator.Send(new Modules.Devices.Application.Clients.Commands.CreateClient.CreateClientCommand(clientId, displayName, clientSecret, defaultTier, maxIdentities),
CancellationToken.None);

Console.WriteLine(JsonSerializer.Serialize(response, JSON_SERIALIZER_OPTIONS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace Backbone.AdminCli.Commands.Clients;

public class DeleteClientsCommand : AdminCliDbCommand
public class DeleteClientsCommand : AdminCliCommand
{
public DeleteClientsCommand(ServiceLocator serviceLocator) : base("delete", serviceLocator, "Deletes the OAuth clients with the given clientId's")
public DeleteClientsCommand(IMediator mediator) : base(mediator, "delete", "Deletes the OAuth clients with the given clientId's")
{
var clientIds = new Argument<string[]>("clientIds")
{
Expand All @@ -17,18 +17,16 @@ public DeleteClientsCommand(ServiceLocator serviceLocator) : base("delete", serv

AddArgument(clientIds);

this.SetHandler(DeleteClients, DB_PROVIDER_OPTION, DB_CONNECTION_STRING_OPTION, clientIds);
this.SetHandler(DeleteClients, clientIds);
}

private async Task DeleteClients(string dbProvider, string dbConnectionString, string[] clientIds)
private async Task DeleteClients(string[] clientIds)
{
var mediator = _serviceLocator.GetService<IMediator>(dbProvider, dbConnectionString);

foreach (var clientId in clientIds)
{
try
{
await mediator.Send(new DeleteClientCommand(clientId), CancellationToken.None);
await _mediator.Send(new DeleteClientCommand(clientId), CancellationToken.None);
Console.WriteLine($@"Successfully deleted client '{clientId}'");
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@

namespace Backbone.AdminCli.Commands.Clients;

public class ListClientsCommand : AdminCliDbCommand
public class ListClientsCommand : AdminCliCommand
{
public ListClientsCommand(ServiceLocator serviceLocator) : base("list", serviceLocator, "List all existing OAuth clients")
public ListClientsCommand(IMediator mediator) : base(mediator, "list", "List all existing OAuth clients")
{
this.SetHandler(ListClients, DB_PROVIDER_OPTION, DB_CONNECTION_STRING_OPTION);
this.SetHandler(ListClients);
}

private async Task ListClients(string dbProvider, string dbConnectionString)
private async Task ListClients()
{
var mediator = _serviceLocator.GetService<IMediator>(dbProvider, dbConnectionString);

var response = await mediator.Send(new ListClientsQuery(), CancellationToken.None);
var response = await _mediator.Send(new ListClientsQuery(), CancellationToken.None);

Console.WriteLine(@"The following clients are configured:");

Expand Down
8 changes: 4 additions & 4 deletions Applications/AdminCli/src/AdminCli/Commands/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace Backbone.AdminCli.Commands;

public class RootCommand : System.CommandLine.RootCommand
{
public RootCommand(ServiceLocator serviceLocator)
public RootCommand(ClientCommand clientCommand, TierCommand tierCommand, AnnouncementCommand announcementCommand)
{
AddCommand(new ClientCommand(serviceLocator));
AddCommand(new TierCommand(serviceLocator));
AddCommand(new AnnouncementCommand(serviceLocator));
AddCommand(clientCommand);
AddCommand(tierCommand);
AddCommand(announcementCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@

namespace Backbone.AdminCli.Commands.Tiers;

public class ListTiersCommand : AdminCliDbCommand
public class ListTiersCommand : AdminCliCommand
{
public ListTiersCommand(ServiceLocator serviceLocator) : base("list", serviceLocator, "List all existing Tiers")
public ListTiersCommand(IMediator mediator) : base(mediator, "list", "List all existing Tiers")
{
this.SetHandler(ListTiers, DB_PROVIDER_OPTION, DB_CONNECTION_STRING_OPTION);
this.SetHandler(ListTiers);
}

private async Task ListTiers(string dbProvider, string dbConnectionString)
private async Task ListTiers()
{
var mediator = _serviceLocator.GetService<IMediator>(dbProvider, dbConnectionString);

var response = await mediator.Send(new ListTiersQuery(new PaginationFilter()), CancellationToken.None);
var response = await _mediator.Send(new ListTiersQuery(new PaginationFilter()), CancellationToken.None);

Console.WriteLine(@"The following tiers are configured:");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Backbone.AdminCli.Commands.BaseClasses;
using System.CommandLine;

namespace Backbone.AdminCli.Commands.Tiers;

public class TierCommand : AdminCliCommand
public class TierCommand : Command
{
public TierCommand(ServiceLocator serviceLocator) : base("tier", serviceLocator)
public TierCommand(ListTiersCommand listTiersCommand) : base("tier")
{
AddCommand(new ListTiersCommand(serviceLocator));
AddCommand(listTiersCommand);
}
}
Loading

0 comments on commit d9c4c7d

Please sign in to comment.