Continuing a new blog series on Jasper:
- Jasper’s Configuration Story
- Jasper’s Extension Model
- Integrating Marten into Jasper Applications (this one)
- Durable Messaging in Jasper
- Integrating Jasper into ASP.Net Core Applications
- Jasper’s HTTP Transport
- Jasper’s “Outbox” Support within ASP.Net Core Applications
Using Marten from Jasper Applications
Time to combine two of my biggest passions (time sinks) and show you how easy it is to integrate Marten into Jasper applications. If you already have a Jasper application, start by adding a reference to the Jasper.Marten Nuget. Using Jasper’s extension model, the Jasper.Marten library will automatically add IoC registrations for the Marten:
IDocumentStore
as a singletonIQuerySession
as scopedIDocumentSession
as scoped
At a bare minimum, you’ll at least need to tell Jasper & Marten what the connection string is to the underlying Postgresql database something like this sample:
public class AppWithMarten : JasperRegistry { public AppWithMarten() { // StoreOptions is a Marten object that fulfills the same // role as JasperRegistry Settings.Alter<StoreOptions>((config, marten) => { // At the simplest, you would just need to tell Marten // the connection string to the application database marten.Connection(config.GetConnectionString("marten")); }); } }
In this case, we’re taking advantage of Jasper’s strong typed configuration model to configure the Marten StoreOptions
object that completely configures a Marten DocumentStore
in the underlying IoC container like this from the Jasper.Marten code:
// Just uses the ASP.Net Core DI registrations registry.Services.AddSingleton<IDocumentStore>(x => { var storeOptions = x.GetService<StoreOptions>(); var documentStore = new DocumentStore(storeOptions); return documentStore; });
And for the basics, that’s all there is to it. For right now, the IDocumentSession
service is resolved by calling IDocumentStore.OpenSession()
, but it’s likely users will want to be able to opt for either lightweight sessions or configure different transactional levels. I don’t know what that’s going to look like yet, but it’s definitely something we’ve thought about for the future.
One thought on “Integrating Marten into Jasper Applications”