-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocumentCollection.cs
91 lines (74 loc) · 2.82 KB
/
DocumentCollection.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Net.DDP.Server;
namespace Net.DDP.Server
{
public class DocumentCollection : IEnumerable<KeyValuePair<string, ReactiveDocument>>
{
public string Name { get; }
readonly Dictionary<string, ReactiveDocument> _documents = new Dictionary<string, ReactiveDocument>();
public event DocumentEvent Changed;
public event DocumentEvent Added;
public event DocumentEvent Removed;
internal DocumentCollection(string name)
{
Name = name;
}
protected void OnChanged(ReactiveDocument document, PropertyChangedEventArgs args)
{
Changed?.Invoke(this, new DocumentEventArgs() {Document = document, EventType = EventType.Changed, PropertyEventArgs = args});
}
protected void OnAdded(ReactiveDocument document)
{
Added?.Invoke(this, new DocumentEventArgs() {Document = document, EventType = EventType.Added});
}
protected void OnRemoved(ReactiveDocument document)
{
Removed?.Invoke(this, new DocumentEventArgs() { Document = document, EventType = EventType.Removed });
}
public void Add(ReactiveDocument document)
{
if (_documents.ContainsKey(document.Id))
{
throw new DuplicateNameException(String.Format("A document with the same id already exists. ID {0}", document.Id));
}
document.PropertyChanged += (sender, args) => OnChanged(sender as ReactiveDocument, args);
_documents.Add(document.Id, document);
OnAdded(document);
}
public void Change(ReactiveDocument document)
{
if (!_documents.ContainsKey(document.Id))
{
throw new DirectoryNotFoundException(String.Format("A document with the same id cannot be found. ID {0}", document.Id));
}
_documents[document.Id] = document;
OnChanged(document, null);
}
public void Remove(string id)
{
if (!_documents.ContainsKey(id))
{
throw new DirectoryNotFoundException(String.Format("A document with the same id cannot be found. ID {0}", id));
}
var document = _documents[id];
_documents.Remove(id);
OnRemoved(document);
}
public IEnumerator<KeyValuePair<string, ReactiveDocument>> GetEnumerator()
{
return _documents.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}