Skip to content

Commit

Permalink
Default DateTimeKind configuration (#195)
Browse files Browse the repository at this point in the history
* Default DateTimeKind configuration

* moving usage to mapper config

* mapper doc update

* test: add DateTime Kind tests

---------

Co-authored-by: AliReZa Sabouri <[email protected]>
  • Loading branch information
moxplod and alirezanet authored Aug 7, 2024
1 parent d47a0f0 commit abd8c16
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
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.

- 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;

/// <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;
}

}


0 comments on commit abd8c16

Please sign in to comment.