-
Notifications
You must be signed in to change notification settings - Fork 8
/
Command.cs
148 lines (122 loc) · 4.9 KB
/
Command.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Collections.Generic;
using OSPSuite.Utility.Collections;
namespace OSPSuite.Core.Commands.Core
{
public interface ICommand
{
/// <summary>
/// Internal id used to identify a command uniquely. This is not the id of the command, since two commands could have the same Id (inverse of inverse)
/// </summary>
string InternalId { get; set; }
/// <summary>
/// Id of the command identifying the action being performed
/// </summary>
CommandId Id { get; set; }
/// <summary>
/// Type of command
/// </summary>
string CommandType { get; set; }
/// <summary>
/// Type of object on which the command is being applied
/// </summary>
string ObjectType { get; set; }
/// <summary>
/// Short description of the command identifying the action (visible in the browser)
/// </summary>
string Description { get; set; }
/// <summary>
/// Comment linked to the command (editable)
/// </summary>
string Comment { get; set; }
/// <summary>
/// Return true if the current command is an inverse command for the command given parameter, otherwise false
/// </summary>
bool IsInverseFor(ICommand command);
/// <summary>
/// Return true if the command should be displayed in the browser, otherwise false
/// </summary>
bool Visible { get; set; }
/// <summary>
/// Longer description of the command, that will be only displayed when requested.
/// </summary>
string ExtendedDescription { get; set; }
/// <summary>
/// Returns a the value defined for the extend properties named <paramref name="propertyName" /> if defined otherwise an empty string
/// </summary>
string ExtendedPropertyValueFor(string propertyName);
/// <summary>
/// Adds or replace the property defined by <paramref name="propertyName" /> and the value <paramref name="propertyValue" />
/// </summary>
/// <param name="propertyName"> Name of the property to add or replace </param>
/// <param name="propertyValue"> Value of the property </param>
void AddExtendedProperty(string propertyName, string propertyValue);
/// <summary>
/// Returns the list of all extended properties available in the given command
/// </summary>
IEnumerable<string> AllExtendedProperties { get; }
/// <summary>
/// Returns true if the command content is loaded. If the value is false, the command cannot be used in a rollback
/// Default is true.
/// </summary>
bool Loaded { get; set; }
}
public interface ICommand<in TExecutionContext> : ICommand
{
/// <summary>
/// Execute the command with the given <typeparamref name="TExecutionContext" />
/// </summary>
/// <param name="context"> context with which the command should be executed </param>
void Execute(TExecutionContext context);
}
public interface IReversibleCommand<in TExecutionContext> : ICommand<TExecutionContext>
{
/// <summary>
/// Restore the data from the context used to run the command (this is typically called before creating an inverse command)
/// </summary>
void RestoreExecutionData(TExecutionContext context);
/// <summary>
/// Create an inverse for the current command
/// </summary>
ICommand<TExecutionContext> InverseCommand(TExecutionContext context);
}
public abstract class Command : ICommand
{
private readonly ICache<string, string> _extendedProperties;
public virtual bool Loaded { get; set; }
public CommandId Id { get; set; }
public string InternalId { get; set; }
public string Description { get; set; }
public string Comment { get; set; }
public string ObjectType { get; set; }
public string CommandType { get; set; }
public bool Visible { get; set; }
public string ExtendedDescription { get; set; }
protected Command() : this(new CommandId())
{
}
protected Command(CommandId id)
{
Id = id;
//default: All commands should be visible
Visible = true;
_extendedProperties = new Cache<string, string>();
//default: Commands are loaded
Loaded = true;
}
public string ExtendedPropertyValueFor(string propertyName)
{
if (_extendedProperties.Contains(propertyName))
return _extendedProperties[propertyName];
return string.Empty;
}
public void AddExtendedProperty(string propertyName, string propertyValue)
{
_extendedProperties[propertyName] = propertyValue;
}
public IEnumerable<string> AllExtendedProperties => _extendedProperties.Keys;
public bool IsInverseFor(ICommand command)
{
return command != null && (Id.IsInverseFor(command.Id));
}
}
}