Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IdentitySession and Lightweight session the default one #2463

Merged
merged 7 commits into from
Feb 13, 2023
Merged
Prev Previous commit
Next Next commit
Added Session Factories for Identity and DirtyTracked sessions
  • Loading branch information
oskardudycz committed Feb 12, 2023
commit b4a84e9acb416c3d20f01ed257cc1ab2488a9697
4 changes: 2 additions & 2 deletions docs/configuration/hostbuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public interface IConfigureMarten
void Configure(IServiceProvider services, StoreOptions options);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/MartenServiceCollectionExtensions.cs#L719-L730' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_iconfiguremarten' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/MartenServiceCollectionExtensions.cs#L728-L739' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_iconfiguremarten' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

You could alternatively implement a custom `IConfigureMarten` class like so:
Expand Down Expand Up @@ -580,7 +580,7 @@ public class Startup
// See https://martendb.io/configuration/optimized_artifact_workflow.html
.OptimizeArtifactWorkflow()
// Spin up the DocumentStore right this second!
.InitializeStore();
.InitializeWith();
}

// And other methods we don't care about here...
Expand Down
2 changes: 1 addition & 1 deletion docs/schema/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ services.AddMarten(opts =>
// database changes on application startup
.ApplyAllDatabaseChangesOnStartup();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L164-L177' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_applyalldatabasechangesonstartup' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/MartenServiceCollectionExtensionsTests.cs#L165-L178' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_applyalldatabasechangesonstartup' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

In the option above, Marten is calling the same functionality within an `IHostedService` background task.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void ConfigureServices(IServiceCollection services)
// See https://martendb.io/configuration/optimized_artifact_workflow.html
.OptimizeArtifactWorkflow()
// Spin up the DocumentStore right this second!
.InitializeStore();
.InitializeWith();
}

// And other methods we don't care about here...
}
#endregion
#endregion
1 change: 1 addition & 0 deletions src/CoreTests/MartenServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Marten.Internal;
using Marten.Internal.Sessions;
using Marten.Services;
using Marten.Sessions;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Microsoft.Extensions.DependencyInjection;
Expand Down
21 changes: 0 additions & 21 deletions src/Marten/LightweightSessionFactory.cs

This file was deleted.

35 changes: 22 additions & 13 deletions src/Marten/MartenServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Marten.Internal;
using Marten.Schema;
using Marten.Services;
using Marten.Sessions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -595,11 +596,24 @@ public MartenConfigurationExpression AddAsyncDaemon(DaemonMode mode)
/// IDocumentStore.LightweightSession();
/// </summary>
/// <returns></returns>
public MartenConfigurationExpression UseLightweightSessions()
{
public MartenConfigurationExpression UseLightweightSessions() =>
BuildSessionsWith<LightweightSessionFactory>();
return this;
}

/// <summary>
/// Use identity sessions by default for the injected IDocumentSession objects. Equivalent to
/// IDocumentStore.IdentitySession();
/// </summary>
/// <returns></returns>
public MartenConfigurationExpression UseIdentitySession() =>
BuildSessionsWith<IdentitySessionFactory>();

/// <summary>
/// Use dirty-tracked sessions by default for the injected IDocumentSession objects. Equivalent to
/// IDocumentStore.DirtyTrackedSession();
/// </summary>
/// <returns></returns>
public MartenConfigurationExpression UseDirtyTrackedSession() =>
BuildSessionsWith<DirtyTrackedSessionFactory>();

/// <summary>
/// Eagerly build the application's DocumentStore during application
Expand Down Expand Up @@ -627,10 +641,8 @@ public IDocumentStore InitializeStore()
/// Adds the optimized artifact workflow to this store. TODO -- LINK TO DOCS
/// </summary>
/// <returns></returns>
public MartenConfigurationExpression OptimizeArtifactWorkflow()
{
return OptimizeArtifactWorkflow(TypeLoadMode.Auto);
}
public MartenConfigurationExpression OptimizeArtifactWorkflow() =>
OptimizeArtifactWorkflow(TypeLoadMode.Auto);

/// <summary>
/// Adds the optimized artifact workflow to this store with ability to override the TypeLoadMode in "Production" mode.
Expand All @@ -651,10 +663,8 @@ public MartenConfigurationExpression OptimizeArtifactWorkflow(TypeLoadMode typeL
/// </summary>
/// <param name="developmentEnvironment"></param>
/// <returns></returns>
public MartenConfigurationExpression OptimizeArtifactWorkflow(string developmentEnvironment)
{
return OptimizeArtifactWorkflow(TypeLoadMode.Auto, developmentEnvironment);
}
public MartenConfigurationExpression OptimizeArtifactWorkflow(string developmentEnvironment) =>
OptimizeArtifactWorkflow(TypeLoadMode.Auto, developmentEnvironment);

/// <summary>
/// Adds the optimized artifact workflow to this store with ability to override the TypeLoadMode in "Production" mode.
Expand All @@ -672,7 +682,6 @@ public MartenConfigurationExpression OptimizeArtifactWorkflow(TypeLoadMode typeL
return this;
}


/// <summary>
/// Adds initial data sets to the Marten store and ensures that they will be
/// executed upon IHost initialization
Expand Down
15 changes: 15 additions & 0 deletions src/Marten/Sessions/DirtyTrackedSessionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Marten.Sessions;

internal class DirtyTrackedSessionFactory: ISessionFactory
{
private readonly IDocumentStore _store;

public DirtyTrackedSessionFactory(IDocumentStore store) =>
_store = store;

public IQuerySession QuerySession() =>
_store.QuerySession();

public IDocumentSession OpenSession() =>
_store.DirtyTrackedSession();
}
15 changes: 15 additions & 0 deletions src/Marten/Sessions/IdentitySessionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Marten.Sessions;

internal class IdentitySessionFactory: ISessionFactory
{
private readonly IDocumentStore _store;

public IdentitySessionFactory(IDocumentStore store) =>
_store = store;

public IQuerySession QuerySession() =>
_store.QuerySession();

public IDocumentSession OpenSession() =>
_store.LightweightSession();
}
15 changes: 15 additions & 0 deletions src/Marten/Sessions/LightweightSessionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Marten.Sessions;

internal class LightweightSessionFactory: ISessionFactory
{
private readonly IDocumentStore _store;

public LightweightSessionFactory(IDocumentStore store) =>
_store = store;

public IQuerySession QuerySession() =>
_store.QuerySession();

public IDocumentSession OpenSession() =>
_store.LightweightSession();
}