-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICommand.cs
55 lines (47 loc) · 1.48 KB
/
ICommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Threading.Tasks;
using Domain;
using Newtonsoft.Json;
using Ninject;
using UI.JsonKnownTypes;
namespace UI.Commands
{
public interface ICommand
{
public string Name { get; }
public bool IsInitial { get; }
public Task<ICommandInfo> Execute(User user, string message, IBot bot);
}
[JsonConverter(typeof(MyJsonKnownTypesConverter<ICommandInfo>))]
public interface ICommandInfo
{
public static ICommandInfo Create<TCom>() where TCom : ICommand
{
return new CommandInfoWithoutData<TCom>();
}
public static ICommandInfo Create<TData, TCom>(TData data) where TCom : ICommand
{
return new CommandInfoWithData<TData, TCom>(data);
}
public ICommand GetCommand(StandardKernel container);
}
public class CommandInfoWithData<TData, TCom> : ICommandInfo where TCom : ICommand
{
[JsonProperty] private readonly TData data;
public CommandInfoWithData(TData data)
{
this.data = data;
}
public ICommand GetCommand(StandardKernel container)
{
var commandFactory = container.Get<ICommandFactory<TData, TCom>>();
return commandFactory.CreateCommand(data);
}
}
public class CommandInfoWithoutData<TCom> : ICommandInfo where TCom : ICommand
{
public ICommand GetCommand(StandardKernel container)
{
return container.Get<TCom>();
}
}
}