fix: files in their projects
This commit is contained in:
13
InServiceQue.InMemory/InjectionExtension.cs
Normal file
13
InServiceQue.InMemory/InjectionExtension.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using InServiceQue.Core.Repositories;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace InServiceQue.InMemory;
|
||||||
|
|
||||||
|
public static class InjectionExtension
|
||||||
|
{
|
||||||
|
public static IServiceCollection UseInMemory(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddTransient<ITaskRepository, TaskRepositoryInMemory>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
InServiceQue.Postgres/InjectionExtension.cs
Normal file
13
InServiceQue.Postgres/InjectionExtension.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using InServiceQue.Core.Repositories;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace InServiceQue.Postgres;
|
||||||
|
|
||||||
|
public static class InjectionExtension
|
||||||
|
{
|
||||||
|
public static IServiceCollection UsePostgres(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddTransient<ITaskRepository, TaskRepository>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,57 +7,8 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
|
|||||||
|
|
||||||
namespace InServiceQue.Sample;
|
namespace InServiceQue.Sample;
|
||||||
|
|
||||||
public class InServiceQueBuilder
|
|
||||||
{
|
|
||||||
private IServiceCollection _services;
|
|
||||||
private ITypeRegistry _typeRegistry;
|
|
||||||
private HostedServiceRegistrator _hostedServiceRegistrator;
|
|
||||||
public InServiceQueBuilder(IServiceCollection services)
|
|
||||||
{
|
|
||||||
_services = services;
|
|
||||||
StartBuilding();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void StartBuilding()
|
|
||||||
{
|
|
||||||
_typeRegistry = new QueueTypeRegistry();
|
|
||||||
_services.AddSingleton<ITypeRegistry>(_typeRegistry);
|
|
||||||
_hostedServiceRegistrator = new HostedServiceRegistrator();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddQueue<T, THandler>()
|
|
||||||
where T : IQueueTask
|
|
||||||
where THandler : IQueueHandler<T>
|
|
||||||
{
|
|
||||||
_typeRegistry.RegisterTaskType(typeof(T).Name, typeof(THandler).GetInterfaces()
|
|
||||||
.First(x => x.IsGenericType
|
|
||||||
&& x.GetGenericTypeDefinition() == typeof(IQueueHandler<>)));
|
|
||||||
|
|
||||||
var taskType = typeof(T);
|
|
||||||
var queueHandlerType = typeof(THandler);
|
|
||||||
//can't resolve then
|
|
||||||
_services.AddScoped(typeof(IQueueHandler<>).MakeGenericType(taskType), queueHandlerType);
|
|
||||||
|
|
||||||
var hostedServiceType = typeof(QueueService<>).MakeGenericType(taskType);
|
|
||||||
_hostedServiceRegistrator.RegisterHostedService(_services, hostedServiceType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class DIExtensions
|
public static class DIExtensions
|
||||||
{
|
{
|
||||||
public static void AddInServiceQue(this IServiceCollection services, Action<InServiceQueBuilder> buildAction)
|
|
||||||
|
|
||||||
{
|
|
||||||
var builder = new InServiceQueBuilder(services);
|
|
||||||
buildAction(builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection RegisterInternals(this IServiceCollection services)
|
|
||||||
{
|
|
||||||
services.AddTransient<ITaskRepository, TaskRepositoryInMemory>();
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection RegisterQueues(this IServiceCollection services)
|
public static IServiceCollection RegisterQueues(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
using var sp = services.BuildServiceProvider();
|
using var sp = services.BuildServiceProvider();
|
||||||
|
|||||||
17
InServiceQue.Sample/OtherMessageHandler.cs
Normal file
17
InServiceQue.Sample/OtherMessageHandler.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using InServiceQue.Services;
|
||||||
|
|
||||||
|
namespace InServiceQue.Sample;
|
||||||
|
|
||||||
|
public class OtherMessageHandler: IQueueHandler<OtherMessageTask>
|
||||||
|
{
|
||||||
|
public bool Handle(string payload)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> HandleAsync(string payload)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Pizda {payload}");
|
||||||
|
return await Task.FromResult<bool>(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
22
InServiceQue.Sample/OtherMessageTask.cs
Normal file
22
InServiceQue.Sample/OtherMessageTask.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using InServiceQue.Core.Models;
|
||||||
|
|
||||||
|
namespace InServiceQue.Sample;
|
||||||
|
|
||||||
|
public class OtherMessageTask: IQueueTask
|
||||||
|
{
|
||||||
|
private string _payload;
|
||||||
|
|
||||||
|
public OtherMessageTask(string payload)
|
||||||
|
{
|
||||||
|
_payload = payload;
|
||||||
|
}
|
||||||
|
public string GetTypeString()
|
||||||
|
{
|
||||||
|
return nameof(OtherMessageTask);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetPayloadString()
|
||||||
|
{
|
||||||
|
return _payload;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,27 @@
|
|||||||
|
using InServiceQue;
|
||||||
using InServiceQue.Core.Models;
|
using InServiceQue.Core.Models;
|
||||||
using InServiceQue.Core.Repositories;
|
using InServiceQue.Core.Repositories;
|
||||||
|
using InServiceQue.InMemory;
|
||||||
using InServiceQue.Sample;
|
using InServiceQue.Sample;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Services.RegisterInternals();
|
|
||||||
builder.Services.AddInServiceQue(builder =>
|
builder.Services.AddInServiceQue(builder =>
|
||||||
{
|
{
|
||||||
builder.AddQueue<SendMessageTask, SendMessageHandler>();
|
builder.AddQueue<SendMessageTask, SendMessageHandler>();
|
||||||
});
|
builder.AddQueue<OtherMessageTask, OtherMessageHandler>();
|
||||||
|
}).UseInMemory();
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.MapGet("/", (string msg) => app.Services.GetService<ITaskRepository>().Insert(new QueueTask(new SendMessageTask(new SendMessagePayload(){To = "John", From = "Garry", Message = msg}))));
|
app.MapGet("/", async (string msg) =>
|
||||||
|
{
|
||||||
|
var taskRepository = app.Services.GetService<ITaskRepository>();
|
||||||
|
await taskRepository.InsertAsync(new QueueTask(new SendMessageTask(new SendMessagePayload()
|
||||||
|
{
|
||||||
|
From = "John", To = "Esther", Message = msg
|
||||||
|
})));
|
||||||
|
await taskRepository.InsertAsync(new QueueTask(new OtherMessageTask(msg)));
|
||||||
|
return new OkResult();
|
||||||
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Reflection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace InServiceQue.Sample;
|
namespace InServiceQue;
|
||||||
|
|
||||||
public class HostedServiceRegistrator
|
public class HostedServiceRegistrator
|
||||||
{
|
{
|
||||||
42
InServiceQue/InServiceQueBuilder.cs
Normal file
42
InServiceQue/InServiceQueBuilder.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using InServiceQue.Core.Models;
|
||||||
|
using InServiceQue.Services;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace InServiceQue;
|
||||||
|
|
||||||
|
public class InServiceQueBuilder
|
||||||
|
{
|
||||||
|
private IServiceCollection _services;
|
||||||
|
private ITypeRegistry _typeRegistry;
|
||||||
|
private HostedServiceRegistrator _hostedServiceRegistrator;
|
||||||
|
|
||||||
|
public InServiceQueBuilder(IServiceCollection services)
|
||||||
|
{
|
||||||
|
_services = services;
|
||||||
|
StartBuilding();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StartBuilding()
|
||||||
|
{
|
||||||
|
_typeRegistry = new QueueTypeRegistry();
|
||||||
|
_services.AddSingleton<ITypeRegistry>(_typeRegistry);
|
||||||
|
_hostedServiceRegistrator = new HostedServiceRegistrator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddQueue<T, THandler>()
|
||||||
|
where T : IQueueTask
|
||||||
|
where THandler : IQueueHandler<T>
|
||||||
|
{
|
||||||
|
_typeRegistry.RegisterTaskType(typeof(T).Name, typeof(THandler).GetInterfaces()
|
||||||
|
.First(x => x.IsGenericType
|
||||||
|
&& x.GetGenericTypeDefinition() == typeof(IQueueHandler<>)));
|
||||||
|
|
||||||
|
var taskType = typeof(T);
|
||||||
|
var queueHandlerType = typeof(THandler);
|
||||||
|
//can't resolve then
|
||||||
|
_services.AddScoped(typeof(IQueueHandler<>).MakeGenericType(taskType), queueHandlerType);
|
||||||
|
|
||||||
|
var hostedServiceType = typeof(QueueService<>).MakeGenericType(taskType);
|
||||||
|
_hostedServiceRegistrator.RegisterHostedService(_services, hostedServiceType);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
InServiceQue/InjectionExtensions.cs
Normal file
15
InServiceQue/InjectionExtensions.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace InServiceQue;
|
||||||
|
|
||||||
|
public static class InjectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddInServiceQue(this IServiceCollection services, Action<InServiceQueBuilder> buildAction)
|
||||||
|
|
||||||
|
{
|
||||||
|
var builder = new InServiceQueBuilder(services);
|
||||||
|
buildAction(builder);
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user