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

(Mostly) using NpgsqlBatch and positional parameters behind the scenes #2894

Merged
merged 8 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified README.md
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/configuration/environment-checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public static async Task use_environment_check()
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/Examples/EnvironmentChecks.cs#L10-L30' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_use_environment_check_in_hosted_service' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
<!-- endSnippet -->
533 changes: 1 addition & 532 deletions docs/configuration/hostbuilder.md

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions docs/configuration/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
::: tip
Newtonsoft.Json is still the default JSON serializer in Marten for backwards compatibility
with previous Marten versions and because it is the most battle-hardened JSON serializer
in the .Net space that "just works."
in the .Net space that "just works."

If you're working on a new system, we recommend enabling the System.Text.Json integration for improved throughput.
If you're working on a new system, we recommend enabling the System.Text.Json integration for improved throughput.
:::

An absolutely essential ingredient in Marten's persistence strategy is JSON serialization of the document objects. Marten aims to make the
Expand Down Expand Up @@ -189,10 +189,10 @@ You can also use other options of `NonPublicMembersStorage`:

- `NonPublicDefaultConstructor` - allows deserialization using non-public default constructor,
- `NonPublicConstructor` - allows deserialization using any constructor. Construction resolution uses the following precedence:
1. Constructor with `JsonConstructor` attribute.
2. Constructor with the biggest parameters' count.
3. If two constructors have the same parameters' count, use public or take the first one.
4. Use default constructor.
1. Constructor with `JsonConstructor` attribute.
2. Constructor with the biggest parameters' count.
3. If two constructors have the same parameters' count, use public or take the first one.
4. Use default constructor.
- `All` - Use both properties with non-public setters and non-public constructors.

When using `System.Text.Json` the only support for private properties is to mark the field using [\[JsonInclude\]](https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonincludeattribute?view=net-6.0) attribute.
Expand Down Expand Up @@ -360,8 +360,8 @@ in order to create the correct SQL queries within JSON bodies.
:::

::: warning
The code below is provided as an example only, `Jil` is no longer maintained
and should not be used in your application.
The code below is provided as an example only, `Jil` is no longer maintained
and should not be used in your application.
:::

<!-- snippet: sample_JilSerializer -->
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration/storeoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,5 @@ var store = DocumentStore.For(_ =>
_.NameDataLength = 100;
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L286-L295' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_setting-name-data-length' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L296-L305' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_setting-name-data-length' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
48 changes: 41 additions & 7 deletions docs/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ public interface IMartenSessionLogger
/// <param name="ex"></param>
void LogFailure(NpgsqlCommand command, Exception ex);

/// <summary>
/// Log a command that executed successfully
/// </summary>
/// <param name="batch"></param>
void LogSuccess(NpgsqlBatch batch);

/// <summary>
/// Log a batch that failed
/// </summary>
/// <param name="batch"></param>
/// <param name="ex"></param>
void LogFailure(NpgsqlBatch batch, Exception ex);

/// <summary>
/// Called immediately after committing an IDocumentSession
/// through SaveChanges() or SaveChangesAsync()
Expand All @@ -261,7 +274,7 @@ public interface IMartenSessionLogger
public void OnBeforeExecute(NpgsqlCommand command);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/IMartenLogger.cs#L11-L67' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_imartenlogger' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/IMartenLogger.cs#L11-L81' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_imartenlogger' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

To apply these logging abstractions, you can either plug your own `IMartenLogger` into the `StoreOptions` object and allow that default logger to create the individual session loggers:
Expand All @@ -274,7 +287,7 @@ var store = DocumentStore.For(_ =>
_.Logger(new ConsoleMartenLogger());
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L146-L153' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-marten-logger' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L156-L163' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-marten-logger' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

You can also directly apply a session logger to any `IQuerySession` or `IDocumentSession` like this:
Expand All @@ -286,7 +299,7 @@ using var session = store.LightweightSession();
// Replace the logger for only this one session
session.Logger = new RecordingLogger();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L155-L161' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-session-logger' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/CoreTests/StoreOptionsTests.cs#L165-L171' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_plugging-in-session-logger' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The session logging is a different abstraction specifically so that you _could_ track database commands issued per session. In effect, my own shop is going to use this capability to understand what HTTP endpoints or service bus message handlers are being unnecessarily chatty in their database interactions. We also hope that the contextual logging of commands per document session makes it easier to understand how our systems behave.
Expand Down Expand Up @@ -317,6 +330,16 @@ public class ConsoleMartenLogger: IMartenLogger, IMartenSessionLogger
Console.WriteLine($" {p.ParameterName}: {GetParameterValue(p)}");
}

public void LogSuccess(NpgsqlBatch batch)
{
foreach (var command in batch.BatchCommands)
{
Console.WriteLine(command.CommandText);
foreach (var p in command.Parameters.OfType<NpgsqlParameter>())
Console.WriteLine($" {p.ParameterName}: {GetParameterValue(p)}");
}
}

private static object? GetParameterValue(NpgsqlParameter p)
{
if (p.Value is IList enumerable)
Expand All @@ -334,15 +357,26 @@ public class ConsoleMartenLogger: IMartenLogger, IMartenSessionLogger

public void LogFailure(NpgsqlCommand command, Exception ex)
{
_stopwatch?.Stop();

Console.WriteLine("Postgresql command failed!");
Console.WriteLine(command.CommandText);
foreach (var p in command.Parameters.OfType<NpgsqlParameter>())
Console.WriteLine($" {p.ParameterName}: {p.Value}");
Console.WriteLine(ex);
}

public void LogFailure(NpgsqlBatch batch, Exception ex)
{
Console.WriteLine("Postgresql command failed!");
foreach (var command in batch.BatchCommands)
{
Console.WriteLine(command.CommandText);
foreach (var p in command.Parameters.OfType<NpgsqlParameter>())
Console.WriteLine($" {p.ParameterName}: {p.Value}");
}

Console.WriteLine(ex);
}

public void RecordSavedChanges(IDocumentSession session, IChangeSet commit)
{
_stopwatch?.Stop();
Expand All @@ -359,7 +393,7 @@ public class ConsoleMartenLogger: IMartenLogger, IMartenSessionLogger
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/IMartenLogger.cs#L69-L136' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_consolemartenlogger' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten/IMartenLogger.cs#L83-L171' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_consolemartenlogger' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Accessing Diagnostics
Expand Down Expand Up @@ -415,7 +449,7 @@ The `IMartenLogger` can be swapped out on any `IQuerySession` or `IDocumentSessi
// session to pipe Marten logging to the xUnit.Net output
theSession.Logger = new TestOutputMartenLogger(_output);
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/archiving_events.cs#L227-L233' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_replacing_logger_per_session' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/archiving_events.cs#L231-L237' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_replacing_logger_per_session' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Previewing the PostgreSQL Query Plan
Expand Down
2 changes: 1 addition & 1 deletion docs/documents/querying/linq/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public void query_with_matches_sql()
user.Id.ShouldBe(u.Id);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L263-L278' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_with_matches_sql' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L267-L282' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_with_matches_sql' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
4 changes: 2 additions & 2 deletions docs/documents/querying/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ a document body, but in that case you will need to supply the full SQL statement
var sumResults = await session
.QueryAsync<int>("select count(*) from mt_doc_target");
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L372-L377' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_by_full_sql' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L376-L381' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_by_full_sql' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

When querying single JSONB properties into a primitive/value type, you'll need to cast the value to the respective postgres type:
Expand All @@ -61,7 +61,7 @@ When querying single JSONB properties into a primitive/value type, you'll need t
var times = await session.QueryAsync<DateTimeOffset>(
"SELECT (data ->> 'ModifiedAt')::timestamptz from mt_doc_user");
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L326-L331' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-queryasync-casting' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/DocumentDbTests/Reading/query_by_sql.cs#L330-L335' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-queryasync-casting' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The basic rules for how Marten handles user-supplied queries are:
Expand Down
4 changes: 2 additions & 2 deletions docs/events/archiving.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var events = await theSession.Events
.Where(x => x.IsArchived)
.ToListAsync();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/archiving_events.cs#L162-L169' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_querying_for_archived_events' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/archiving_events.cs#L166-L173' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_querying_for_archived_events' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

You can also query for all events both archived and not archived with `MaybeArchived()`
Expand All @@ -47,5 +47,5 @@ like so:
var events = await theSession.Events.QueryAllRawEvents()
.Where(x => x.MaybeArchived()).ToListAsync();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/archiving_events.cs#L193-L198' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_for_maybe_archived_events' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/EventSourcingTests/archiving_events.cs#L197-L202' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_for_maybe_archived_events' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
19 changes: 0 additions & 19 deletions docs/events/projections/projection-by-event-type.md
Original file line number Diff line number Diff line change
@@ -1,19 +0,0 @@
# Projecting by Event Type

While projections can target specific stream or streams, it is also possible to project by event types. The following sample demonstrates this with the `CandleProjection` that implements the `ViewProjection` interface to build `Candle` projections from events of type `Tick`.

Introduce a type to hold candle data:

<[sample:sample-type-candle]>

This data will then be populated and updated from observing ticks:

<[sample:sample-type-tick]>

We then introduce a projection that subscribes to the 'Tick' event:

<[sample:sample-candleprojection]>

Lastly, we configure the Event Store to use the newly introduced projection:

<[sample:sample-project-by-event-type]>
Loading
Loading