Skip to content

Commit ee80471

Browse files
committed
grrr
1 parent 65ab68b commit ee80471

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

docs/document/Avalonia/docs/MVVM Essentials/1. Introduction.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ A model should pure with a bunch of properties only.(May include implicit conver
1919
## ViewModel
2020

2121
ViewModel is the mediator for Model and View.
22-
Isolates the two, making View completely ignorant to Model.(Model surely don't know about View, it's just a pure shape.)
22+
23+
Isolates the two, making View completely ignorant to Model.(Model surely don't know about View, it's just a pure shape)
24+
2325
It represents the status of a Model instance as a Observable with extra logic for creating or manipulating itself.
2426

2527
## View

docs/document/Avalonia/docs/MVVM Essentials/3. Let Property Observable.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Foo : ObservableObject
1919
}
2020
```
2121

22+
## ObservableProperty Approach
23+
2224
`ObservableProperty` saves even more. It's empowered by source generator, which needs an extra `partial` mark for emiting code at somewhere else.
2325

2426
```cs
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Trigger a Command
2+
3+
## RelayCommand
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Command
2+
3+
An object to instruct a specific action with all required information.
4+
5+
## Motivation
6+
7+
Actions and assignments can't be serialized generally, a Command pattern solves by:
8+
9+
- provides a way to store information of actions.
10+
- can undo base on the kept command information.
11+
12+
It's heavily used by cli implementations and GUI development.
13+
14+
## ICommand
15+
16+
```cs
17+
var editor = new Editor(content: "hello");
18+
// wrap command info inside a instance
19+
var commandInsert = new EditorCommand(editor, EditorCommandType.Insert, ", world"); // [!code highlight]
20+
var commandDelete = new EditorCommand(editor, EditorCommandType.Delete, 7); // [!code highlight]
21+
22+
commandInsert.Execute(); // [!code highlight]
23+
commandDelete.Execute(); // [!code highlight]
24+
25+
public interface ICommand
26+
{
27+
void Execute();
28+
}
29+
30+
public record class EditorCommand(Editor Editor, EditorCommandType CommandType, params object[]? Args) : ICommand
31+
{
32+
public void Execute()
33+
{
34+
switch (CommandType)
35+
{
36+
case EditorCommandType.Insert:
37+
Editor.Insert((string)(Args?[0] ?? string.Empty));
38+
break;
39+
case EditorCommandType.Delete:
40+
Editor.Delete((int)(Args?[0] ?? 0));
41+
break;
42+
default:
43+
break;
44+
}
45+
}
46+
47+
public enum EditorCommandType
48+
{
49+
Insert, Delete
50+
}
51+
}
52+
53+
public class Editor
54+
{
55+
private string _content = string.Empty;
56+
57+
public Editor(string content) => _content = content;
58+
59+
public void Insert(string content)
60+
{
61+
_content += content;
62+
Console.WriteLine(_content);
63+
}
64+
public void Delete(int count)
65+
{
66+
_content = _content[..(_content.Length - count)];
67+
Console.WriteLine(_content);
68+
}
69+
}
70+
```
71+
72+
## Undo a Command
73+
74+
```cs
75+
var editor = new Editor(content: "hello");
76+
var commandInsert = new EditorCommand(editor, EditorCommandType.Insert, ", world");
77+
var commandDelete = new EditorCommand(editor, EditorCommandType.Delete, 7);
78+
79+
commandInsert.Execute();
80+
commandDelete.Execute();
81+
82+
commandDelete.Undo(); // [!code ++]
83+
commandInsert.Undo(); // [!code ++]
84+
85+
public interface ICommand
86+
{
87+
void Execute();
88+
void Undo(); // [!code ++]
89+
}
90+
91+
public enum EditorCommandType
92+
{
93+
Insert, Delete
94+
}
95+
96+
public record class EditorCommand(Editor Editor, EditorCommandType CommandType, params object[]? Args) : ICommand
97+
{
98+
public int InsertLenght { get; init; } = Args?[0] is string s ? s.Length : 0; // [!code ++]
99+
100+
private bool succeed; // implying last execute succeed or not // [!code ++]
101+
102+
public void Execute()
103+
{
104+
switch (CommandType)
105+
{
106+
case EditorCommandType.Insert:
107+
Editor.Insert((string)(Args?[0] ?? string.Empty));
108+
succeed = true;
109+
break;
110+
case EditorCommandType.Delete:
111+
succeed = Editor.Delete((int)(Args?[0] ?? 0));
112+
break;
113+
default:
114+
break;
115+
}
116+
}
117+
118+
public void Undo() // [!code ++]
119+
{ // [!code ++]
120+
if (!succeed) return; // if previous execution failed, no need to undo // [!code ++]
121+
switch (CommandType) // [!code ++]
122+
{ // [!code ++]
123+
case EditorCommandType.Insert: // [!code ++]
124+
Editor.Delete(InsertLenght); // [!code ++]
125+
break; // [!code ++]
126+
case EditorCommandType.Delete: // [!code ++]
127+
Editor.Insert(Editor.Deleted); // [!code ++]
128+
break; // [!code ++]
129+
default: // [!code ++]
130+
break; // [!code ++]
131+
} // [!code ++]
132+
} // [!code ++]
133+
}
134+
135+
public class Editor
136+
{
137+
private string _content = string.Empty;
138+
139+
public string Deleted { get; set; } = string.Empty; // [!code ++]
140+
141+
public Editor(string content) => _content = content;
142+
143+
public void Insert(string content)
144+
{
145+
_content += content;
146+
Console.WriteLine(_content);
147+
}
148+
public bool Delete(int count) // [!code highlight]
149+
{
150+
if (_content.Length >= count) // [!code ++]
151+
{ // [!code ++]
152+
Deleted = _content[^count..]; // [!code ++]
153+
_content = _content[..(_content.Length - count)]; // [!code ++]
154+
Console.WriteLine(_content); // [!code ++]
155+
return true; // [!code ++]
156+
} // [!code ++]
157+
return false; // [!code ++]
158+
}
159+
}
160+
```

0 commit comments

Comments
 (0)