mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
AggregateRootRepository
This commit is contained in:
@ -57,11 +57,16 @@ namespace FreeSql
|
||||
public Type EntityType => _repository.EntityType;
|
||||
public IDataFilter<TEntity> DataFilter => _repository.DataFilter;
|
||||
|
||||
public void Attach(TEntity entity) => AttachAggregateRoot(entity);
|
||||
public void Attach(TEntity entity)
|
||||
{
|
||||
var state = CreateEntityState(entity);
|
||||
if (_states.ContainsKey(state.Key)) _states[state.Key] = state;
|
||||
else _states.Add(state.Key, state);
|
||||
}
|
||||
public void Attach(IEnumerable<TEntity> entity)
|
||||
{
|
||||
foreach (var item in entity)
|
||||
AttachAggregateRoot(item);
|
||||
Attach(item);
|
||||
}
|
||||
public IBaseRepository<TEntity> AttachOnlyPrimary(TEntity data) => _repository.AttachOnlyPrimary(data);
|
||||
public Dictionary<string, object[]> CompareState(TEntity newdata) => _repository.CompareState(newdata);
|
||||
@ -130,17 +135,11 @@ namespace FreeSql
|
||||
if (string.IsNullOrEmpty(key)) return null;
|
||||
return _states.ContainsKey(key);
|
||||
}
|
||||
void AttachAggregateRoot(TEntity entity)
|
||||
{
|
||||
var state = CreateEntityState(entity);
|
||||
if (_states.ContainsKey(state.Key)) _states[state.Key] = state;
|
||||
else _states.Add(state.Key, state);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Selectoriginal
|
||||
public virtual ISelect<TEntity> Select => SelectAggregateRoot;
|
||||
ISelect<TEntity> SelectAggregateRoot
|
||||
protected ISelect<TEntity> SelectAggregateRoot
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -149,6 +148,33 @@ namespace FreeSql
|
||||
return query;
|
||||
}
|
||||
}
|
||||
protected void SelectAggregateRootTracking(object list)
|
||||
{
|
||||
if (list == null) return;
|
||||
var ls = list as IEnumerable<TEntity>;
|
||||
if (ls == null)
|
||||
{
|
||||
var ie = list as IEnumerable;
|
||||
if (ie == null) return;
|
||||
var isfirst = true;
|
||||
foreach (var item in ie)
|
||||
{
|
||||
if (item == null) continue;
|
||||
if (isfirst)
|
||||
{
|
||||
isfirst = false;
|
||||
var itemType = item.GetType();
|
||||
if (itemType == typeof(object)) return;
|
||||
if (itemType.FullName.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType;
|
||||
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
|
||||
if (itemType.GetConstructor(Type.EmptyTypes) == null) return;
|
||||
}
|
||||
if (item is TEntity item2) Attach(item2);
|
||||
else return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
void SelectAggregateRootNavigateReader<T1>(ISelect<T1> currentQuery, Type entityType, string navigatePath, Stack<Type> ignores)
|
||||
{
|
||||
if (ignores.Any(a => a == entityType)) return;
|
||||
@ -182,32 +208,6 @@ namespace FreeSql
|
||||
}
|
||||
ignores.Pop();
|
||||
}
|
||||
void SelectAggregateRootTracking(object list)
|
||||
{
|
||||
if (list == null) return;
|
||||
var ls = list as IEnumerable<TEntity>;
|
||||
if (ls == null)
|
||||
{
|
||||
var ie = list as IEnumerable;
|
||||
if (ie == null) return;
|
||||
var isfirst = true;
|
||||
foreach (var item in ie)
|
||||
{
|
||||
if (item == null) continue;
|
||||
if (isfirst)
|
||||
{
|
||||
isfirst = false;
|
||||
var itemType = item.GetType();
|
||||
if (itemType == typeof(object)) return;
|
||||
if (itemType.FullName.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType;
|
||||
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
|
||||
if (itemType.GetConstructor(Type.EmptyTypes) == null) return;
|
||||
}
|
||||
AttachAggregateRoot(item as TEntity);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
@ -71,10 +71,15 @@ namespace FreeSql
|
||||
}
|
||||
List<T1> LocalInsertAggregateRoot<T1>(IBaseRepository<T1> repository, IEnumerable<T1> entitys) where T1 : class
|
||||
{
|
||||
var table = repository.Orm.CodeFirst.GetTableByEntity(repository.EntityType);
|
||||
if (table.Primarys.Any(col => col.Attribute.IsIdentity))
|
||||
{
|
||||
foreach (var entity in entitys)
|
||||
repository.Orm.ClearEntityPrimaryValueWithIdentity(repository.EntityType, entity);
|
||||
}
|
||||
var ret = repository.Insert(entitys);
|
||||
foreach (var entity in entitys) LocalCanAggregateRoot(repository.EntityType, entity, true);
|
||||
|
||||
var table = repository.Orm.CodeFirst.GetTableByEntity(repository.EntityType);
|
||||
foreach (var prop in table.Properties.Values)
|
||||
{
|
||||
var tbref = table.GetTableRef(prop.Name, false);
|
||||
@ -166,7 +171,7 @@ namespace FreeSql
|
||||
var stateKey = Orm.GetEntityKeyString(EntityType, entity, false);
|
||||
if (_states.TryGetValue(stateKey, out var state) == false) throw new Exception($"AggregateRootRepository 使用仓储对象查询后,才可以更新数据 {Orm.GetEntityString(EntityType, entity)}");
|
||||
AggregateRootUtils.CompareEntityValue(Orm, EntityType, state.Value, entity, null, insertLog, updateLog, deleteLog);
|
||||
AttachAggregateRoot(entity);
|
||||
Attach(entity);
|
||||
}
|
||||
return insertLog.Count + updateLog.Count + deleteLog.Count;
|
||||
}
|
||||
@ -193,7 +198,7 @@ namespace FreeSql
|
||||
var stateKey = Orm.GetEntityKeyString(EntityType, entity, false);
|
||||
if (_states.TryGetValue(stateKey, out var state) == false) throw new Exception($"AggregateRootRepository 使用仓储对象查询后,才可以保存数据 {Orm.GetEntityString(EntityType, entity)}");
|
||||
AggregateRootUtils.CompareEntityValue(Orm, EntityType, state.Value, entity, propertyName, insertLog, updateLog, deleteLog);
|
||||
AttachAggregateRoot(entity);
|
||||
Attach(entity);
|
||||
}
|
||||
|
||||
protected List<TEntity> _dataEditing;
|
||||
|
12
FreeSql.Repository/FreeSqlRepositoryExtensions.cs
Normal file
12
FreeSql.Repository/FreeSqlRepositoryExtensions.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
public static class FreeSqlRepositoryExtensions
|
||||
{
|
||||
public static IBaseRepository<TEntity> GetAggregateRootRepository<TEntity>(this IFreeSql that) where TEntity : class
|
||||
{
|
||||
return new AggregateRootRepository<TEntity>(that);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user