public static bool SynchronizeTo(this ISchool source, ISchool target) { bool isModified = false; var sourceSupport = source as ISchoolSynchronizationSourceSupport; // Back synch non-reference portion of PK (PK properties cannot be changed, therefore they can be omitted in the resource payload, but we need them for proper comparisons for persistence) if (source.SchoolName != target.SchoolName) { source.SchoolName = target.SchoolName; } // Copy non-PK properties if ((sourceSupport == null || sourceSupport.IsSchoolYearSupported) && target.SchoolYear != source.SchoolYear) { target.SchoolYear = source.SchoolYear; isModified = true; } // ---------------------------------- // Synch One-to-one relationships // ---------------------------------- // SchoolAddress if (sourceSupport == null || sourceSupport.IsSchoolAddressSupported) { if (source.SchoolAddress == null) { if (target.SchoolAddress != null) { target.SchoolAddress = null; isModified = true; } } else { if (target.SchoolAddress == null) { var itemType = target.GetType().GetProperty("SchoolAddress").PropertyType; var newItem = Activator.CreateInstance(itemType); target.SchoolAddress = (ISchoolAddress)newItem; } isModified |= source.SchoolAddress.Synchronize(target.SchoolAddress); } } // ------------------------------------------------------------- // Sync lists return(isModified); }
public static void MapTo(this ISchool source, ISchool target, Action <ISchool, ISchool> onMapped) { var sourceSynchSupport = source as ISchoolSynchronizationSourceSupport; var targetSynchSupport = target as ISchoolSynchronizationSourceSupport; // Copy resource Id target.Id = source.Id; // Copy contextual primary key values target.SchoolName = source.SchoolName; // Copy non-PK properties if (sourceSynchSupport.IsSchoolYearSupported) { target.SchoolYear = source.SchoolYear; } else { targetSynchSupport.IsSchoolYearSupported = false; } // Copy Aggregate Reference Data if (GeneratedArtifactStaticDependencies.AuthorizationContextProvider == null || GeneratedArtifactStaticDependencies.AuthorizationContextProvider.GetAction() == RequestActions.ReadActionUri) { target.SchoolYearTypeResourceId = source.SchoolYearTypeResourceId; target.SchoolYearTypeDiscriminator = source.SchoolYearTypeDiscriminator; } // ---------------------------------- // Map One-to-one relationships // ---------------------------------- // SchoolAddress (Source) if (sourceSynchSupport.IsSchoolAddressSupported) { var itemProperty = target.GetType().GetProperty("SchoolAddress"); if (itemProperty != null) { if (source.SchoolAddress == null) { target.SchoolAddress = null; } else { var itemType = itemProperty.PropertyType; object targetSchoolAddress = Activator.CreateInstance(itemType); (targetSchoolAddress as IChildEntity)?.SetParent(target); source.SchoolAddress.Map(targetSchoolAddress); // Update the target reference appropriately target.SchoolAddress = (ISchoolAddress)targetSchoolAddress; } } } else { targetSynchSupport.IsSchoolAddressSupported = false; } // ------------------------------------------------------------- // Map lists var eTagProvider = new ETagProvider(); // Convert value to ETag, if appropriate var entityWithETag = target as IHasETag; if (entityWithETag != null) { entityWithETag.ETag = eTagProvider.GetETag(source); } // Convert value to LastModifiedDate, if appropriate var dateVersionedEntity = target as IDateVersionedEntity; var etagSource = source as IHasETag; if (dateVersionedEntity != null && etagSource != null) { dateVersionedEntity.LastModifiedDate = eTagProvider.GetDateTime(etagSource.ETag); } }