|
@@ -2,6 +2,7 @@
|
|
|
using System.Linq;
|
|
|
using System.Linq.Expressions;
|
|
|
using System.Reflection;
|
|
|
+using FluentResults;
|
|
|
|
|
|
namespace InABox.Core
|
|
|
{
|
|
@@ -22,11 +23,17 @@ namespace InABox.Core
|
|
|
}
|
|
|
|
|
|
public class LinkedProperty<TLinkedEntity, TEntityLink, TType> : ILinkedProperty
|
|
|
+ where TLinkedEntity : class
|
|
|
+ where TEntityLink : class
|
|
|
{
|
|
|
public Type Type => typeof(TLinkedEntity);
|
|
|
public String Path { get; }
|
|
|
+
|
|
|
+ private Func<TEntityLink?, TType> _source;
|
|
|
public String Source { get; }
|
|
|
public String Target { get; }
|
|
|
+
|
|
|
+ private readonly Func<TLinkedEntity?, TType, Result<TType>>? _func;
|
|
|
|
|
|
public override string ToString()
|
|
|
{
|
|
@@ -34,19 +41,30 @@ namespace InABox.Core
|
|
|
}
|
|
|
|
|
|
public LinkedProperty(Expression<Func<TLinkedEntity, TEntityLink>> path,
|
|
|
- Expression<Func<TEntityLink, TType>> source,
|
|
|
- Expression<Func<TLinkedEntity, TType>> target)
|
|
|
+ Expression<Func<TEntityLink?, TType>> source,
|
|
|
+ Expression<Func<TLinkedEntity?, TType>> target,
|
|
|
+ Func<TLinkedEntity?,TType, Result<TType>>? func = null)
|
|
|
{
|
|
|
Path = CoreUtils.GetFullPropertyName(path, ".");
|
|
|
Source = CoreUtils.GetFullPropertyName(source, ".");
|
|
|
Target = CoreUtils.GetFullPropertyName(target,".");
|
|
|
+ _func = func;
|
|
|
+ _source = source.Compile();
|
|
|
}
|
|
|
|
|
|
public void Update(object? from, object? to)
|
|
|
{
|
|
|
if (from != null && to != null)
|
|
|
{
|
|
|
- var value = CoreUtils.GetPropertyValue(from, Source);
|
|
|
+ var value = _source.Invoke(from as TEntityLink);
|
|
|
+ //var value = CoreUtils.GetPropertyValue(from, Source);
|
|
|
+ if (_func != null)
|
|
|
+ {
|
|
|
+ var result = _func(to as TLinkedEntity, value);
|
|
|
+ if (!result.IsSuccess)
|
|
|
+ return;
|
|
|
+ value = result.Value;
|
|
|
+ }
|
|
|
CoreUtils.SetPropertyValue(to, Target, value);
|
|
|
}
|
|
|
}
|