AggregateRootRepository

This commit is contained in:
2881099 2022-09-02 17:08:36 +08:00
parent ab9db8270d
commit 8162e21ce5
3 changed files with 85 additions and 57 deletions

View File

@ -36,7 +36,9 @@ namespace FreeSql
var repos = new Dictionary<Type, object>(); var repos = new Dictionary<Type, object>();
try try
{ {
return InsertAggregateRootStatic(_repository, GetChildRepository, entitys); var ret = InsertAggregateRootStatic(_repository, GetChildRepository, entitys);
Attach(ret);
return ret;
} }
finally finally
{ {

View File

@ -12,13 +12,36 @@ using System.Reflection;
static class AggregateRootUtils static class AggregateRootUtils
{ {
public static void CompareEntityValue(IFreeSql fsql, Type entityType, object entityBefore, object entityAfter, string navigatePropertyName, public static void CompareEntityValue(IFreeSql fsql, Type rootEntityType, object rootEntityBefore, object rootEntityAfter, string rootNavigatePropertyName,
List<NativeTuple<Type, object>> insertLog, List<NativeTuple<Type, object>> insertLog,
List<NativeTuple<Type, object, object, List<string>>> updateLog, List<NativeTuple<Type, object, object, List<string>>> updateLog,
List<NativeTuple<Type, object>> deleteLog) List<NativeTuple<Type, object>> deleteLog)
{
Dictionary<Type, Dictionary<string, bool>> ignores = new Dictionary<Type, Dictionary<string, bool>>();
LocalCompareEntityValue(rootEntityType, rootEntityBefore, rootEntityAfter, rootNavigatePropertyName);
ignores.Clear();
void LocalCompareEntityValue(Type entityType, object entityBefore, object entityAfter, string navigatePropertyName)
{ {
if (entityType == null) entityType = entityBefore?.GetType() ?? entityAfter?.GetType(); if (entityType == null) entityType = entityBefore?.GetType() ?? entityAfter?.GetType();
if (entityBefore != null)
{
var stateKey = $":before:// {fsql.GetEntityKeyString(entityType, entityBefore, false)}";
if (ignores.TryGetValue(entityType, out var stateKeys) == false) ignores.Add(entityType, stateKeys = new Dictionary<string, bool>());
if (stateKeys.ContainsKey(stateKey)) return;
stateKeys.Add(stateKey, true);
}
if (entityAfter != null)
{
var stateKey = $":after:// {fsql.GetEntityKeyString(entityType, entityAfter, false)}";
if (ignores.TryGetValue(entityType, out var stateKeys) == false) ignores.Add(entityType, stateKeys = new Dictionary<string, bool>());
if (stateKeys.ContainsKey(stateKey)) return;
stateKeys.Add(stateKey, true);
}
var table = fsql.CodeFirst.GetTableByEntity(entityType); var table = fsql.CodeFirst.GetTableByEntity(entityType);
if (table == null) return;
if (entityBefore == null && entityAfter == null) return; if (entityBefore == null && entityAfter == null) return;
if (entityBefore == null && entityAfter != null) if (entityBefore == null && entityAfter != null)
{ {
@ -42,8 +65,8 @@ static class AggregateRootUtils
{ {
if (col.Attribute.IsVersion) continue; if (col.Attribute.IsVersion) continue;
var propvalBefore = table.GetPropertyValue(entityBefore, col.CsName); var propvalBefore = table.GetPropertyValue(entityBefore, col.CsName);
var propvalAfter = table.GetPropertyValue(entityBefore, col.CsName); var propvalAfter = table.GetPropertyValue(entityAfter, col.CsName);
if (propvalBefore != propvalAfter) changes.Add(col.CsName); if (object.Equals(propvalBefore, propvalAfter) == false) changes.Add(col.CsName);
continue; continue;
} }
} }
@ -56,11 +79,11 @@ static class AggregateRootUtils
if (tbref == null) continue; if (tbref == null) continue;
if (navigatePropertyName != null && prop.Name != navigatePropertyName) continue; if (navigatePropertyName != null && prop.Name != navigatePropertyName) continue;
var propvalBefore = table.GetPropertyValue(entityBefore, prop.Name); var propvalBefore = table.GetPropertyValue(entityBefore, prop.Name);
var propvalAfter = table.GetPropertyValue(entityBefore, prop.Name); var propvalAfter = table.GetPropertyValue(entityAfter, prop.Name);
switch (tbref.RefType) switch (tbref.RefType)
{ {
case TableRefType.OneToOne: case TableRefType.OneToOne:
CompareEntityValue(fsql, tbref.RefEntityType, propvalBefore, propvalAfter, null, insertLog, updateLog, deleteLog); LocalCompareEntityValue(tbref.RefEntityType, propvalBefore, propvalAfter, null);
break; break;
case TableRefType.OneToMany: case TableRefType.OneToMany:
LocalCompareEntityValueCollection(tbref, propvalBefore as IEnumerable, propvalAfter as IEnumerable); LocalCompareEntityValueCollection(tbref, propvalBefore as IEnumerable, propvalAfter as IEnumerable);
@ -75,7 +98,7 @@ static class AggregateRootUtils
break; break;
} }
} }
}
void LocalCompareEntityValueCollection(TableRef tbref, IEnumerable collectionBefore, IEnumerable collectionAfter) void LocalCompareEntityValueCollection(TableRef tbref, IEnumerable collectionBefore, IEnumerable collectionAfter)
{ {
var elementType = tbref.RefType == TableRefType.ManyToMany ? tbref.RefMiddleEntityType : tbref.RefEntityType; var elementType = tbref.RefType == TableRefType.ManyToMany ? tbref.RefMiddleEntityType : tbref.RefEntityType;
@ -133,7 +156,7 @@ static class AggregateRootUtils
} }
} }
foreach (var key in dictBefore.Keys) foreach (var key in dictBefore.Keys)
CompareEntityValue(fsql, elementType, dictBefore[key], dictAfter[key], null, insertLog, updateLog, deleteLog); LocalCompareEntityValue(elementType, dictBefore[key], dictAfter[key], null);
} }
} }

View File

@ -78,6 +78,9 @@ namespace FreeSql.Tests.DbContext2
Assert.NotNull(users[1].Ext); Assert.NotNull(users[1].Ext);
Assert.Equal(3, users[1].Ext.UserId); Assert.Equal(3, users[1].Ext.UserId);
Assert.Equal("admin03_remark", users[1].Ext.Remark); Assert.Equal("admin03_remark", users[1].Ext.Remark);
user.Ext.Remark = "admin01_remark changed01";
repo.Update(user);
} }
} }
class User class User