I have a fetch method that may not find a matching record so data would not be set. But, the fetch automatically sets the return object as isnew = false, regardless of success or not. So, to solve this, I use MarkNew() in case no data has been found.
This mechanism seems to work, as if I put a breakpoint on the last } of the method, the isnew variable equals true.
BUT ... when processing returns to the caller code, isnew is false again, nullifying the whole operation and resulting in an update attempt that ofcourse will fail as it does not exist.
What do I need to do to make this work?
When CSLA is configured to use the DataPortal_Fetch method it also calls MarkOld() for you, hence IsNew == false in the UI. The DataPortal_XYZ methods are designed to reduce the amount of repetitive coding required but are less flexible.
Two solutions I can think of:
1) Use the ObjectFactory DataPortal model, which gives you full control of the data portal process but requires more coding.
2) In your factory method, first call an Exists command and then call either DataPortal.Fetch or DataPortal.Create; pseudo code:
private static OrderEdit GetOrder(long id)
That ObjectFactory looks promising, i'll look into it. The other option needs very redundant logic, which is not very performant.
Well I've looked into it with a little more detail and it seems I'm struggling to find a solution that does not mean a total refactoring of my Model.
The only version that I have that even recognizes the ObjectFactory attribute, is the CSLA 4.1 library, but that comes with so many changes, that the entire Model layer would need a serious overhaul. Not impossible, but a lot of work. And, since I don't know 4.1 at all, I am not inclined to use that right now.
2) Essentially this is a by-pass of core CSLA functionality, and if I am not going to use CSLA, I am going to not use it all, rather than do some mutant hybrid form. And either way, that would mean a total overhaul of the entire Model, again.
I hope there is something simple I am overlooking? Perhaps you can point me to a download link of a CSLA version that does support ObjectFactory without all those changes?
Object factory is available from Csla 3.6.0 and up so you will not have to move to Csla 4.1
There has been several discussions on what to do if DataPortal_Fetch does not find an object.
The recommended behaviour is to throw an Exception that no data was found (create your own serializable Exception object).
The client can then add exception handling code to handle that exception and rather call DataPortal.Create to return a new object.
I got an ObjectFactory test using the 3.8.3 version working, so that seems feasible (and by far the best solution). Thanks. I'll have to see what that means for the entire model.
Jonny is correct though.
The recommended solution is for a failed fetch to throw an exception. In your factory method you can catch this exception and return the result of a call to the create factory method - that would achieve your desired result without any fancy code or altering your model.
Copyright (c) Marimer LLC