-
-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathStartup.cs
More file actions
78 lines (63 loc) · 2.06 KB
/
Copy pathStartup.cs
File metadata and controls
78 lines (63 loc) · 2.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
using System.Data;
using JasperFx;
using Marten;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weasel.Core;
using Weasel.Postgresql;
namespace AspNetCoreWithMarten.Samples.ConfiguringSessionCreation;
#region sample_customsessionfactory
public class CustomSessionFactory: ISessionFactory
{
private readonly IDocumentStore _store;
// This is important! You will need to use the
// IDocumentStore to open sessions
public CustomSessionFactory(IDocumentStore store)
{
_store = store;
}
public IQuerySession QuerySession()
{
return _store.QuerySession();
}
public IDocumentSession OpenSession()
{
// Opting for the "lightweight" session
// option with no identity map tracking
// and choosing to use Serializable transactions
// just to be different
return _store.LightweightSession(IsolationLevel.Serializable);
}
}
#endregion
public class Startup
{
public Startup(IConfiguration configuration, IHostEnvironment hosting)
{
Configuration = configuration;
Hosting = hosting;
}
public IConfiguration Configuration { get; }
public IHostEnvironment Hosting { get; }
public void ConfigureServices(IServiceCollection services)
{
#region sample_addmartenwithcustomsessioncreation
var connectionString = Configuration.GetConnectionString("postgres");
services.AddMarten(opts =>
{
opts.Connection(connectionString);
})
// Chained helper to replace the built in
// session factory behavior
.BuildSessionsWith<CustomSessionFactory>();
// In a "Production" environment, we're turning off the
// automatic database migrations and dynamic code generation
services.CritterStackDefaults(x =>
{
x.Production.ResourceAutoCreate = AutoCreate.None;
});
#endregion
}
// And other methods we don't care about here...
}