Skip to content

Commit

Permalink
Templates refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
i4004 committed Jan 14, 2021
1 parent 2255ca0 commit d1e0585
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 57 deletions.
6 changes: 2 additions & 4 deletions Simplify.ProjectsTemplates.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>

<PackageType>Template</PackageType>
<PackageVersion>0.1.3</PackageVersion>
<PackageVersion>0.2</PackageVersion>
<PackageId>Simplify.ProjectsTemplates</PackageId>
<IsPackable>true</IsPackable>
<PackageProjectUrl>https://github.com/SimplifyNet/Simplify.ProjectsTemplates</PackageProjectUrl>
Expand All @@ -12,9 +12,7 @@
<RepositoryType>GIT</RepositoryType>
<PackageReleaseNotes>
Updates
* Upgrade to Simplify.Scheduler 1.0
* Upgrade to Microsoft.Extensions.Configuration.Json 3.1.5
* Switch to .NET Core 3.1
Templates refactoring
</PackageReleaseNotes>
<Title>Simplify project templates</Title>
<Authors>Alexander Krylkov</Authors>
Expand Down
4 changes: 4 additions & 0 deletions Simplify.ProjectsTemplates.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<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">
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Crontab/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=registrator/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion templates/MyProject.Scheduler/MyProject.Scheduler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.11" />
<PackageReference Include="Simplify.Scheduler" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
Expand Down
29 changes: 11 additions & 18 deletions templates/MyProject.Scheduler/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using MyProject.Scheduler.Infrastructure;
using MyProject.Scheduler.Setup;
using Simplify.DI;
using Simplify.Scheduler;
Expand All @@ -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<Worker>(IocRegistrations.Configuration);

scheduler.OnException += OnException;
using var scheduler = new SingleTaskScheduler<Worker>(IocRegistrations.Configuration)
.SubscribeLog();

if (scheduler.Start(args))
return;

// Launch without the scheduler for testing

using var scope = DIContainer.Current.BeginLifetimeScope();
scope.Resolver.Resolve<Worker>().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<Worker>().Run();
}
}
}
15 changes: 9 additions & 6 deletions templates/MyProject.Scheduler/Setup/IocRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Worker>();

DIContainer.Current.Register<Worker>();
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<LangVersion>8.0</LangVersion>
<Version>0.1</Version>
<Description>MyProject.WindowsService service</Description>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.11" />
<PackageReference Include="Simplify.WindowsServices" Version="2.12.0" />
</ItemGroup>
<ItemGroup>
Expand Down
31 changes: 10 additions & 21 deletions templates/MyProject.WindowsService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using MyProject.WindowsService.Infrastructure;
using MyProject.WindowsService.Setup;
using Simplify.DI;
using Simplify.WindowsServices;
Expand All @@ -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<Worker>(IocRegistrations.Configuration))
{
handler.OnException += OnException;
using var handler = new SingleTaskServiceHandler<Worker>(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<Worker>().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<Worker>().Run();
}
}
}
15 changes: 9 additions & 6 deletions templates/MyProject.WindowsService/Setup/IocRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Worker>();

DIContainer.Current.Register<Worker>();
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;
}
}
}

0 comments on commit d1e0585

Please sign in to comment.