You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow BCL date/time types to be used when the NodaTime plugin is in use #2439
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).
+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.
Reacted by Alexander Langer, David Roth, Yannick Beauchamp-H and Will Dean
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?
@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.
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?
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.
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.
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.
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
Activity
krisztiankocsis commentedon May 6, 2019
+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.
roji commentedon May 7, 2019
@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.
dorny commentedon Jun 24, 2019
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 FROMin 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 theTimestampto/fromDateTimeand then uses instance ofNpgsqlSimpleTypeHandler<DateTime>to actually read/write it. With current API I had to use ugly hack (reflection) to get instance ofNpgsqlSimpleTypeHandler<DateTime>since the implementation class as well as theNpgsqlTypeHandlerFactory<>.Create(..)method are internal.Would be at least possible to make those classes or
NpgsqlTypeHandlerFactory<>.Create(..)method public?Is there any better solution?
YohDeadfall commentedon Jun 24, 2019
@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.
roji commentedon Jun 26, 2019
@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.
roji commentedon Aug 14, 2019
See also #2568
willdean commentedon Mar 14, 2020
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?
roji commentedon Mar 14, 2020
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?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.
willdean commentedon Mar 14, 2020
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.
roji commentedon Mar 14, 2020
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.
YohDeadfall commentedon Mar 15, 2020
@roji, do you mean 5.0? If so, this is what a new design of type handlers solves too, you know it.
25 remaining items
Add DateTime support to nodatimes TimestampTzHandler
Add DateTime support to nodatimes TimestampTzHandler
[-]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[/+]