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.
The economic aspect only works if you trace off the pattern size you need and save the original pattern for future use. Patterns for children are much more economical if you can buy multi-sized patterns and use the larger sizes in the future as the child grows. A multi-sized pattern is much easier to make minor sizing alterations.
One of the reasons we use the repository pattern is to encapsulate fat queries. These queries make it hard to read, understand and test actions in ASP.NET MVC controllers. Also, as your application grows, the chances of you repeating a fat query in multiple places increases.
‘Instead, you should have a separate repository per domain class, like OrderRepository, ShippingRepository and ProductRepository.’ Do you mean that the repository could be the same as the controller name as prefix?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With