public static void SyncCollectionByKey <TDbContext, TSource, TSourceItem, TDestination, TDestinationItem, TKey> ( this IMemberConfigurationExpression <TSource, TDestination, ICollection <TDestinationItem> > config, IEntityFrameworkUnitOfWorkProvider <TDbContext> unitOfWorkProvider, Expression <Func <TSource, ICollection <TSourceItem> > > sourceCollectionSelector, Func <TSourceItem, TKey> sourceKeySelector, Func <TDestinationItem, TKey> destinationKeySelector, Func <TSourceItem, TDestinationItem> createFunction = null, Action <TSourceItem, TDestinationItem> updateFunction = null, Action <TDestinationItem> removeFunction = null, Func <TDestinationItem, bool> destinationFilter = null, bool keepRemovedItemsInDestinationCollection = true ) where TDbContext : DbContext where TDestinationItem : class { if (removeFunction == null) { removeFunction = d => { var uow = EntityFrameworkUnitOfWork.TryGetDbContext(unitOfWorkProvider); uow.Set <TDestinationItem>().Remove(d); }; } Extensions.SyncCollectionByKey(config, sourceCollectionSelector, sourceKeySelector, destinationKeySelector, createFunction, updateFunction, removeFunction, keepRemovedItemsInDestinationCollection, destinationFilter); }
public static void IgnoreSourceWhenDefault <TSource, TDestination>(this IMemberConfigurationExpression <TSource, TDestination, object> opt) { var destinationType = opt.DestinationMember.GetMemberType(); object defaultValue = destinationType.GetTypeInfo().IsValueType ? Activator.CreateInstance(destinationType) : null; opt.Condition((src, dest, srcValue) => !Equals(srcValue, defaultValue)); }
private void PropertyBuilder(IMemberConfigurationExpression <Product, ProductResponse, string> obj) { var url = "https://localhost:44306/content"; obj.MapFrom(s => s.PictureUrl); obj.AddTransform(x => string.IsNullOrEmpty(x) ? "No url found.." : $"{url}/{x}"); }
public static void MapFromField <TSource, TDestination, TMember>( this IMemberConfigurationExpression <TSource, TDestination, TMember> config, string fieldId) where TSource : ODataProductModel { config.MapFrom((model, destination, member, context) => model.GetValue <TMember>(fieldId, context.GetMappingCulture())); }
public static void DynamicMapFrom <TSource, TDestination, TMember>(this IMemberConfigurationExpression <TSource, TDestination, TMember> xExpression, Type xSelectedPropertyType, LambdaExpression xMapExpression) { MethodInfo method = xExpression.GetType().GetMethods().Where(m => m.Name == "MapFrom").FirstOrDefault(f => f.GetParameters().Any(a => a.Name.Equals("mapExpression"))); MethodInfo generic = method.MakeGenericMethod(xSelectedPropertyType); generic.Invoke(xExpression, new object[] { xMapExpression }); }
public MemberConfigurationEntityCacheKeyExpression( IMemberConfigurationExpression <TSource, TDestination, TMember> memberConfigurationExpression, Expression <Func <TSource, TKey> > sourceKeyMember) { _memberConfigurationExpression = memberConfigurationExpression; _sourceKeyMember = sourceKeyMember; }
public static void FromEntityCache <TSource, TDestination, TMember>( this IMemberConfigurationExpression <TSource, TDestination, TMember> memberConfigurationExpression, Action <MemberConfigurationEntityCacheKeyExpression <TSource, TDestination, TMember, TSource> > cacheOptions ) { FromEntityCache(memberConfigurationExpression, x => x, cacheOptions); }
private void DepartmentFamilies(IMemberConfigurationExpression <DepartmentEntity, DepartmentResource, CategoryFamily[]> opt) { opt.MapFrom((tr, a, b, map) => tr.CategoriesByFamily.Select(s => new CategoryFamily() { Name = s.Key, Categories = map.Mapper.Map <CategoryResource[]>(s.Value) })); }
public static void MapFromPrice <TSource, TDestination, TMember>( this IMemberConfigurationExpression <TSource, TDestination, TMember> config, string listId) where TSource : ODataProductModel { config.MapFrom(model => model.GetPrice(listId)); }
public static void SyncCollectionByKey <TDbContext, TSource, TSourceItem, TDestination, TDestinationItem> ( this IMemberConfigurationExpression <TSource, TDestination, ICollection <TDestinationItem> > config, IEntityFrameworkUnitOfWorkProvider <TDbContext> unitOfWorkProvider, Expression <Func <TSource, ICollection <TSourceItem> > > sourceCollectionSelector, Func <TSourceItem, TDestinationItem> createFunction = null, Action <TSourceItem, TDestinationItem> updateFunction = null, Action <TDestinationItem> removeFunction = null, Func <TDestinationItem, bool> destinationFilter = null, bool keepRemovedItemsInDestinationCollection = true ) where TDbContext : DbContext where TDestinationItem : class { if (removeFunction == null) { removeFunction = d => { var dbContext = EntityFrameworkUnitOfWork.TryGetDbContext(unitOfWorkProvider); dbContext.Set <TDestinationItem>().Remove(d); }; } var sourceKeyEntityType = typeof(TSourceItem).GetTypeInfo() .GetInterfaces() .Select(s => s.GetTypeInfo()) .SingleOrDefault(s => s.IsGenericType && s.GetGenericTypeDefinition() == typeof(IEntity <>)); var sourceKeyType = sourceKeyEntityType?.GetGenericArguments()[0]; var destinationKeyEntityType = typeof(TDestinationItem).GetTypeInfo() .GetInterfaces() .Select(s => s.GetTypeInfo()) .SingleOrDefault(s => s.IsGenericType && s.GetGenericTypeDefinition() == typeof(IEntity <>)); var destinationKeyType = destinationKeyEntityType?.GetGenericArguments()[0]; if (sourceKeyType == null || destinationKeyType == null || sourceKeyType != destinationKeyType) { throw new InvalidOperationException( "The source and destination collection items must implement the IEntity<TKey> interface and the keys must be equal!"); } var sourceParam = Expression.Parameter(typeof(TSourceItem), "i"); var sourceKeySelector = Expression.Lambda(Expression.Property(sourceParam, nameof(IEntity <int> .Id)), sourceParam); var destinationParam = Expression.Parameter(typeof(TDestinationItem), "i"); var destinationKeySelector = Expression.Lambda(Expression.Property(destinationParam, nameof(IEntity <int> .Id)), destinationParam); var method = typeof(Extensions).GetTypeInfo().GetMethod("SyncCollectionByKeyReflectionOnly", BindingFlags.NonPublic | BindingFlags.Static); method.MakeGenericMethod(typeof(TSource), typeof(TSourceItem), typeof(TDestination), typeof(TDestinationItem), sourceKeyType) .Invoke(null, new object[] { config, sourceCollectionSelector, sourceKeySelector, destinationKeySelector, createFunction, updateFunction, removeFunction, keepRemovedItemsInDestinationCollection, destinationFilter }); }
public static void ConvertToHtmlContent <TSource, TDestination>( this IMemberConfigurationExpression <TSource, TDestination, IHtmlContent> expression, Expression <Func <TSource, string> > sourceMember ) { ThrowIfExpressionIsNull(expression); expression.ConvertUsing <HtmlContentConverter, string>(sourceMember); }
internal NHibernateReferenceConfiguration( IMemberConfigurationExpression <TSrc> configurationExpression, Func <ISession> getSession, Func <TSrc, TMember> getSourceMember) { _configurationExpression = configurationExpression; _getSession = getSession; _getSourceMember = getSourceMember; }
public static void MapFromTextOptionLabel <TSource, TDestination, TMember>( this IMemberConfigurationExpression <TSource, TDestination, TMember> config, string fieldId, CultureInfo culture) where TSource : ODataProductModel { config.MapFromTextOptionLabel(fieldId, culture.ToString()); }
public static void IfSafeAgainst <T, TException>(this IMemberConfigurationExpression <T> options, Func <T, object> get) where TException : Exception { return(options.Condition(source => { try { var value = get(source); return true; } catch (TException) { return false; } })); }
public static void IfSafeAgainst <T, TException>(this IMemberConfigurationExpression <T> options, Func <T, object> get) where TException : Exception { return(options.Condition(source => { try { string value = source.PositionFolder; return true; } catch (StrongTypingException) { return false; } })); }
public static void MapFromField <TSource, TDestination, TMember>( this IMemberConfigurationExpression <TSource, TDestination, TMember> config, string fieldId, string culture) where TSource : ODataProductModel { config.MapFrom(model => model.GetValue <TMember>(fieldId, CultureInfo.GetCultureInfo(culture))); }
private static void ResolveImageModelFromBlogPost <T>(IMemberConfigurationExpression <BlogPost, T, ImageViewModel> exp) { exp.MapFrom((blogPost, viewModel, image) => blogPost.Image == null ? null : new ImageViewModel() { UriPath = blogPost.Image.UriPath }); }
public static void ConvertMediaPathToUri <TSource, TDestination>(this IMemberConfigurationExpression <TSource, TDestination, Uri> expression, Expression <Func <TSource, string> > sourceMember) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } expression.ConvertUsing <MediaUriConverter, string>(sourceMember); }
public static void DropAndCreateCollection <TSource, TSourceItem, TDestination, TDestinationItem> ( this IMemberConfigurationExpression <TSource, TDestination, ICollection <TDestinationItem> > config, Expression <Func <TSource, ICollection <TSourceItem> > > sourceCollectionSelector, Func <TSourceItem, TDestinationItem> projection = null, Action <TDestinationItem> removeCallback = null ) { config.ResolveUsing(new DropAndCreateCollectionResolver <TSource, TSourceItem, TDestination, TDestinationItem>(projection, removeCallback), sourceCollectionSelector); }
public static void IgnoreSourceWhenDefault <TSource, TDestination>(this IMemberConfigurationExpression <TSource, TDestination, object> opt) { opt.Condition((src, dest, srcValue) => { var propertyInfo = src.GetType().GetProperty(opt.DestinationMember.Name); var srcPropValue = propertyInfo.GetValue(src, null); bool isNull = srcPropValue == null; bool useSource = !isNull; return(useSource); }); }
/// <summary> /// Strings the condition. /// </summary> /// <param name="map">The map.</param> /// <param name="expression">The expression.</param> public static void StringCondition(PropertyMap map, IMemberConfigurationExpression expression) => expression.Condition( (source, destination, sourceValue, sourceDestination, context) => { if (!string.IsNullOrWhiteSpace((string)sourceValue)) { return(true); } return(false); } );
/// <summary> /// Nullables the value type condition. /// </summary> /// <param name="map">The map.</param> /// <param name="expression">The expression.</param> public static void NullableValueTypeCondition(PropertyMap map, IMemberConfigurationExpression expression) => expression.Condition( (source, destination, sourceValue, sourceDestination, context) => { if (sourceValue != null) { return(true); } return(false); } );
public void ApplyConfiguration(IMemberConfigurationExpression memberConfigurationExpression) { var sourceMemberAttribute = memberConfigurationExpression.DestinationMember.GetCustomAttribute <SourceMemberAttribute>(); if (sourceMemberAttribute != null) { memberConfigurationExpression.MapFrom(Type, sourceMemberAttribute.Name); } else { memberConfigurationExpression.MapFrom(Type); } }
public static void DropAndCreateCollection <TSource, TSourceItem, TDestination, TDestinationItem> ( this IMemberConfigurationExpression <TSource, TDestination, ICollection <TDestinationItem> > config, Expression <Func <TSource, ICollection <TSourceItem> > > sourceCollectionSelector, Func <TSourceItem, TDestinationItem> projection = null, Action <TDestinationItem> removeCallback = null, Func <TDestinationItem, bool> destinationFilter = null ) { var dropAndCreateCollectionResolver = new DropAndCreateCollectionResolver <TSource, TSourceItem, TDestination, TDestinationItem>(projection, removeCallback, destinationFilter); config.MapFrom(dropAndCreateCollectionResolver, sourceCollectionSelector); }
/// <summary> /// Nullables the value type condition. /// </summary> /// <param name="map">The map.</param> /// <param name="expression">The expression.</param> public static void NullableValueTypeCondition(PropertyMap map, IMemberConfigurationExpression expression) { expression.Condition( (_, _, sourceValue, _, _) => { if (sourceValue != null) { return(true); } return(false); } ); }
/// <summary> /// Strings the condition. /// </summary> /// <param name="map">The map.</param> /// <param name="expression">The expression.</param> public static void StringCondition(PropertyMap map, IMemberConfigurationExpression expression) { expression.Condition( (_, _, sourceValue, _, _) => { if (!string.IsNullOrWhiteSpace((string)sourceValue)) { return(true); } return(false); } ); }
private static void SyncCollectionByKeyReflectionOnly <TSource, TSourceItem, TDestination, TDestinationItem, TKey> ( this IMemberConfigurationExpression <TSource, TDestination, ICollection <TDestinationItem> > config, Expression <Func <TSource, ICollection <TSourceItem> > > sourceCollectionSelector, Expression <Func <TSourceItem, TKey> > sourceKeySelector, Expression <Func <TDestinationItem, TKey> > destinationSelector, Func <TSourceItem, TDestinationItem> createFunction = null, Action <TSourceItem, TDestinationItem> updateFunction = null, Action <TDestinationItem> removeFunction = null, bool keepRemovedItemsInDestinationCollection = false ) { SyncCollectionByKey(config, sourceCollectionSelector, sourceKeySelector.Compile(), destinationSelector.Compile(), createFunction, updateFunction, removeFunction, keepRemovedItemsInDestinationCollection); }
public static void NullSafeMapFrom <T, TResult>(this IMemberConfigurationExpression <T> opt, Expression <Func <T, TResult> > sourceMemberExpression) { var sourceMember = sourceMemberExpression.Compile(); opt.MapFrom(src => { try { return(sourceMember(src)); } catch (NullReferenceException) {} return(default(TResult)); }); }
public static void FromEntityCache <TSource, TDestination, TMember, TKey>( this IMemberConfigurationExpression <TSource, TDestination, TMember> memberConfigurationExpression, Expression <Func <TSource, TKey> > sourceKeyMember, Action <MemberConfigurationEntityCacheKeyExpression <TSource, TDestination, TMember, TKey> > cacheOptions ) { if (cacheOptions is null) { throw new ArgumentNullException(nameof(cacheOptions)); } var cacheKeyExpression = new MemberConfigurationEntityCacheKeyExpression <TSource, TDestination, TMember, TKey>( memberConfigurationExpression, sourceKeyMember); cacheOptions(cacheKeyExpression); }
/// <summary> /// Values the type condition. /// </summary> /// <param name="map">The map.</param> /// <param name="expression">The expression.</param> public static void ValueTypeCondition(PropertyMap map, IMemberConfigurationExpression expression) { var defaultValue = Activator.CreateInstance(map.SourceType); expression.Condition( (source, destination, sourceValue, sourceDestination, context) => { if (!Equals(defaultValue, sourceValue)) { return(true); } return(false); } ); }