Skip to content

Commit

Permalink
Changes after self-code review of the PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcelhanon committed Nov 20, 2024
1 parent 61eccbc commit 5631d98
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Execute(TContext context, TResult result)
{
// Map the persistent models to resources
IList<TResourceModel> resources = new List<TResourceModel>();
context.PersistentModels.MapResultItemCollectionTo(resources);
context.PersistentModels.MapCollectionTo(resources);

result.Resources = resources;
}
Expand Down
72 changes: 0 additions & 72 deletions Application/EdFi.Ods.Common/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,77 +161,5 @@ Type GetItemType()
return type;
}
}

// TODO: ODS-6551 - Revisit names of these methods
public static void MapResultItemCollectionTo<TSource, TTarget>(
this ICollection<TSource> sourceResultItemsList,
ICollection<TTarget> targetList,
bool itemCreatable = true,
object parent = null,
Func<TSource, bool> isItemIncluded = null)
where TSource : IMappable
{
if (sourceResultItemsList == null)
{
return;
}

if (targetList == null)
{
return;
}

var targetListType = targetList.GetType();
var itemType = GetItemType();

foreach (var sourceItem in sourceResultItemsList.Where(e => isItemIncluded == null || isItemIncluded(e)))
{
if (!itemCreatable)
{
// Use context provider to note the potential Data Policy Exception here (which applies only if the resource
// is being created, otherwise the SynchronizeCollectionTo method to the existing entity will handle any
// data policy violations).
if (GeneratedArtifactStaticDependencies.DataPolicyExceptionContextProvider.Get() == null)
{
string profileName = GeneratedArtifactStaticDependencies.ProfileContentTypeContextProvider.Get().ProfileName;
GeneratedArtifactStaticDependencies.DataPolicyExceptionContextProvider.Set(new DataPolicyException(profileName, itemType.Name));
}
}

// Create and map the item
var mappedTargetItem = (TTarget) Activator.CreateInstance(itemType);

if (parent != null)
{
(mappedTargetItem as IChildEntity)?.SetParent(parent);
}

sourceItem.Map(mappedTargetItem);
targetList.Add(mappedTargetItem);
}

Type GetItemType()
{
if (_itemTypeByUnderlyingListType.TryGetValue(targetListType, out Type type))
{
return type;
}

var listTypes = targetListType.GetGenericArguments();

if (listTypes.Length == 0)
{
throw new ArgumentException(
$"Target list type of '{targetListType.FullName}' does not have any generic arguments.");
}

// Assumption: ItemType is last generic argument (most of the time this will be a List<T>,
// but it could be a CovariantIListAdapter<TBase, TDerived>. We want the last generic argument type.
type = listTypes[^1];
_itemTypeByUnderlyingListType[targetListType] = type;

return type;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static byte[] SerializeAndCompressAggregateData(AggregateRootWithComposit
// Serialize the object directly to a MemoryStream
using var memoryStream = new MemoryStream();

// Write the LastModifiedDate value at the head of the stream so we can detect if the JSON has changed and is invalid without deserializing it
// Write the LastModifiedDate value at the head of the stream so we can detect if the table data has changed and
// the serialized entity is obsolete without deserializing it
byte[] lastModifiedDateBytes = BitConverter.GetBytes(entity.LastModifiedDate.ToBinary());
memoryStream.Write(lastModifiedDateBytes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ await DeadlockPolicyHelper.RetryPolicy.ExecuteAsync(
async ctx =>
{
bool isRetry = false;

if (ctx.TryGetValue("Retries", out object retryCount))
{
isRetry = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,4 @@ protected QueryBuilder GetSingleItemQueryBuilder()
return idQueryBuilder.Clone();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ await DeadlockPolicyHelper.RetryPolicy.ExecuteAsync(
async ctx =>
{
bool isRetry = false;

if (ctx.TryGetValue("Retries", out object retryCount))
{
isRetry = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<TEntity> DeserializeAsync<TEntity>(IItemRawData itemRawData)
{
TEntity entity = default;

// TODO: ODS-6551 - Restore to fallback to NHibernate-based load once code is stable and known tests pass without suppressing deserialization failures
// TODO: ODS-6551 - Considering restoring this fallback to NHibernate-based load once code is stable and known tests pass without suppressing deserialization failures
// try
{
// Deserialize the entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public ICollection<T> Deserialize(ref MessagePackReader reader, MessagePackSeria

return persistentCollection;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1461,9 +1461,16 @@
"\r",
"pm.test(\"Should return one expected resouce\", () => {\r",
" pm.expect(responseItems.count()).to.equal(1);\r",
" pm.expect(responseItems[0].id).to.equals(pm.environment.get('known:highSchool_id'));\r",
" pm.expect(responseItems[0].schoolId).to.equals(pm.environment.get('supplied:schoolId'));\r",
"});"
"});\r",
"\r",
"pm.test(\"Item returned should have the correct id.\", () => {\r",
" pm.expect(responseItems[0].id).to.equals(pm.environment.get('known:highSchool_id'));\r",
"});\r",
"\r",
"pm.test(\"Item returned should have the correct schoolId.\", () => {\r",
" pm.expect(responseItems[0].schoolId).to.equals(pm.environment.get('supplied:schoolId'));\r",
"});\r",
""
],
"type": "text/javascript",
"packages": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ namespace {{AggregateNamespace}}
{{/OneToOnes}}
{{#RelocatedExtensionOneToOnes}}
[IgnoreMember]
// [Key({{#MessagePackIndexer}}{{NextKey}}{{/MessagePackIndexer}})]
// This property implementation exists to provide the mapper with reflection-based access to the target instance's .NET type (for creating new instances)
public {{AggregateNamespacePrefix}}{{OtherClassName}} {{OtherClassName}}
{
Expand Down

0 comments on commit 5631d98

Please sign in to comment.