-
-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathMultipleDocumentStores.cs
More file actions
123 lines (96 loc) · 3.43 KB
/
Copy pathMultipleDocumentStores.cs
File metadata and controls
123 lines (96 loc) · 3.43 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
using System.Threading;
using System.Threading.Tasks;
using JasperFx;
using JasperFx.Events.Daemon;
using Marten;
using Marten.Schema;
using Microsoft.Extensions.Hosting;
namespace CoreTests.Examples;
public class MultipleDocumentStores
{
public static async Task bootstrap()
{
#region sample_bootstrapping_separate_store
using var host = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
// You can still use AddMarten() for the main document store
// of this application
services.AddMarten("some connection string");
services.AddMartenStore<IInvoicingStore>(opts =>
{
// All the normal options are available here
opts.Connection("different connection string");
// more configuration
})
// Optionally apply all database schema
// changes on startup
.ApplyAllDatabaseChangesOnStartup()
// Run the async daemon for this database
.AddAsyncDaemon(DaemonMode.HotCold)
// Use IInitialData
.InitializeWith(new DefaultDataSet());
// In a "Production" environment, we're turning off the
// automatic database migrations
services.CritterStackDefaults(x =>
{
x.Production.ResourceAutoCreate = AutoCreate.None;
});
}).StartAsync();
#endregion
}
public static async Task bootstrap_with_session_config()
{
#region sample_bootstrapping_separate_store_with_session_config
using var host = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddMarten("some connection string");
services.AddMartenStore<IInvoicingStore>(opts =>
{
opts.Connection("different connection string");
})
// Use lightweight sessions for this ancillary store
.UseLightweightSessions();
// Or use identity map sessions
// .UseIdentitySessions();
// Or use dirty-tracked sessions
// .UseDirtyTrackedSessions();
// Or use a custom session factory
// .BuildSessionsWith<CustomSessionFactory>();
}).StartAsync();
#endregion
}
}
public class DefaultDataSet: IInitialData
{
public Task Populate(IDocumentStore store, CancellationToken cancellation)
{
throw new System.NotImplementedException();
}
}
#region sample_iinvoicingstore
// These marker interfaces *must* be public
public interface IInvoicingStore : IDocumentStore
{
}
#endregion
#region sample_invoicingservice
public class InvoicingService
{
private readonly IInvoicingStore _store;
// IInvoicingStore can be injected like any other
// service in your IoC container
public InvoicingService(IInvoicingStore store)
{
_store = store;
}
public async Task DoSomethingWithInvoices()
{
// Important to dispose the session when you're done
// with it
await using var session = _store.LightweightSession();
// do stuff with the session you just opened
}
}
#endregion