diff --git a/InServiceQue.InMemory/InjectionExtension.cs b/InServiceQue.InMemory/InjectionExtension.cs new file mode 100644 index 0000000..5864a62 --- /dev/null +++ b/InServiceQue.InMemory/InjectionExtension.cs @@ -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(); + return services; + } +} \ No newline at end of file diff --git a/InServiceQue.Postgres/InjectionExtension.cs b/InServiceQue.Postgres/InjectionExtension.cs new file mode 100644 index 0000000..e6cce7a --- /dev/null +++ b/InServiceQue.Postgres/InjectionExtension.cs @@ -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(); + return services; + } +} \ No newline at end of file diff --git a/InServiceQue.Sample/DIExtensions.cs b/InServiceQue.Sample/DIExtensions.cs index 71a6bc6..16c1a65 100644 --- a/InServiceQue.Sample/DIExtensions.cs +++ b/InServiceQue.Sample/DIExtensions.cs @@ -7,57 +7,8 @@ using Microsoft.Extensions.DependencyInjection.Extensions; 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(_typeRegistry); - _hostedServiceRegistrator = new HostedServiceRegistrator(); - } - - public void AddQueue() - where T : IQueueTask - where THandler : IQueueHandler - { - _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 void AddInServiceQue(this IServiceCollection services, Action buildAction) - - { - var builder = new InServiceQueBuilder(services); - buildAction(builder); - } - - public static IServiceCollection RegisterInternals(this IServiceCollection services) - { - services.AddTransient(); - return services; - } - public static IServiceCollection RegisterQueues(this IServiceCollection services) { using var sp = services.BuildServiceProvider(); diff --git a/InServiceQue.Sample/OtherMessageHandler.cs b/InServiceQue.Sample/OtherMessageHandler.cs new file mode 100644 index 0000000..0cb4487 --- /dev/null +++ b/InServiceQue.Sample/OtherMessageHandler.cs @@ -0,0 +1,17 @@ +using InServiceQue.Services; + +namespace InServiceQue.Sample; + +public class OtherMessageHandler: IQueueHandler +{ + public bool Handle(string payload) + { + throw new NotImplementedException(); + } + + public async Task HandleAsync(string payload) + { + Console.WriteLine($"Pizda {payload}"); + return await Task.FromResult(true); + } +} \ No newline at end of file diff --git a/InServiceQue.Sample/OtherMessageTask.cs b/InServiceQue.Sample/OtherMessageTask.cs new file mode 100644 index 0000000..d33b29b --- /dev/null +++ b/InServiceQue.Sample/OtherMessageTask.cs @@ -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; + } +} \ No newline at end of file diff --git a/InServiceQue.Sample/Program.cs b/InServiceQue.Sample/Program.cs index 269609a..a56ca6a 100644 --- a/InServiceQue.Sample/Program.cs +++ b/InServiceQue.Sample/Program.cs @@ -1,15 +1,27 @@ +using InServiceQue; using InServiceQue.Core.Models; using InServiceQue.Core.Repositories; +using InServiceQue.InMemory; using InServiceQue.Sample; +using Microsoft.AspNetCore.Mvc; var builder = WebApplication.CreateBuilder(args); -builder.Services.RegisterInternals(); builder.Services.AddInServiceQue(builder => { builder.AddQueue(); -}); + builder.AddQueue(); +}).UseInMemory(); var app = builder.Build(); -app.MapGet("/", (string msg) => app.Services.GetService().Insert(new QueueTask(new SendMessageTask(new SendMessagePayload(){To = "John", From = "Garry", Message = msg})))); +app.MapGet("/", async (string msg) => +{ + var taskRepository = app.Services.GetService(); + 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(); \ No newline at end of file diff --git a/InServiceQue.Sample/HostedServiceRegistrator.cs b/InServiceQue/HostedServiceRegistrator.cs similarity index 85% rename from InServiceQue.Sample/HostedServiceRegistrator.cs rename to InServiceQue/HostedServiceRegistrator.cs index 0fac615..15af473 100644 --- a/InServiceQue.Sample/HostedServiceRegistrator.cs +++ b/InServiceQue/HostedServiceRegistrator.cs @@ -1,6 +1,7 @@ -using System.Reflection; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; -namespace InServiceQue.Sample; +namespace InServiceQue; public class HostedServiceRegistrator { diff --git a/InServiceQue/InServiceQueBuilder.cs b/InServiceQue/InServiceQueBuilder.cs new file mode 100644 index 0000000..24134a0 --- /dev/null +++ b/InServiceQue/InServiceQueBuilder.cs @@ -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(_typeRegistry); + _hostedServiceRegistrator = new HostedServiceRegistrator(); + } + + public void AddQueue() + where T : IQueueTask + where THandler : IQueueHandler + { + _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); + } +} \ No newline at end of file diff --git a/InServiceQue/InjectionExtensions.cs b/InServiceQue/InjectionExtensions.cs new file mode 100644 index 0000000..7faf548 --- /dev/null +++ b/InServiceQue/InjectionExtensions.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace InServiceQue; + +public static class InjectionExtensions +{ + public static IServiceCollection AddInServiceQue(this IServiceCollection services, Action buildAction) + + { + var builder = new InServiceQueBuilder(services); + buildAction(builder); + + return services; + } +} \ No newline at end of file