Exemple #1
0
        private static Type GetPossibleConventionType(ConventionMatch match)
        {
            if (Nullable.GetUnderlyingType(match.LinkedSourceModelProperty.PropertyType) != null)
            {
                if (match.LinkTargetProperty.PropertyType.IsLinkedSource())
                {
                    return(typeof(INestedLinkedSourceByNullableIdConvention));
                }

                return(typeof(IReferenceByNullableIdConvention));
            }

            if (IsGenericList(match.LinkTargetProperty))
            {
                var linkTargetListItemType = match.LinkTargetProperty.PropertyType.GetEnumerableItemType();
                if (linkTargetListItemType.IsLinkedSource())
                {
                    return(typeof(INestedLinkedSourceListConvention));
                }

                return(typeof(IReferenceListConvention));
            }

            if (match.LinkTargetProperty.PropertyType.IsLinkedSource())
            {
                return(typeof(INestedLinkedSourceConvention));
            }

            return(typeof(IReferenceConvention));
        }
        private void ApplyConvention(ConventionMatch match)
        {
            try
            {
                if (match.Convention is IReferenceConvention || match.Convention is INestedLinkedSourceConvention)
                {
                    ApplySingleValueConvention(match);
                }

                if (match.Convention is IReferenceListConvention || match.Convention is INestedLinkedSourceListConvention)
                {
                    ApplyListConvention(match);
                }

                if (match.Convention is IReferenceByNullableIdConvention || match.Convention is INestedLinkedSourceByNullableIdConvention)
                {
                    ApplyNullableIdConvention(match);
                }
            }
            catch (TargetInvocationException ex)
            {
                throw new LinkItException(
                          $"The convention \"{match.Convention.Name}\" failed for Apply. Link target id: {match.LinkTargetProperty.GetFriendlyName()}, linked source model property: {match.LinkedSourceModelProperty.GetFriendlyName()}",
                          ex.GetBaseException()
                          );
            }
        }
Exemple #3
0
 private void ApplyConvention(ConventionMatch match)
 {
     try
     {
         if (match.Convention is ISingleValueConvention)
         {
             ApplySingleValueConvention(match);
         }
         if (match.Convention is IMultiValueConvention)
         {
             ApplyMultiValueConvention(match);
         }
         if (match.Convention is IByNullableValueTypeIdConvention)
         {
             ApplyNullableValueTypeIdConvention(match);
         }
     }
     catch (TargetInvocationException ex)
     {
         throw new Exception(
                   $"The convention \"{match.Convention.Name}\" failed for Apply. Link target id: {match.LinkTargetProperty.GetFullName()}, linked source model property: {match.LinkedSourceModelProperty.Name}",
                   ex.InnerException
                   );
     }
 }
Exemple #4
0
        private void ApplySingleValueConvention(ConventionMatch match)
        {
            var method        = GetType().GetMethod(nameof(ApplySingleValueConventionGeneric));
            var genericMethod = method.MakeGenericMethod(
                match.LinkedSourceType,
                match.LinkTargetProperty.PropertyType,
                match.LinkedSourceModelProperty.PropertyType
                );

            genericMethod.Invoke(this, new object[] { match });
        }
Exemple #5
0
 private Type GetPossibleConventionType(ConventionMatch match)
 {
     if (Nullable.GetUnderlyingType(match.LinkedSourceModelProperty.PropertyType) != null)
     {
         return(typeof(IByNullableValueTypeIdConvention));
     }
     if (IsGenericList(match.LinkTargetProperty))
     {
         return(typeof(IMultiValueConvention));
     }
     return(typeof(ISingleValueConvention));
 }
        private void ApplySingleValueConvention(ConventionMatch match)
        {
            var genericParameters = new []
            {
                match.LinkedSourceType,
                match.LinkTargetProperty.PropertyType,
                match.LinkedSourceModelProperty.PropertyType
            };

            var conventionApplyMethod = match.Convention.GetType().MakeGenericMethod(
                nameof(IReferenceConvention.Apply), genericParameters
                );

            var callConventionGenericMethod = GetType().MakeGenericMethod(
                nameof(ApplySingleValueConventionGeneric), genericParameters
                );

            callConventionGenericMethod.Invoke(this, new object[] { match, conventionApplyMethod });
        }
Exemple #7
0
        private bool DoesConventionApply(ConventionMatch match)
        {
            var possibleConventionType = GetPossibleConventionType(match);

            if (!possibleConventionType.IsInstanceOfType(match.Convention))
            {
                return(false);
            }

            try
            {
                return(match.Convention.DoesApply(match.LinkedSourceModelProperty, match.LinkTargetProperty));
            }
            catch (Exception ex)
            {
                throw new Exception(
                          $"The convention \"{match.Convention.Name}\" failed for DoesApply. Link target id: {match.LinkTargetProperty.GetFullName()}, linked source model property: {match.LinkedSourceModelProperty.Name}",
                          ex
                          );
            }
        }
        public void ApplySingleValueConventionGeneric <TLinkedSource, TLinkTargetProperty, TLinkedSourceModelProperty>(ConventionMatch match, MethodInfo applyMethod)
            where TLinkedSource : ILinkedSource
        {
            var getLinkTargetProperty = FuncGenerator.GenerateFromGetterAsExpression <TLinkedSource, TLinkTargetProperty>(
                match.LinkTargetProperty.Name
                );
            var getLinkedSourceModelProperty = FuncGenerator
                                               .GenerateFromGetter <TLinkedSource, TLinkedSourceModelProperty>(
                $"Model.{match.LinkedSourceModelProperty.Name}"
                );

            var parameters = new object[]
            {
                _loadLinkProtocolBuilder.For <TLinkedSource>(),
                getLinkedSourceModelProperty,
                getLinkTargetProperty,
                match.LinkedSourceModelProperty,
                match.LinkTargetProperty
            };

            applyMethod.Invoke(match.Convention, parameters);
        }
Exemple #9
0
        public void ApplySingleValueConventionGeneric <TLinkedSource, TLinkTargetProperty, TLinkedSourceModelProperty>(ConventionMatch match)
        {
            var getLinkTargetProperty = FuncGenerator.GenerateFromGetterAsExpression <TLinkedSource, TLinkTargetProperty>(
                match.LinkTargetProperty.Name
                );
            var getLinkedSourceModelProperty = FuncGenerator
                                               .GenerateFromGetter <TLinkedSource, TLinkedSourceModelProperty>(
                $"Model.{match.LinkedSourceModelProperty.Name}"
                );

            var casted = (ISingleValueConvention)match.Convention;

            casted.Apply(
                _loadLinkProtocolBuilder.For <TLinkedSource>(),
                getLinkedSourceModelProperty,
                getLinkTargetProperty,
                match.LinkedSourceModelProperty, match.LinkTargetProperty);
        }