Reputation: 5044
I'm in the midst of a rather large project which involves decomposing a very old and large ColdFusion legacy application and creating some .NET services in it's place. Due to this, there are some requirements I'm not a huge fan of that it needs to support as we transition to this new backend. One of these requirements is a single endpoint that must take a very large JSON payload with hundreds of optional fields and save it all in one go.
I've broken down this data into a rather large domain object and various nested subclasses, and save each subclass in a transaction if it is included in the request.
The code looks something like this:
using (var transaction = await _transactionFactory.CreateDbTransactionScopeAsync(token))
{
//save basic patient info
newPatient = await _patientRepo.CreatePatientAsync(request, transaction, token);
//save patient medicare information
if (request.PatientMedicare != null)
newPatient.PatientMedicare = await _patientRepo.CreatePatientMedicareAsync(newPatient.Id, request.PatientMedicare, transaction, token);
//save patient flags
if (request.PatientFlags != null)
newPatient.PatientFlags = await CreatePatientFlagsAsync(newPatient.Id, request.PatientFlags, transaction, token);
//save patient code
if (request.PatientCode != null)
newPatient.PatientCode = await _patientRepo.CreatePatientCodeAsync(newPatient.Id, request.PatientCode, transaction, token);
//save patient facilities
if (request.PatientFacilities != null)
newPatient.PatientFacilities = await CreatePatientFacilitiesAsync(newPatient.Id, request.PatientFacilities, transaction, token);
... etc (this goes on for 15+ subclasses)
If I could avoid having to do this I would but until we are able to re-write more of this ColdFusion and front end code this is really the only option.
Is there a pattern or something that would make this a little cleaner? Something like a builder or factory pattern but that handles saving instead of creation of an object?
This is going to be a common issue I deal with with other domain objects a cleaner way to approach this would be awesome.
Upvotes: 7
Views: 117
Reputation: 3973
I'm not sure about the use cases you need to support but one way to do this would be to just save the JSON as is and work from there.
Especially with for instance the PostgreSQL JSON types this could work very nicely but even just storing it in a text field could be a viable option under some use cases and loads. It would also free your hands so you can move to the next step quickly.
Upvotes: 1