Skip to content

Commit 12e0711

Browse files
committed
表内容变化记录(add/edit/remove)
1 parent f681fbf commit 12e0711

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

Diff for: Extensions/FreeSql.Extensions.AggregateRoot/AggregateRootRepository/AggregateRootUtils.cs

+36-18
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,15 @@ void LocalNavigateReader(Type entityType, object entity)
356356

357357
public static void MapEntityValue(string boundaryName, IFreeSql fsql, Type rootEntityType, object rootEntityFrom, object rootEntityTo)
358358
{
359+
var isDict = rootEntityTo?.GetType() == typeof(Dictionary<string, object>);
359360
Dictionary<Type, Dictionary<string, bool>> ignores = new Dictionary<Type, Dictionary<string, bool>>();
360361
LocalMapEntityValue(rootEntityType, rootEntityFrom, rootEntityTo, true);
361362
ignores.Clear();
362363

363364
void LocalMapEntityValue(Type entityType, object entityFrom, object entityTo, bool cascade)
364365
{
365366
if (entityFrom == null || entityTo == null) return;
366-
if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType();
367+
if (entityType == null) entityType = entityFrom.GetType();
367368
var table = fsql.CodeFirst.GetTableByEntity(entityType);
368369
if (table == null) return;
369370

@@ -377,7 +378,8 @@ void LocalMapEntityValue(Type entityType, object entityFrom, object entityTo, bo
377378
if (table.ColumnsByCsIgnore.ContainsKey(prop.Name)) continue;
378379
if (table.ColumnsByCs.ContainsKey(prop.Name))
379380
{
380-
table.SetPropertyValue(entityTo, prop.Name, table.GetPropertyValue(entityFrom, prop.Name));
381+
if (isDict) (entityTo as Dictionary<string, object>)[prop.Name] = table.GetPropertyValue(entityFrom, prop.Name);
382+
else table.SetPropertyValue(entityTo, prop.Name, table.GetPropertyValue(entityFrom, prop.Name));
381383
continue;
382384
}
383385
if (cascade == false) continue;
@@ -388,16 +390,18 @@ void LocalMapEntityValue(Type entityType, object entityFrom, object entityTo, bo
388390
var propvalFrom = EntityUtilExtensions.GetEntityValueWithPropertyName(fsql, entityType, entityFrom, prop.Name);
389391
if (propvalFrom == null)
390392
{
391-
EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, null);
393+
if (isDict) (entityTo as Dictionary<string, object>)[prop.Name] = null;
394+
else EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, null);
392395
continue;
393396
}
394397
switch (tbref.RefType)
395398
{
396399
case TableRefType.OneToOne:
397-
var propvalTo = tbref.RefEntityType.CreateInstanceGetDefaultValue();
400+
var propvalTo = isDict ? new Dictionary<string, object>() : tbref.RefEntityType.CreateInstanceGetDefaultValue();
398401
SetNavigateRelationshipValue(fsql, tbref, table.Type, entityFrom, propvalFrom);
399402
LocalMapEntityValue(tbref.RefEntityType, propvalFrom, propvalTo, boundaryAttr?.BreakThen != true);
400-
EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, propvalTo);
403+
if (isDict) (entityTo as Dictionary<string, object>)[prop.Name] = null;
404+
else EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, propvalTo);
401405
break;
402406
case TableRefType.OneToMany:
403407
SetNavigateRelationshipValue(fsql, tbref, table.Type, entityFrom, propvalFrom);
@@ -414,22 +418,36 @@ void LocalMapEntityValue(Type entityType, object entityFrom, object entityTo, bo
414418
}
415419
void LocalMapEntityValueCollection(Type entityType, object entityFrom, object entityTo, TableRef tbref, IEnumerable propvalFrom, PropertyInfo prop, bool cascade)
416420
{
417-
var propvalTo = typeof(List<>).MakeGenericType(tbref.RefEntityType).CreateInstanceGetDefaultValue();
418-
var propvalToIList = propvalTo as IList;
419-
foreach (var fromItem in propvalFrom)
421+
if (isDict)
420422
{
421-
var toItem = tbref.RefEntityType.CreateInstanceGetDefaultValue();
422-
LocalMapEntityValue(tbref.RefEntityType, fromItem, toItem, cascade);
423-
propvalToIList.Add(toItem);
423+
var propvalTo = new List<Dictionary<string,object>>();
424+
foreach (var fromItem in propvalFrom)
425+
{
426+
var toItem = new Dictionary<string, object>();
427+
LocalMapEntityValue(tbref.RefEntityType, fromItem, toItem, cascade);
428+
propvalTo.Add(toItem);
429+
}
430+
(entityTo as Dictionary<string, object>)[prop.Name] = propvalTo;
424431
}
425-
var propvalType = prop.PropertyType.GetGenericTypeDefinition();
426-
if (propvalType == typeof(List<>) || propvalType == typeof(ICollection<>))
427-
EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, propvalTo);
428-
else if (propvalType == typeof(ObservableCollection<>))
432+
else
429433
{
430-
//var propvalTypeOcCtor = typeof(ObservableCollection<>).MakeGenericType(tbref.RefEntityType).GetConstructor(new[] { typeof(List<>).MakeGenericType(tbref.RefEntityType) });
431-
var propvalTypeOc = Activator.CreateInstance(typeof(ObservableCollection<>).MakeGenericType(tbref.RefEntityType), new object[] { propvalTo });
432-
EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, propvalTypeOc);
434+
var propvalTo = typeof(List<>).MakeGenericType(tbref.RefEntityType).CreateInstanceGetDefaultValue();
435+
var propvalToIList = propvalTo as IList;
436+
foreach (var fromItem in propvalFrom)
437+
{
438+
var toItem = tbref.RefEntityType.CreateInstanceGetDefaultValue();
439+
LocalMapEntityValue(tbref.RefEntityType, fromItem, toItem, cascade);
440+
propvalToIList.Add(toItem);
441+
}
442+
var propvalType = prop.PropertyType.GetGenericTypeDefinition();
443+
if (propvalType == typeof(List<>) || propvalType == typeof(ICollection<>))
444+
EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, propvalTo);
445+
else if (propvalType == typeof(ObservableCollection<>))
446+
{
447+
//var propvalTypeOcCtor = typeof(ObservableCollection<>).MakeGenericType(tbref.RefEntityType).GetConstructor(new[] { typeof(List<>).MakeGenericType(tbref.RefEntityType) });
448+
var propvalTypeOc = Activator.CreateInstance(typeof(ObservableCollection<>).MakeGenericType(tbref.RefEntityType), new object[] { propvalTo });
449+
EntityUtilExtensions.SetEntityValueWithPropertyName(fsql, entityType, entityTo, prop.Name, propvalTypeOc);
450+
}
433451
}
434452
}
435453
}

0 commit comments

Comments
 (0)