using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Artemis.Storage.Entities.Profile.DataBindings;
namespace Artemis.Core
{
///
public class DataBindingRegistration : IDataBindingRegistration
{
internal DataBindingRegistration(LayerProperty layerProperty,
DataBindingConverter converter,
Expression> propertyExpression)
{
LayerProperty = layerProperty ?? throw new ArgumentNullException(nameof(layerProperty));
Converter = converter ?? throw new ArgumentNullException(nameof(converter));
PropertyExpression = propertyExpression ?? throw new ArgumentNullException(nameof(propertyExpression));
if (propertyExpression.Body is MemberExpression memberExpression)
Member = memberExpression.Member;
}
///
/// Gets the layer property this registration was made on
///
public LayerProperty LayerProperty { get; }
///
/// Gets the converter that's used by the data binding
///
public DataBindingConverter Converter { get; }
///
/// Gets the expression that that accesses the property
///
public Expression> PropertyExpression { get; }
///
/// Gets the member the targets
/// null if the is not a member expression
///
public MemberInfo Member { get; }
///
/// Gets the data binding created using this registration
///
public DataBinding DataBinding { get; internal set; }
///
public IDataBinding GetDataBinding()
{
return DataBinding;
}
///
public IDataBinding CreateDataBinding()
{
if (DataBinding != null)
return DataBinding;
DataBindingEntity dataBinding = LayerProperty.Entity.DataBindingEntities.FirstOrDefault(e => e.TargetExpression == PropertyExpression.ToString());
if (dataBinding == null)
return null;
DataBinding = new DataBinding(LayerProperty, dataBinding);
return DataBinding;
}
}
}