Skip to content

Allow BCL date/time types to be used when the NodaTime plugin is in use #2439

Description

@roji

Since we've released the NodaTime plugin in 4.0, we occasionally receive complaints about not being able to use both the BCL types and the NodaTime plugin in the same application. Marten specifically seems to be severely impacted by this (JasperFx/marten#1242), but there have been other requests (e.g. #2295, npgsql/efcore.pg#747).

One the write side things seem easy, but on the read side, there's one handler mapped to a given PostgreSQL type. We could have more than one handler mapped there (possibly by having NpgsqlTypeHandler reference the "next" handler as a linked list), but right now the handler API throws when it fails, so we'd have to catch that exception and move on to the next handler (which isn't really acceptable). We could modify the handler API to try writing (i.e. return a bool), but that's a non-trivial API change.

Interestingly we just ran into a very similar issue in #2435 (which we ended up not needing).

/cc @jeremydmiller @tetious @lngr

Activity

  1. added this to the 4.1 milestone on Apr 17, 2019
  2. krisztiankocsis commented on May 6, 2019

    @krisztiankocsis

    +1
    When inheriting IdentityDbContext, you implicitly have DateTimeOffset in the entitities, but just for this we don't want to use the BCL API and prefer having NodaTime. Currently, it makes almost impossible. Having two DbContext instances also not a great option.

  3. roji commented on May 7, 2019

    @roji
    MemberAuthor

    @krisztiankocsis that's a pretty good scenario. I can't really promise anything (other issues have higher priority) but I'll try to think about this.

  4. dorny commented on Jun 24, 2019

    @dorny

    I have another use case for this. Although not something which is commonly used.

    We have pretty complex data structure defined using protobuf (c# classes generated from .proto files).
    This model is 1:1 mapped to hierarchy of postgres custom types (arrays, enums) and written to DB using COPY FROM in binary mode.

    The issues is with Timestamp - protobuf uses Timestamp which is unknown to the npgsql driver.

    For now I've solved it using a "decorator" - I've implemented NpgsqlSimpleTypeHandler<Timestamp> which converts the Timestamp to/from DateTime and then uses instance of NpgsqlSimpleTypeHandler<DateTime> to actually read/write it. With current API I had to use ugly hack (reflection) to get instance of NpgsqlSimpleTypeHandler<DateTime> since the implementation class as well as the NpgsqlTypeHandlerFactory<>.Create(..) method are internal.

    Would be at least possible to make those classes or NpgsqlTypeHandlerFactory<>.Create(..) method public?

    Is there any better solution?

  5. YohDeadfall commented on Jun 24, 2019

    @YohDeadfall
    Contributor

    @dorny I have thoughts about making them public, but it will be in the next major update.

    As a workaround you can replicate the logic in your handler.

  6. roji commented on Jun 26, 2019

    @roji
    MemberAuthor

    @dorny note that making some specific handlers public has nothing really to do with this issue, which is about permitting registering more than one handler on a single PostgreSQL data type (for sending purposes).

    Can you please open another issue requesting this? @YohDeadfall we could do a pass to see what other type handlers should be exposed publicly. In the meantime I agree that copying the handler code into your project is a viable workaround.

  7. roji commented on Aug 14, 2019

    @roji
    MemberAuthor

    See also #2568

  8. modified the milestones: 4.1, 5.0 on Aug 30, 2019
  9. willdean commented on Mar 14, 2020

    @willdean

    We've just hit this problem and sadly given-up on using NodaTime directly with npgsql for the moment because of clashing with 3rd-party code that uses BCL types. Can I add a +1 for the approach in this issue as opposed to, say, npgsql/efcore.pg#747

    To me, it was a surprise that a type plug-in would replace existing types (given that the underlying stack is already great at many-to-many mapping between db types and framework types).

    To me, naively, the 'try' solution suggested by @roji above sounds most sensible: if the plugin can't find a conversion then let the core capability have a go. I understand this requires an architectural change, but is this high-architecture which needs very special attention, or is it a pile of tedious grunt-work chasing bools around the place which the community (i.e. me) could help with in some way?

  10. roji commented on Mar 14, 2020

    @roji
    MemberAuthor

    When I first worked on the plugin architecture, I really didn't anticipate that people would need to mix several date/time mappings within the same application... First, note that currently there isn't any distinction between "core capability" and plugins - and I don't think there should be: the same problem could arise when two plugins clash. Note also that in some places only one can win: when reading a PostgreSQL timestamp (without specified an expected CLR type via GetFieldValue), what should we be returning, the first-registered mapping? The last?

    I understand this requires an architectural change, but is this high-architecture which needs very special attention

    More like this. I wouldn't think this is a good change for an outsider to make who isn't quite familiar with Npgsql internals.

  11. willdean commented on Mar 14, 2020

    @willdean

    I wouldn't think this is a good change for an outsider to make who isn't quite familiar with Npgsql internals.

    I quite understand. I just preferred to be someone who offered to help, rather than merely nagged for features!

    As I think has emerged during the lifetime of the NodaTime plugin, it's actually very common in certain kinds of apps (e.g ASP.net) to need to mix these types, because of the mixed authorship of so much code.

    My ignorant mental model of how the plug-ins would work (perhaps it's useful to know how an outsider would perceive things?) is that in the "Other .NET types" column on https://www.npgsql.org/doc/types/basic.html, the NodaTime types would effectively appear as extra entries. So there wouldn't be any difference in default behaviour, but NodaTime types in entities would just magically work. It may well be that I have a very EF-centric view of the problem, though.

    Thanks for all your efforts anyway.

  12. roji commented on Mar 14, 2020

    @roji
    MemberAuthor

    No worries and thanks for the offer to help. This is definitely doable, it's mostly a question of priorization. I'll try my best to get this in for the next release.

  13. YohDeadfall commented on Mar 15, 2020

    @YohDeadfall
    Contributor

    @roji, do you mean 5.0? If so, this is what a new design of type handlers solves too, you know it.

  14. 25 remaining items

  15. modified the milestones: 4.1.5, Backlog on Sep 8, 2020
  16. self-assigned this
    on Sep 8, 2020
  17. changed the title [-]Look into allowing more than one type handler for a PostgreSQL type[/-] [+]Allow BCL date/time types to be used when the NodaTime plugin is in use[/+] on Jan 26, 2021
  18. modified the milestones: Backlog, 4.1.5 on Jan 26, 2021
  19. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions