-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWorkItem.cs
More file actions
104 lines (87 loc) · 2.14 KB
/
WorkItem.cs
File metadata and controls
104 lines (87 loc) · 2.14 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace SoftwarePatterns.Core.State
{
public class WorkItem : ICommands
{
private static WorkItemContainer _container;
private Status _state;
private List<Type> _stateCommandTypes;
private ICommands _stateCommands;
public WorkItem()
{
}
public string Description { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public Status State
{
get { return _state; }
set
{
_state = value;
SetStateCommands();
}
}
private void LoadPossibleTypes()
{
_stateCommandTypes = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => !type.IsAbstract
&& type.Name != typeof(WorkItem).Name.ToString(CultureInfo.InvariantCulture)
&& type.GetInterface(typeof (ICommands).ToString()) != null)
.ToList();
}
private void SetStateCommands()
{
if (_stateCommandTypes == null)
LoadPossibleTypes();
if (_stateCommandTypes != null)
{
var command = _stateCommandTypes
.Where(type => type.Name.ToLowerInvariant().Contains(_state.ToString().ToLowerInvariant()))
.Select(type => (ICommands) Activator.CreateInstance(type, this))
.FirstOrDefault();
if (command == null) throw new ArgumentException("Can't find state command for the given state");
_stateCommands = command;
}
}
public bool Delete()
{
var canDelete = _stateCommands.Delete();
if (canDelete)
_container.Remove(this);
return canDelete;
}
public void Edit(string name, string description)
{
_stateCommands.Edit(name, description);
}
public void Print()
{
_stateCommands.Print();
}
public void SetState(Status newState)
{
_stateCommands.SetState(newState);
}
public static WorkItem Create()
{
var wi = new WorkItem {Id = -1, State = Status.Proposed};
_container.Add(wi);
return wi;
}
public static WorkItem FindById(int id)
{
return _container.WorkItems.FirstOrDefault(item => item.Id == id);
}
public static void Init(WorkItemContainer container)
{
_container = container;
}
}
}