-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Classes.lib.ps1
155 lines (119 loc) · 4.21 KB
/
Classes.lib.ps1
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
149
150
151
152
153
154
155
<#
.Synopsis
PowerShell classes for tests of saving and reading with -As.
.Description
Tip: Define types with Bson* attributes in a separate script and dot-source
it to the consumer script after `Import-Module Mdbc`. If you define classes
right in the consumer script PowerShell may fail to parse Bson* attributes.
For Bson* serialization attributes and type mapping, see:
https://mongodb.github.io/mongo-csharp-driver/2.9/reference/bson/mapping/
See Classes.test.ps1 for tests with this script classes.
#>
# To avoid long type names
using namespace MongoDB.Bson.Serialization.Attributes
using namespace System.Collections.Generic
##############################################################################
### Serialized types
# Serialized types are registered by Register-MdbcClassMap.
# Use Bson* attributes in order to customise serialization.
# This class ignores fields other than .c and .a
# and maps them as .c ~ .PostCode, .a ~ .Address
[BsonIgnoreExtraElements()]
class Address {
[BsonElement('c')]
[string] $PostCode
[BsonElement('a')]
[string] $Address
}
# This class maps .Pin to ._id and puts all extra fields to .Extra of the
# convenient type Mdbc.Dictionary. BsonExtraElements works both ways: on
# reading extras are added to $Extra, on saving $Extra's entries become
# fields of the saved document.
class Person {
[BsonId()]
[int] $Pin
[string] $Name
[BsonIgnoreIfDefault()]
[Address] $Address
[BsonExtraElements()]
[Mdbc.Dictionary] $Extra
}
# Register types as serialized, either using the types or full names.
# Other parameters are not needed, attributes drive the serialization.
Register-MdbcClassMap ([Address])
Register-MdbcClassMap Person
##############################################################################
### Polymorphic sub-documents
# - PolyBase: base type for PolyType1 and PolyType2
# - PolyData: document using mixed PolyType1 and PolyType2
class PolyBase {
$b1
}
class PolyType1 : PolyBase {
$p1
}
class PolyType2 : PolyBase {
$p2
}
class PolyData {
$name
[List[PolyBase]]$data
}
# NOTE: In this scenario we tweak serialization by Register-MdbcClassMap, not
# by Bson* attributes. As a result, classes may be easily defined right where
# they are used, there is no need in a separate dot-sourced file with classes.
Register-MdbcClassMap PolyBase
Register-MdbcClassMap PolyType1 -Discriminator T1
Register-MdbcClassMap PolyType2 -Discriminator T2
Register-MdbcClassMap PolyData -IdProperty name
##############################################################################
### Polymorphic top documents
# - MyTypeBase
# This is the base class for all top level documents. It contains .id (driver
# maps to _id automatically). Top level documents are defined by child
# classes. Important: set DiscriminatorIsRequired, so that derived types
# inherit it and save their discriminators _t for later typed reading.
# - MyType1, MyType2
# Top level documents are classes derived from MyTypeBase.
class MyTypeBase {
$id
}
class MyType1 : MyTypeBase {
$p1
}
class MyType2 : MyTypeBase {
$p2
}
Register-MdbcClassMap MyTypeBase -DiscriminatorIsRequired
Register-MdbcClassMap MyType1
Register-MdbcClassMap MyType2
##############################################################################
### Simple types
# Simple types are not registered and do not have Bson* attributes. On saving
# they are converted to documents by properties, on reading with `-As` fields
# are literally mapped to properties.
# Trivial class: no types, no attributes, even one-liner.
# (Array -> Mdbc.Collection, Document -> Mdbc.Dictionary)
class PlainObject {$_id; $foo; $arr; $doc}
# Ditto with some types, to change default Array/Document types:
class PlainObject2 {
$_id
$foo
[object[]]$arr
[hashtable]$doc
}
# Some members may have to be declared with an appropriate type:
class FooByteArray {
$_id
[byte[]]$foo
}
class FooBsonValue {
$_id
[MongoDB.Bson.BsonValue]$foo
}
##############################################################################
### More technical, less how-to cases
class SerialWithContainers1 {$_id; $arr; $doc}
class SerialWithContainers2 {$_id; [Mdbc.Collection]$arr; [Mdbc.Dictionary]$doc}
Register-MdbcClassMap SerialWithContainers1
Register-MdbcClassMap SerialWithContainers2