-
-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathConfiguringDocumentStore.cs
More file actions
248 lines (206 loc) · 7.06 KB
/
Copy pathConfiguringDocumentStore.cs
File metadata and controls
248 lines (206 loc) · 7.06 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System.Linq;
using System.Threading.Tasks;
using JasperFx;
using Marten.Services;
using Marten.Storage;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Newtonsoft.Json;
using Weasel.Core;
using Weasel.Postgresql;
using Marten.Newtonsoft;
namespace Marten.Testing.Examples;
// Leave this commented out please, and always use the User
// in Marten.Testing.Documents
/*
#region sample_user_document
public class User
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Internal { get; set; }
public string? UserName { get; set; }
public string? Department { get; set; }
}
#endregion
*/
public class ConfiguringDocumentStore
{
public async Task start_a_basic_store()
{
#region sample_start_a_store
var store = DocumentStore
.For("host=localhost;database=marten_testing;password=mypassword;username=someuser");
#endregion
#region sample_start_a_query_session
await using (var session = store.QuerySession())
{
var internalUsers = await session
.Query<User>()
.Where(x => x.Internal)
.ToListAsync();
}
#endregion
#region sample_opening_sessions
// Open a session for querying, loading, and
// updating documents
await using (var session = store.LightweightSession())
{
var user = new User { FirstName = "Han", LastName = "Solo" };
session.Store(user);
await session.SaveChangesAsync();
}
#endregion
}
public void start_a_complex_store()
{
#region sample_start_a_complex_store
var store = DocumentStore.For(_ =>
{
// Turn this off in production
_.AutoCreateSchemaObjects = AutoCreate.None;
// This is still mandatory
_.Connection("some connection string");
});
#endregion
}
public void customize_serializer()
{
#region sample_customize_serializer
var store = DocumentStore.For(_ =>
{
_.Connection("some connection string");
// System.Text.Json - Enabled by default
_.UseSystemTextJsonForSerialization(); // [!code ++]
// Newtonsoft - Opt-in
_.UseNewtonsoftForSerialization(); // [!code ++]
});
#endregion
}
public void customize_json_net_serialization()
{
#region sample_customize_json_net_serialization
var serializer = new Marten.Services.JsonNetSerializer();
// To change the enum storage policy to store Enum's as strings:
serializer.EnumStorage = EnumStorage.AsString;
// All other customizations:
serializer.Configure(_ =>
{
// Code directly against a Newtonsoft.Json JsonSerializer
_.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
_.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
});
var store = DocumentStore.For(_ =>
{
// Replace the default JsonNetSerializer with the one we configured
// above
_.Serializer(serializer);
});
#endregion
}
public void customize_json_enum_storage_serialization()
{
#region sample_customize_json_enum_storage_serialization
var store = DocumentStore.For(_ =>
{
// STJ // [!code focus:5]
_.UseSystemTextJsonForSerialization(enumStorage: EnumStorage.AsString);
// Newtonsoft
_.UseNewtonsoftForSerialization(enumStorage: EnumStorage.AsString);
});
#endregion
}
public void customize_json_camelcase_casing_serialization()
{
#region sample_customize_json_camelcase_casing_serialization
var store = DocumentStore.For(_ =>
{
// STJ // [!code focus:5]
_.UseSystemTextJsonForSerialization(casing: Casing.CamelCase);
// Newtonsoft
_.UseNewtonsoftForSerialization(casing: Casing.CamelCase);
});
#endregion
}
public void customize_json_net_snakecase_collectionstorage()
{
#region sample_customize_json_net_snakecase_collectionstorage
var store = DocumentStore.For(_ =>
{
// Replace the default (strongly typed) JsonNetSerializer collection storage // [!code focus:3]
// with JSON array formatting
_.UseNewtonsoftForSerialization(collectionStorage: CollectionStorage.AsArray);
});
#endregion
}
public void customize_json_net_nonpublicsetters()
{
#region sample_customize_json_net_nonpublicsetters
var store = DocumentStore.For(_ =>
{
// Allow the JsonNetSerializer to also deserialize using non-public setters // [!code focus:2]
_.UseNewtonsoftForSerialization(nonPublicMembersStorage: NonPublicMembersStorage.NonPublicSetters);
});
#endregion
}
public void customize_serializers_advanced()
{
#region sample_customize_json_advanced
var store = DocumentStore.For(_ =>
{
_.UseNewtonsoftForSerialization( // [!code focus:14]
enumStorage: EnumStorage.AsString,
configure: settings =>
{
settings.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
});
_.UseSystemTextJsonForSerialization(
enumStorage: EnumStorage.AsString,
configure: settings =>
{
settings.MaxDepth = 100;
});
});
#endregion
}
public void setting_event_schema()
{
#region sample_setting_event_schema
var store = DocumentStore.For(_ =>
{
_.Connection("some connection string");
// Places all the Event Store schema objects
// into the "events" schema
_.Events.DatabaseSchemaName = "events";
});
#endregion
}
#region sample_custom-store-options
public class MyStoreOptions: StoreOptions
{
public static IDocumentStore ToStore()
{
return new DocumentStore(new MyStoreOptions());
}
public MyStoreOptions()
{
Connection(ConnectionSource.ConnectionString);
Serializer(new JsonNetSerializer { EnumStorage = EnumStorage.AsString });
Schema.For<User>().Index(x => x.UserName);
}
}
#endregion
public void set_multi_tenancy_on_events()
{
#region sample_making_the_events_multi_tenanted
var store = DocumentStore.For(opts =>
{
opts.Connection("some connection string");
// And that's all it takes, the events are now multi-tenanted
opts.Events.TenancyStyle = TenancyStyle.Conjoined;
});
#endregion
}
}