mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
AggregateRootRepository
This commit is contained in:
parent
ab9db8270d
commit
8162e21ce5
@ -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
|
||||||
{
|
{
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user