|
@@ -71,20 +71,55 @@ namespace InABox.Core
|
|
|
|
|
|
public class EntityDuplicator<TEntity> : IEntityDuplicator where TEntity : Entity, IRemotable, IPersistent
|
|
|
{
|
|
|
- private readonly List<Tuple<Type, Type, Expression>> _relationships = new List<Tuple<Type, Type, Expression>>();
|
|
|
+ private interface IRelationship
|
|
|
+ {
|
|
|
+ Type ParentType { get; }
|
|
|
|
|
|
- public void Duplicate(IEnumerable<TEntity> entites) =>
|
|
|
- Duplicate(typeof(TEntity),
|
|
|
- new Filter<TEntity>(x => x.ID).InList(entites.Select(x => x.ID).ToArray()));
|
|
|
+ Type ChildType { get; }
|
|
|
|
|
|
- public void Duplicate(IFilter filter)
|
|
|
+ IFilter GetFilter(Entity parent);
|
|
|
+ }
|
|
|
+ private class EntityLinkRelationship<TParent, TChild> : IRelationship
|
|
|
{
|
|
|
- Duplicate(typeof(TEntity), filter);
|
|
|
+ public Type ParentType => typeof(TParent);
|
|
|
+
|
|
|
+ public Type ChildType => typeof(TChild);
|
|
|
+
|
|
|
+ public Column<TChild> Column { get; set; }
|
|
|
+
|
|
|
+ public IFilter GetFilter(Entity parent)
|
|
|
+ {
|
|
|
+ return new Filter<TChild>(Column).IsEqualTo(parent.ID);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ private class GenericRelationship<TParent, TChild> : IRelationship
|
|
|
+ where TParent : Entity
|
|
|
+ {
|
|
|
+ public Type ParentType => typeof(TParent);
|
|
|
+
|
|
|
+ public Type ChildType => typeof(TChild);
|
|
|
+
|
|
|
+ public Column<TChild> Column { get; set; }
|
|
|
+
|
|
|
+ public Func<TParent, object?> Func { get; set; }
|
|
|
+
|
|
|
+ public IFilter GetFilter(Entity parent)
|
|
|
+ {
|
|
|
+ return new Filter<TChild>(Column).IsEqualTo(Func(parent as TParent));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private readonly List<IRelationship> _relationships = new List<IRelationship>();
|
|
|
+
|
|
|
+ public void Duplicate(IEnumerable<TEntity> entites) =>
|
|
|
+ Duplicate(typeof(TEntity),
|
|
|
+ new Filter<TEntity>(x => x.ID).InList(entites.Select(x => x.ID).ToArray()));
|
|
|
+
|
|
|
private void Duplicate(Type parent, IFilter filter)
|
|
|
{
|
|
|
- var table = ClientFactory.CreateClient(parent).Query(filter);
|
|
|
+ var table = ClientFactory.CreateClient(parent)
|
|
|
+ .Query(filter, Columns.Create(parent).DefaultColumns(ColumnType.DataColumns));
|
|
|
foreach (var row in table.Rows)
|
|
|
{
|
|
|
var update = (row.ToObject(parent) as Entity)!;
|
|
@@ -92,21 +127,33 @@ namespace InABox.Core
|
|
|
update.ID = Guid.Empty;
|
|
|
update.CommitChanges();
|
|
|
ClientFactory.CreateClient(parent).Save(update, "Duplicated Record");
|
|
|
- foreach (var relationship in _relationships.Where(x => x.Item1 == parent))
|
|
|
+ foreach (var relationship in _relationships.Where(x => x.ParentType == parent))
|
|
|
{
|
|
|
- var filtertype = typeof(Filter<>).MakeGenericType(relationship.Item2);
|
|
|
- var newfilter = Activator.CreateInstance(filtertype) as IFilter;
|
|
|
- filter.Expression = relationship.Item3;
|
|
|
- filter.Operator = Operator.IsEqualTo;
|
|
|
- filter.Value = id;
|
|
|
- Duplicate(relationship.Item2, filter);
|
|
|
+ Duplicate(relationship.ChildType, relationship.GetFilter(update));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void AddChild<TParent, TChild>(Expression<Func<TChild, IEntityLink<TParent>>> childkey) where TParent : Entity where TChild : Entity
|
|
|
+ public void AddChild<TParent, TChild, TParentLink>(Expression<Func<TChild, TParentLink>> childkey)
|
|
|
+ where TParent : Entity, IRemotable, IPersistent
|
|
|
+ where TChild : Entity, IRemotable, IPersistent
|
|
|
+ where TParentLink : IEntityLink<TParent>
|
|
|
{
|
|
|
- _relationships.Add(new Tuple<Type, Type, Expression>(typeof(TParent), typeof(TChild), childkey));
|
|
|
+ _relationships.Add(new EntityLinkRelationship<TParent, TChild>
|
|
|
+ {
|
|
|
+ Column = new Column<TChild>(CoreUtils.GetFullPropertyName(childkey, ".") + ".ID")
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void AddChild<TParent, TChild>(Column<TChild> linkColumn, Func<TParent, object?> value)
|
|
|
+ where TParent : Entity, IRemotable, IPersistent
|
|
|
+ where TChild : Entity, IRemotable, IPersistent
|
|
|
+ {
|
|
|
+ _relationships.Add(new GenericRelationship<TParent, TChild>
|
|
|
+ {
|
|
|
+ Column = linkColumn,
|
|
|
+ Func = value
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
void IEntityDuplicator.Duplicate(IEnumerable<BaseObject> entities) => Duplicate(entities.Cast<TEntity>());
|