added timer service to home controller

This commit is contained in:
chsakell
2016-09-30 11:32:27 +03:00
parent 63866a2746
commit 1f14c0c55f
10 changed files with 94 additions and 14 deletions

View File

@@ -0,0 +1,15 @@
using AutoMapper;
namespace LiveGameFeed.Core.Mappings
{
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<DomainToViewModelMappingProfile>();
});
}
}
}

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using LiveGameFeed.Models;
namespace LiveGameFeed.Core.Mappings
{
public class DomainToViewModelMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<Match, MatchViewModel>();
Mapper.CreateMap<Feed, FeedViewModel>();
}
}
}

View File

@@ -0,0 +1,9 @@
using System;
namespace LiveGameFeed.Core.MvcTimer
{
public interface ITimerService
{
event EventHandler TimerElapsed;
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace LiveGameFeed.Core.MvcTimer
{
public class TimerEventArgs : EventArgs
{
public TimerEventArgs(int value)
{
Value = value;
}
public int Value { get; }
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Threading;
using Microsoft.Extensions.Options;
namespace LiveGameFeed.Core.MvcTimer
{
public class TimerService : ITimerService
{
private Timer _timer;
readonly Random _random = new Random();
public event EventHandler TimerElapsed;
public TimerService(IOptions<TimerServiceConfiguration> options)
{
var optionsTimerServiceConfiguration = options;
_timer = new Timer(
OnTimerElapsed,
null,
optionsTimerServiceConfiguration.Value.DueTime,
optionsTimerServiceConfiguration.Value.Period);
}
private void OnTimerElapsed(object sender)
{
TimerElapsed?.Invoke(this, new TimerEventArgs(_random.Next(0, 100)));
}
}
}

View File

@@ -0,0 +1,9 @@
namespace LiveGameFeed.Core.MvcTimer
{
public class TimerServiceConfiguration
{
public int DueTime { get; set; }
public int Period { get; set; }
}
}