/// <summary> /// Defines the mapping for the specified <paramref name="member" />. /// </summary> /// <typeparam name="TDocumentType">The type of the document type.</typeparam> /// <typeparam name="TPublishedElement">The type of the published element.</typeparam> /// <param name="mapping">The mapping.</param> /// <param name="member">The member to map.</param> /// <param name="implementation">The mapping implementation.</param> /// <returns> /// The current mapping. /// </returns> /// <exception cref="System.ArgumentException">Member should be a method expression like: i => i.Method.</exception> public static MappingExpression <TDocumentType, TPublishedElement> ForMethodCall <TDocumentType, TPublishedElement>(MappingExpression <TDocumentType, TPublishedElement> mapping, LambdaExpression member, Delegate implementation) where TPublishedElement : IPublishedElement { if (!(member.Body is UnaryExpression expression) || !(expression.Operand is MethodCallExpression call) || !(call.Object is ConstantExpression constantExpression) || !(constantExpression.Value is MethodInfo methodInfo)) { throw new ArgumentException("Member should be a method expression like: i => i.Method"); } if (mapping.ModelMap.IsForAll && mapping.ModelMap.Type != methodInfo.DeclaringType) { throw new ArgumentException($"{methodInfo.Name} should be part of {mapping.ModelMap.Type.Name} (in for all mode)."); } mapping.ModelMap.Implementations.Add(methodInfo, implementation); return(mapping); }
/// <summary> /// Defines the mapping for the specified <paramref name="member" />. /// </summary> /// <typeparam name="TDocumentType">The type of the document type.</typeparam> /// <typeparam name="TPublishedElement">The type of the published element.</typeparam> /// <typeparam name="TMember">The type of the member.</typeparam> /// <param name="mapping">The mapping.</param> /// <param name="member">The member.</param> /// <param name="propertyName">Name of the property.</param> /// <returns> /// The current mapping. /// </returns> public static MappingExpression <TDocumentType, TPublishedElement> ForGridMember <TDocumentType, TPublishedElement, TMember>(this MappingExpression <TDocumentType, TPublishedElement> mapping, Expression <Func <TDocumentType, TMember> > member, string propertyName) where TPublishedElement : IPublishedElement { if (!MappingHtmlStringExtensions.IsDefined(typeof(TMember))) { throw new NotSupportedException($"{typeof(TMember).Name} is not defined as IHtmlString"); } return(mapping.ForMember(member, c => { var attempt = ViewRenderer.GetGridHtml(c.Value(propertyName)).TryConvertTo <TMember>(); return attempt.Success ? attempt.Result : default !;
/// <summary> /// Defines the mapping for the specified <paramref name="member" />. /// </summary> /// <typeparam name="TDocumentType">The type of the document type.</typeparam> /// <typeparam name="TPublishedElement">The type of the published element.</typeparam> /// <typeparam name="TMember">The type of the member.</typeparam> /// <typeparam name="T1">The type of the first parameter.</typeparam> /// <typeparam name="T2">The type of the second parameter.</typeparam> /// <typeparam name="T3">The type of the thrid parameter.</typeparam> /// <param name="mapping">The mapping.</param> /// <param name="member">The member to map.</param> /// <param name="implementation">The mapping implementation.</param> /// <returns> /// The current mapping. /// </returns> public static MappingExpression <TDocumentType, TPublishedElement> ForMember <TDocumentType, TPublishedElement, TMember, T1, T2, T3>(this MappingExpression <TDocumentType, TPublishedElement> mapping, Expression <Func <TDocumentType, Func <T1, T2, T3, TMember> > > member, Func <TPublishedElement, T1, T2, T3, TMember> implementation) where TPublishedElement : IPublishedElement => ForMethodCall(mapping, member, implementation);
/// <summary> /// Defines the mapping for the specified <paramref name="member" />. /// </summary> /// <typeparam name="TDocumentType">The type of the document type.</typeparam> /// <typeparam name="TPublishedElement">The type of the published element.</typeparam> /// <param name="mapping">The mapping.</param> /// <param name="member">The member.</param> /// <param name="propertyName">Name of the property.</param> /// <returns> /// The current mapping. /// </returns> public static MappingExpression <TDocumentType, TPublishedElement> ForGridMember <TDocumentType, TPublishedElement>(this MappingExpression <TDocumentType, TPublishedElement> mapping, Expression <Func <TDocumentType, string> > member, string propertyName) where TPublishedElement : IPublishedElement => mapping.ForMember(member, c => ViewRenderer.GetGridHtml(c.Value(propertyName)));