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 many optional/required subclasses 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 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 as opposed to creation/instantiation 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.

  • Have a look at graphql. – Robert Harvey Aug 1 '18 at 18:55
  • How are these being stored? Does each repository method call represent a stored procedure or some kind of EF update? Do you have the freedom to change the methods or do you still need to call each one of them separately somehow? – John Wu Aug 3 '18 at 0:17
  • @JohnWu Yes, each Repo method calls a simple insert stored procedure. I do have the freedom to change such methods, so if you have an idea that goes a different way then feel free to let me know! – Hoser Aug 3 '18 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.