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
Track IsNew as property #509
Comments
I haven't put together a test project for this, but what if you just set Note also that while If neither of those meet your needs, then how would you design this new |
True, but this is an autoincrement field. I want new records to autoincrement unless I specify a value. In practice, this is a mobile app, and for the sync operations I am importing a lot of external data (insert an external value into the autoincrement id field) while for local editing I am creating genuinely new records.
definitely don't want to round trip to the db
the property would replace the method, so the method could just return the property. as I mentioned, SubSonic (PetaPoco's ancestor project) did this. i still have all the code (several projects still use SS2 and SS3). PetaPoco is more lightweight, so I'd have to check that it has the capability to track this. SS also had the ability to track 'dirty' columns, so it would just generate an update to columns that had been changed, rather than all of them. I don't want to keep banging on about SubSonic, but it was a great project and I only left it because it was too LINQ-y (a mistake IMO) and SS3 had some performance issues. PP inherited all its class templates from SS. I've hacked the templates (extensively) and the core code (somewhat) of PP based on a version from several years ago, and I'm just now coming back to the project source repo due to the T4 issues and NET Standard support. So I am keen to feed back features I see as useful. Just out of interest, I am also a core maintainer on the Slickgrid project. |
Okay, I see where you're going, but I have a couple of concerns. First, by redefining what I'm not opposed to what you're suggesting, just trying to suss out some details. How about a modification to |
An InsertWithPrimaryKey() function would get my vote, it's just so less ambiguous than inferring behaviour from POCO properties. If the property name doesn't explicitly advertise what it does it's bound to get misused and generate issue reports. |
Hmmm, OK I'll have a think. I hadn't considered these options. That said, creating a POCO with a PK without having retrieved it from the DB, or taken care to clear the table first, is surely an anti-pattern? But anyway, will consider the consequences. |
Isn't "creating a POCO with a PK without having retrieved it from the DB" exactly the use case you're trying to solve for? I thought I understood that you were creating a new object with a PK value; I think that a change is breaking if it alters established and non-buggy behavior. The current behavior of |
My apologies for barging in here rather suddenly. I am really busy at the moment, but the code I'm working on will cover this issue from every angle. I'll make what changes are necessary on my private repo and get back here with something more coherent later on. thanks ... |
The methods Save and IsNew are somewhat dumb. PP is lightweight and complexities, such as this, can be hard to solve easily. Save and IsNew, I think from memory, will only work when the DB assigns the values for the PK. The protected method 🤔 There is maybe an opportunity to allow hooking this via the fluent configuration.... |
I note and echo some concerns raised in #10 (it is from 2011 so I've opened this new issue!).
I tried inserting a PK into an autonumber-PK field, which is acceptable on many DBMS's.
However IsNew is coded to base the return on whether the PK is still at default value or not.
Save()
callsIsNew()
and, getting false, then callsUpdate()
, which fails silently because the record does not exist.Calling
Insert()
directly fails because it will not insert a value into an autonumber column.I propose creating an IsNew property on the poco column that will track the status (IsNew=false where loaded from the DB, new otherwise). This is how SubSonic worked.
Also perhaps a
CanInsertAutonum
property on the providers to allow this behaviour.Just checking this would be acceptable before I tool up a PR.
The text was updated successfully, but these errors were encountered: