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

Default DateTimeKind configuration #195

Merged
merged 4 commits into from
Aug 7, 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
7 changes: 7 additions & 0 deletions docs/pages/guide/gridifyGlobalConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ some ORMs like NHibernate don't support this. You can disable this behavior by s
- type: `bool`
- default: `false`

### DefaultDateTimeKind

By default, Gridify uses the `DateTimeKind.Unspecified` when parsing dates. You can change this behavior by setting this property to `DateTimeKind.Utc` or `DateTimeKind.Local`. This option is useful when you want to use Gridify with a database that requires a specific `DateTimeKind`, for example when using npgsql and postgresql.
moxplod marked this conversation as resolved.
Show resolved Hide resolved

- type: `DateTimeKind`
- default: `null`

## CustomOperators

Using the `Register` method of this property you can add your own custom operators.
Expand Down
11 changes: 11 additions & 0 deletions docs/pages/guide/gridifyMapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ By setting this to `false`, Gridify don't allow searching on null values using t
var mapper = new GridifyMapper<Person>(q => q.AllowNullSearch = false);
```

### DefaultDateTimeKind

By setting this property to a `DateTimeKind` value, you can change the default `DateTimeKind` used when parsing dates.

- type: `DateTimeKind`
- default: `null`

``` csharp
var mapper = new GridifyMapper<Person>(q => q.DefaultDateTimeKind = DateTimeKind.Utc);
```

## Filtering on Nested Collections

You can use LINQ `Select` and `SelectMany` methods to filter your data using its nested collections.
Expand Down
8 changes: 8 additions & 0 deletions src/Gridify/Builder/BaseQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ private static object AddIndexerNullCheck(IGMap<T> gMap, object query)
{
return BuildAlwaysFalseQuery(parameter);
}

if (value is DateTime dateTime)
{
if (mapper.Configuration.DefaultDateTimeKind.HasValue)
{
value = DateTime.SpecifyKind(dateTime, mapper.Configuration.DefaultDateTimeKind.Value);
}
}
}

// handle case-Insensitive search
Expand Down
7 changes: 7 additions & 0 deletions src/Gridify/GridifyGlobalConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public static class GridifyGlobalConfiguration
/// Default is false
/// </summary>
public static bool DisableNullChecks { get; set; } = false;

/// <summary>
/// By default, DateTimeKind.Unspecified is used.
/// You can change this behavior by setting this property to a DateTimeKind value.
/// Default is null
/// </summary>
public static DateTimeKind? DefaultDateTimeKind { get; set; } = null;
moxplod marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Specifies how field names are inferred from CLR property names.
Expand Down
7 changes: 7 additions & 0 deletions src/Gridify/GridifyMapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public record GridifyMapperConfiguration
/// Default is false
/// </summary>
public bool IgnoreNotMappedFields { get; set; } = GridifyGlobalConfiguration.IgnoreNotMappedFields;

/// <summary>
/// By default, DateTimeKind.Unspecified is used.
/// You can change this behavior by setting this property to a DateTimeKind value.
/// Default is null
/// </summary>
public DateTimeKind? DefaultDateTimeKind { get; set; } = GridifyGlobalConfiguration.DefaultDateTimeKind;

/// <summary>
/// Specifies how field names are inferred from CLR property names.
Expand Down
30 changes: 30 additions & 0 deletions test/EntityFrameworkPostgreSqlIntegrationTests/Issue188Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using EntityFrameworkIntegrationTests.cs;
using Microsoft.EntityFrameworkCore;
using Xunit;

namespace Gridify.Tests;

public class Issue188Tests
{

private readonly MyDbContext _dbContext = new();

[Fact]
public void DateTimeFilteringWithUTCKind_UsingGridifyMapper_ShouldNotThrowException()
{
var mapper = new GridifyMapper<User>(q => q.DefaultDateTimeKind = DateTimeKind.Utc)
.GenerateMappings();
_dbContext.Users.ApplyFiltering("CreateDate>2023-11-14", mapper).ToQueryString();
}

[Fact]
public void DateTimeFilteringWithUTCKind_UsingGlobalConfiguration_ShouldNotThrowException()
{
GridifyGlobalConfiguration.DefaultDateTimeKind = DateTimeKind.Utc;
_dbContext.Users.ApplyFiltering("CreateDate>2023-11-14").ToQueryString();
GridifyGlobalConfiguration.DefaultDateTimeKind = null;
}

}


Loading