Knowing what the field key names are for each address type makes it easy to flatten the address data structure into a Dictionary, Map, or key-value pairs. Then, it becomes easy to access each address field value and get or set the address information.
Solution 1: key-value pairs
Ideally, consumers of SuperOffice CRM Web Service APIs could work with address information in a more intuitive way. One such possibility would be to work with key-value pairs, where field key names are the key part. Here is an example:Solution 2: AddressFieldExtensions
Since web service entities are POCO objects, they don’t contain methods. Below, however, I have created two extension methods to get and set the address based on a genericDictionary<string, string>. With this structure, it becomes easy to both read and write address fields, and persist those changes back to SuperOffice CRM.
AddressConstants
The first thing is to define a set of constants that represent eachAddressType, as well as each address field name:
AddressFieldExtensions
Next are the extension methods that simplify how to get and set address information as key-value pairs.Change address format
Changing address formats are easy, and accomplished by changing the entity country ID. Before changing thecountryId, make sure to first get the original address information to preserve the existing data, then change the country ID of the entity and save it.
Next, the returned entity will now contain the new address format and you can again get this as a Dictionary and merge it with the previous one.
Finally, change any of the address information and persist the changes.