Skip to content

Commit

Permalink
Refactor module behaviour out and into a BaseModule.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterKneale committed Apr 2, 2024
1 parent 2e0bfac commit 70b658c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 90 deletions.
34 changes: 34 additions & 0 deletions src/Micro.Common/BaseModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.Extensions.DependencyInjection;

namespace Micro.Users;

public class BaseModule
{
private readonly Func<IServiceScope> _scopeFactory;

protected BaseModule(Func<IServiceScope> scopeFactory)
{
_scopeFactory = scopeFactory;
}

public async Task SendCommand(IRequest command)
{
using var scope = _scopeFactory.Invoke();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Send(command);
}

public async Task<TResult> SendQuery<TResult>(IRequest<TResult> query)
{
using var scope = _scopeFactory.Invoke();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
return await dispatcher.Send(query);
}

public async Task PublishNotification(INotification notification)
{
using var scope = _scopeFactory.Invoke();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Publish(notification);
}
}
7 changes: 0 additions & 7 deletions src/Micro.Tenants/ITenantsModule.cs

This file was deleted.

28 changes: 5 additions & 23 deletions src/Micro.Tenants/TenantsModule.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
using Micro.Tenants.Infrastructure;
using Micro.Common;
using Micro.Tenants.Infrastructure;
using Micro.Users;

namespace Micro.Tenants;

public class TenantsModule : ITenantsModule
{
public async Task SendCommand(IRequest command)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Send(command);
}
public interface ITenantsModule : IModule;

public async Task<TResult> SendQuery<TResult>(IRequest<TResult> query)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
return await dispatcher.Send(query);
}

public async Task PublishNotification(INotification notification)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Publish(notification);
}
}
public class TenantsModule() : BaseModule(CompositionRoot.BeginLifetimeScope), ITenantsModule;
7 changes: 0 additions & 7 deletions src/Micro.Translations/ITranslationModule.cs

This file was deleted.

28 changes: 5 additions & 23 deletions src/Micro.Translations/TranslationModule.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
using Micro.Translations.Infrastructure;
using Micro.Common;
using Micro.Translations.Infrastructure;
using Micro.Users;

namespace Micro.Translations;

public class TranslationModule : ITranslationModule
{
public async Task SendCommand(IRequest command)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Send(command);
}
public interface ITranslationModule : IModule;

public async Task<TResult> SendQuery<TResult>(IRequest<TResult> query)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
return await dispatcher.Send(query);
}

public async Task PublishNotification(INotification notification)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Publish(notification);
}
}
public class TranslationModule() : BaseModule(CompositionRoot.BeginLifetimeScope), ITranslationModule;
7 changes: 0 additions & 7 deletions src/Micro.Users/IUsersModule.cs

This file was deleted.

27 changes: 4 additions & 23 deletions src/Micro.Users/UsersModule.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
using Micro.Users.Infrastructure;
using Micro.Common;
using Micro.Users.Infrastructure;

namespace Micro.Users;

public class UsersModule : IUsersModule
{
public async Task SendCommand(IRequest command)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Send(command);
}
public interface IUsersModule : IModule;

public async Task<TResult> SendQuery<TResult>(IRequest<TResult> query)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
return await dispatcher.Send(query);
}

public async Task PublishNotification(INotification notification)
{
using var scope = CompositionRoot.BeginLifetimeScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IMediator>();
await dispatcher.Publish(notification);
}
}
public class UsersModule() : BaseModule(CompositionRoot.BeginLifetimeScope), IUsersModule;

0 comments on commit 70b658c

Please sign in to comment.