Skip to content

Commit d1e0585

Browse files
committed
Templates refactoring
1 parent 2255ca0 commit d1e0585

File tree

10 files changed

+88
-57
lines changed

10 files changed

+88
-57
lines changed

Simplify.ProjectsTemplates.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>netcoreapp3.1</TargetFramework>
44

55
<PackageType>Template</PackageType>
6-
<PackageVersion>0.1.3</PackageVersion>
6+
<PackageVersion>0.2</PackageVersion>
77
<PackageId>Simplify.ProjectsTemplates</PackageId>
88
<IsPackable>true</IsPackable>
99
<PackageProjectUrl>https://github.com/SimplifyNet/Simplify.ProjectsTemplates</PackageProjectUrl>
@@ -12,9 +12,7 @@
1212
<RepositoryType>GIT</RepositoryType>
1313
<PackageReleaseNotes>
1414
Updates
15-
* Upgrade to Simplify.Scheduler 1.0
16-
* Upgrade to Microsoft.Extensions.Configuration.Json 3.1.5
17-
* Switch to .NET Core 3.1
15+
Templates refactoring
1816
</PackageReleaseNotes>
1917
<Title>Simplify project templates</Title>
2018
<Authors>Alexander Krylkov</Authors>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Crontab/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=registrator/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Diagnostics;
2+
using Simplify.Scheduler;
3+
4+
namespace MyProject.Scheduler.Infrastructure
5+
{
6+
public static class SchedulerHandlerObserver
7+
{
8+
public static MultitaskScheduler SubscribeLog(this MultitaskScheduler handler)
9+
{
10+
handler.OnException += OnException;
11+
12+
return handler;
13+
}
14+
15+
private static void OnException(SchedulerExceptionArgs args)
16+
{
17+
Trace.WriteLine(args.Exception.Message);
18+
}
19+
}
20+
}

templates/MyProject.Scheduler/MyProject.Scheduler.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<OutputType>Exe</OutputType>
88
</PropertyGroup>
99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.11" />
1111
<PackageReference Include="Simplify.Scheduler" Version="1.0.0" />
1212
</ItemGroup>
1313
<ItemGroup>
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Diagnostics;
1+
using MyProject.Scheduler.Infrastructure;
22
using MyProject.Scheduler.Setup;
33
using Simplify.DI;
44
using Simplify.Scheduler;
@@ -17,31 +17,24 @@ private static void Main(string[] args)
1717
#endif
1818
//+:cnd:noEmit
1919

20-
InitializeContainer();
20+
// IOC container setup
21+
IocRegistrations.Register().Verify();
2122

22-
using var scheduler = new SingleTaskScheduler<Worker>(IocRegistrations.Configuration);
23-
24-
scheduler.OnException += OnException;
23+
using var scheduler = new SingleTaskScheduler<Worker>(IocRegistrations.Configuration)
24+
.SubscribeLog();
2525

2626
if (scheduler.Start(args))
2727
return;
2828

29-
// Launch without the scheduler for testing
30-
31-
using var scope = DIContainer.Current.BeginLifetimeScope();
32-
scope.Resolver.Resolve<Worker>().Run();
33-
}
34-
35-
private static void OnException(SchedulerExceptionArgs args)
36-
{
37-
Trace.WriteLine(args.Exception.Message);
29+
if (!scheduler.Start(args))
30+
// Launch without the scheduler for testing
31+
RunAsAConsoleApplication();
3832
}
3933

40-
private static void InitializeContainer()
34+
private static void RunAsAConsoleApplication()
4135
{
42-
IocRegistrations.Register();
43-
44-
DIContainer.Current.Verify();
36+
using var scope = DIContainer.Current.BeginLifetimeScope();
37+
scope.Resolver.Resolve<Worker>().Run();
4538
}
4639
}
4740
}

templates/MyProject.Scheduler/Setup/IocRegistrations.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33

44
namespace MyProject.Scheduler.Setup
55
{
6-
public class IocRegistrations
6+
public static class IocRegistrations
77
{
88
public static IConfiguration Configuration { get; private set; }
99

10-
public static void Register()
10+
public static IDIContainerProvider Register()
1111
{
12-
RegisterConfiguration();
12+
DIContainer.Current.RegisterConfiguration()
13+
.Register<Worker>();
1314

14-
DIContainer.Current.Register<Worker>();
15+
return DIContainer.Current;
1516
}
1617

17-
private static void RegisterConfiguration()
18+
private static IDIRegistrator RegisterConfiguration(this IDIRegistrator registrator)
1819
{
1920
Configuration = new ConfigurationBuilder()
2021
.AddJsonFile("appsettings.json", false)
2122
.Build();
2223

23-
DIContainer.Current.Register(p => Configuration, LifetimeType.Singleton);
24+
registrator.Register(p => Configuration, LifetimeType.Singleton);
25+
26+
return registrator;
2427
}
2528
}
2629
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Diagnostics;
2+
using Simplify.WindowsServices;
3+
4+
namespace MyProject.WindowsService.Infrastructure
5+
{
6+
public static class WindowsServiceHandlerObserver
7+
{
8+
public static MultitaskServiceHandler SubscribeLog(this MultitaskServiceHandler handler)
9+
{
10+
handler.OnException += OnException;
11+
12+
return handler;
13+
}
14+
15+
private static void OnException(ServiceExceptionArgs args)
16+
{
17+
Trace.WriteLine(args.Exception.Message);
18+
}
19+
}
20+
}

templates/MyProject.WindowsService/MyProject.WindowsService.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net462</TargetFramework>
4+
<LangVersion>8.0</LangVersion>
45
<Version>0.1</Version>
56
<Description>MyProject.WindowsService service</Description>
67
<OutputType>Exe</OutputType>
78
</PropertyGroup>
89
<ItemGroup>
9-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.11" />
1011
<PackageReference Include="Simplify.WindowsServices" Version="2.12.0" />
1112
</ItemGroup>
1213
<ItemGroup>
Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Diagnostics;
1+
using MyProject.WindowsService.Infrastructure;
22
using MyProject.WindowsService.Setup;
33
using Simplify.DI;
44
using Simplify.WindowsServices;
@@ -17,31 +17,20 @@ private static void Main(string[] args)
1717
#endif
1818
//+:cnd:noEmit
1919

20-
InitializeContainer();
20+
// IOC container setup
21+
IocRegistrations.Register().Verify();
2122

22-
using (var handler = new SingleTaskServiceHandler<Worker>(IocRegistrations.Configuration))
23-
{
24-
handler.OnException += OnException;
23+
using var handler = new SingleTaskServiceHandler<Worker>(IocRegistrations.Configuration)
24+
.SubscribeLog();
2525

26-
if (handler.Start(args))
27-
return;
28-
}
29-
30-
// On time launch as a console application for testing
31-
using (var scope = DIContainer.Current.BeginLifetimeScope())
32-
scope.Resolver.Resolve<Worker>().Run();
26+
if (!handler.Start(args))
27+
RunAsAConsoleApplication();
3328
}
3429

35-
private static void OnException(ServiceExceptionArgs args)
30+
private static void RunAsAConsoleApplication()
3631
{
37-
Trace.WriteLine(args.Exception.Message);
38-
}
39-
40-
private static void InitializeContainer()
41-
{
42-
IocRegistrations.Register();
43-
44-
DIContainer.Current.Verify();
32+
using var scope = DIContainer.Current.BeginLifetimeScope();
33+
scope.Resolver.Resolve<Worker>().Run();
4534
}
4635
}
4736
}

templates/MyProject.WindowsService/Setup/IocRegistrations.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33

44
namespace MyProject.WindowsService.Setup
55
{
6-
public class IocRegistrations
6+
public static class IocRegistrations
77
{
88
public static IConfiguration Configuration { get; private set; }
99

10-
public static void Register()
10+
public static IDIContainerProvider Register()
1111
{
12-
RegisterConfiguration();
12+
DIContainer.Current.RegisterConfiguration()
13+
.Register<Worker>();
1314

14-
DIContainer.Current.Register<Worker>();
15+
return DIContainer.Current;
1516
}
1617

17-
private static void RegisterConfiguration()
18+
private static IDIRegistrator RegisterConfiguration(this IDIRegistrator registrator)
1819
{
1920
Configuration = new ConfigurationBuilder()
2021
.AddJsonFile("appsettings.json", false)
2122
.Build();
2223

23-
DIContainer.Current.Register(p => Configuration, LifetimeType.Singleton);
24+
registrator.Register(p => Configuration, LifetimeType.Singleton);
25+
26+
return registrator;
2427
}
2528
}
2629
}

0 commit comments

Comments
 (0)