From d1e0585c3625afcfb3c72924b24a1c0c8bfe3d2c Mon Sep 17 00:00:00 2001 From: Alexanderius Date: Thu, 14 Jan 2021 22:49:10 +0600 Subject: [PATCH] Templates refactoring --- Simplify.ProjectsTemplates.csproj | 6 ++-- Simplify.ProjectsTemplates.sln.DotSettings | 4 +++ .../SchedulerHandlerObserver.cs | 20 ++++++++++++ .../MyProject.Scheduler.csproj | 2 +- templates/MyProject.Scheduler/Program.cs | 29 +++++++---------- .../Setup/IocRegistrations.cs | 15 +++++---- .../WindowsServiceHandlerObserver.cs | 20 ++++++++++++ .../MyProject.WindowsService.csproj | 3 +- templates/MyProject.WindowsService/Program.cs | 31 ++++++------------- .../Setup/IocRegistrations.cs | 15 +++++---- 10 files changed, 88 insertions(+), 57 deletions(-) create mode 100644 Simplify.ProjectsTemplates.sln.DotSettings create mode 100644 templates/MyProject.Scheduler/Infrastructure/SchedulerHandlerObserver.cs create mode 100644 templates/MyProject.WindowsService/Infrastructure/WindowsServiceHandlerObserver.cs diff --git a/Simplify.ProjectsTemplates.csproj b/Simplify.ProjectsTemplates.csproj index 3da26d0..d1fd686 100644 --- a/Simplify.ProjectsTemplates.csproj +++ b/Simplify.ProjectsTemplates.csproj @@ -3,7 +3,7 @@ netcoreapp3.1 Template - 0.1.3 + 0.2 Simplify.ProjectsTemplates true https://github.com/SimplifyNet/Simplify.ProjectsTemplates @@ -12,9 +12,7 @@ GIT Updates - * Upgrade to Simplify.Scheduler 1.0 - * Upgrade to Microsoft.Extensions.Configuration.Json 3.1.5 - * Switch to .NET Core 3.1 + Templates refactoring Simplify project templates Alexander Krylkov diff --git a/Simplify.ProjectsTemplates.sln.DotSettings b/Simplify.ProjectsTemplates.sln.DotSettings new file mode 100644 index 0000000..678a6d6 --- /dev/null +++ b/Simplify.ProjectsTemplates.sln.DotSettings @@ -0,0 +1,4 @@ + + True + True + True \ No newline at end of file diff --git a/templates/MyProject.Scheduler/Infrastructure/SchedulerHandlerObserver.cs b/templates/MyProject.Scheduler/Infrastructure/SchedulerHandlerObserver.cs new file mode 100644 index 0000000..f3e9c72 --- /dev/null +++ b/templates/MyProject.Scheduler/Infrastructure/SchedulerHandlerObserver.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using Simplify.Scheduler; + +namespace MyProject.Scheduler.Infrastructure +{ + public static class SchedulerHandlerObserver + { + public static MultitaskScheduler SubscribeLog(this MultitaskScheduler handler) + { + handler.OnException += OnException; + + return handler; + } + + private static void OnException(SchedulerExceptionArgs args) + { + Trace.WriteLine(args.Exception.Message); + } + } +} \ No newline at end of file diff --git a/templates/MyProject.Scheduler/MyProject.Scheduler.csproj b/templates/MyProject.Scheduler/MyProject.Scheduler.csproj index e01d095..d3567dd 100644 --- a/templates/MyProject.Scheduler/MyProject.Scheduler.csproj +++ b/templates/MyProject.Scheduler/MyProject.Scheduler.csproj @@ -7,7 +7,7 @@ Exe - + diff --git a/templates/MyProject.Scheduler/Program.cs b/templates/MyProject.Scheduler/Program.cs index d02d00d..3125092 100644 --- a/templates/MyProject.Scheduler/Program.cs +++ b/templates/MyProject.Scheduler/Program.cs @@ -1,4 +1,4 @@ -using System.Diagnostics; +using MyProject.Scheduler.Infrastructure; using MyProject.Scheduler.Setup; using Simplify.DI; using Simplify.Scheduler; @@ -17,31 +17,24 @@ private static void Main(string[] args) #endif //+:cnd:noEmit - InitializeContainer(); + // IOC container setup + IocRegistrations.Register().Verify(); - using var scheduler = new SingleTaskScheduler(IocRegistrations.Configuration); - - scheduler.OnException += OnException; + using var scheduler = new SingleTaskScheduler(IocRegistrations.Configuration) + .SubscribeLog(); if (scheduler.Start(args)) return; - // Launch without the scheduler for testing - - using var scope = DIContainer.Current.BeginLifetimeScope(); - scope.Resolver.Resolve().Run(); - } - - private static void OnException(SchedulerExceptionArgs args) - { - Trace.WriteLine(args.Exception.Message); + if (!scheduler.Start(args)) + // Launch without the scheduler for testing + RunAsAConsoleApplication(); } - private static void InitializeContainer() + private static void RunAsAConsoleApplication() { - IocRegistrations.Register(); - - DIContainer.Current.Verify(); + using var scope = DIContainer.Current.BeginLifetimeScope(); + scope.Resolver.Resolve().Run(); } } } \ No newline at end of file diff --git a/templates/MyProject.Scheduler/Setup/IocRegistrations.cs b/templates/MyProject.Scheduler/Setup/IocRegistrations.cs index 6627ff5..b342644 100644 --- a/templates/MyProject.Scheduler/Setup/IocRegistrations.cs +++ b/templates/MyProject.Scheduler/Setup/IocRegistrations.cs @@ -3,24 +3,27 @@ namespace MyProject.Scheduler.Setup { - public class IocRegistrations + public static class IocRegistrations { public static IConfiguration Configuration { get; private set; } - public static void Register() + public static IDIContainerProvider Register() { - RegisterConfiguration(); + DIContainer.Current.RegisterConfiguration() + .Register(); - DIContainer.Current.Register(); + return DIContainer.Current; } - private static void RegisterConfiguration() + private static IDIRegistrator RegisterConfiguration(this IDIRegistrator registrator) { Configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", false) .Build(); - DIContainer.Current.Register(p => Configuration, LifetimeType.Singleton); + registrator.Register(p => Configuration, LifetimeType.Singleton); + + return registrator; } } } \ No newline at end of file diff --git a/templates/MyProject.WindowsService/Infrastructure/WindowsServiceHandlerObserver.cs b/templates/MyProject.WindowsService/Infrastructure/WindowsServiceHandlerObserver.cs new file mode 100644 index 0000000..ba99b84 --- /dev/null +++ b/templates/MyProject.WindowsService/Infrastructure/WindowsServiceHandlerObserver.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using Simplify.WindowsServices; + +namespace MyProject.WindowsService.Infrastructure +{ + public static class WindowsServiceHandlerObserver + { + public static MultitaskServiceHandler SubscribeLog(this MultitaskServiceHandler handler) + { + handler.OnException += OnException; + + return handler; + } + + private static void OnException(ServiceExceptionArgs args) + { + Trace.WriteLine(args.Exception.Message); + } + } +} \ No newline at end of file diff --git a/templates/MyProject.WindowsService/MyProject.WindowsService.csproj b/templates/MyProject.WindowsService/MyProject.WindowsService.csproj index 3dcbbfa..8a59f60 100644 --- a/templates/MyProject.WindowsService/MyProject.WindowsService.csproj +++ b/templates/MyProject.WindowsService/MyProject.WindowsService.csproj @@ -1,12 +1,13 @@  net462 + 8.0 0.1 MyProject.WindowsService service Exe - + diff --git a/templates/MyProject.WindowsService/Program.cs b/templates/MyProject.WindowsService/Program.cs index fcc6ddf..0c859f4 100644 --- a/templates/MyProject.WindowsService/Program.cs +++ b/templates/MyProject.WindowsService/Program.cs @@ -1,4 +1,4 @@ -using System.Diagnostics; +using MyProject.WindowsService.Infrastructure; using MyProject.WindowsService.Setup; using Simplify.DI; using Simplify.WindowsServices; @@ -17,31 +17,20 @@ private static void Main(string[] args) #endif //+:cnd:noEmit - InitializeContainer(); + // IOC container setup + IocRegistrations.Register().Verify(); - using (var handler = new SingleTaskServiceHandler(IocRegistrations.Configuration)) - { - handler.OnException += OnException; + using var handler = new SingleTaskServiceHandler(IocRegistrations.Configuration) + .SubscribeLog(); - if (handler.Start(args)) - return; - } - - // On time launch as a console application for testing - using (var scope = DIContainer.Current.BeginLifetimeScope()) - scope.Resolver.Resolve().Run(); + if (!handler.Start(args)) + RunAsAConsoleApplication(); } - private static void OnException(ServiceExceptionArgs args) + private static void RunAsAConsoleApplication() { - Trace.WriteLine(args.Exception.Message); - } - - private static void InitializeContainer() - { - IocRegistrations.Register(); - - DIContainer.Current.Verify(); + using var scope = DIContainer.Current.BeginLifetimeScope(); + scope.Resolver.Resolve().Run(); } } } \ No newline at end of file diff --git a/templates/MyProject.WindowsService/Setup/IocRegistrations.cs b/templates/MyProject.WindowsService/Setup/IocRegistrations.cs index 8c2dd79..9631db2 100644 --- a/templates/MyProject.WindowsService/Setup/IocRegistrations.cs +++ b/templates/MyProject.WindowsService/Setup/IocRegistrations.cs @@ -3,24 +3,27 @@ namespace MyProject.WindowsService.Setup { - public class IocRegistrations + public static class IocRegistrations { public static IConfiguration Configuration { get; private set; } - public static void Register() + public static IDIContainerProvider Register() { - RegisterConfiguration(); + DIContainer.Current.RegisterConfiguration() + .Register(); - DIContainer.Current.Register(); + return DIContainer.Current; } - private static void RegisterConfiguration() + private static IDIRegistrator RegisterConfiguration(this IDIRegistrator registrator) { Configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", false) .Build(); - DIContainer.Current.Register(p => Configuration, LifetimeType.Singleton); + registrator.Register(p => Configuration, LifetimeType.Singleton); + + return registrator; } } } \ No newline at end of file