Esempio n. 1
0
        /// <summary>
        /// <para>Applies a previously registered action on a object. The contextCotainer object MUST contain any context objects required by the registered action.</para>
        /// <para>Registered actions on interfaces and base classes are also automaticaly applied in this order: Interface actions, Base Class (top to bottom) actions, Target type action.</para>
        /// </summary>
        /// <typeparam name="T">The target type</typeparam>
        /// <param name="item">The target instance</param>
        /// <param name="contextContainer">A context container created by LinqMapper.CreateContext() or null</param>
        /// <returns>The target instance</returns>
        public static T ApplyAfterMap <T>(T item, IObjectContext contextContainer = null)
        {
            if (item == null)
            {
                return(item);
            }

            contextContainer = contextContainer ?? LinqMapper.CreateContext();
            var runtimeInfo = InternalCache.GetRuntimeInfo(typeof(T));

            if (runtimeInfo != null)
            {
                //make sure to dispose tracker
                using (var tracker = new MaterializerTracker())
                {
                    if (Materializer.TreeTraversal == TreeTraversal.ChildrenFirst)
                    {
                        runtimeInfo.ApplyOn(item, contextContainer, tracker);
                    }
                    else
                    {
                        runtimeInfo.ApplyOnRootFirst(item, contextContainer, tracker);
                    }
                }
            }

            return(item);
        }
            public ILinqMapperSyntax <TSource, TDest> IgnoreMembers(params string[] members)
            {
                var map = LinqMapper.GetMapCreator(_key);

                map.IgnoreMembers(members);

                return(this);
            }
Esempio n. 3
0
        private Dictionary <string, MemberAssignment> GetPropertyBindings()
        {
            if (this._PropertyBindings != null || _expression == null)
            {
                return(this._PropertyBindings);
            }

            return(this._PropertyBindings = LinqMapper.GetBindingExpressionsDictionary(_expression));
        }
        private IMapCreator GetBaseMapCreator()
        {
            if (_baseMapCreator != null)
            {
                return(_baseMapCreator);
            }

            return(_baseMapCreator = LinqMapper.GetMapCreator(_basekey));
        }
Esempio n. 5
0
        private IQueryable <TDest> GetDestQuery()
        {
            var map   = LinqMapper.GetMap <TSource, TDest>(_materializerContext);
            var query = _source.Select(map);

            if (_queryAction != null)
            {
                query = _queryAction(query);
            }

            return(query);
        }
            public ILinqMapperSyntax <TSource, TDest> IgnoreRuntimeMembers(Func <IObjectContext, IEnumerable <string> > runtimeIgnore)
            {
                if (runtimeIgnore == null)
                {
                    throw new ArgumentNullException("runtimeDelegate");
                }

                var map = LinqMapper.GetMapCreator(_key);

                map.IgnoreRuntimeMembers(runtimeIgnore);

                return(this);
            }
            public ILinqMapperSyntax <TSource, TDest> IgnoreMembers(params Expression <Func <TDest, object> >[] members)
            {
                if (members == null || members.Length == 0)
                {
                    return(this);
                }

                var memberNames = members.Select(m => GetMemberName(m)).ToList();
                var map         = LinqMapper.GetMapCreator(_key);

                map.IgnoreMembers(memberNames);

                return(this);
            }
            public ILinqMapperSyntax <TSource, TDest> CreateRuntimeMap <TContext>(Func <TContext, Expression <Func <TSource, TDest> > > runtimeMapCreator)
                where TContext : class
            {
                if (runtimeMapCreator == null)
                {
                    return(this);
                }

                var map        = LinqMapper.GetMapCreator(_key);
                var runtimeMap = new RuntimeMapCreator <TContext, TSource, TDest>(runtimeMapCreator);

                map.Add(runtimeMap);

                return(this);
            }
            public ILinqMapperSyntax <TSource, TDest> IncludeBase <TBaseSource, TBaseDest>()
            {
                if (!typeof(TBaseSource).IsAssignableFrom(typeof(TSource)) || !typeof(TBaseDest).IsAssignableFrom(typeof(TDest)) ||
                    (typeof(TBaseSource) == typeof(TSource) && typeof(TBaseDest) == typeof(TDest)))
                {
                    throw new ArgumentException($"One or more base types [{typeof(TBaseSource).FullName}, {typeof(TBaseDest).FullName}] are not compatible with the current map types [{typeof(TSource).FullName}, {typeof(TDest).FullName}].");
                }

                var map     = LinqMapper.GetMapCreator(_key);
                var baseMap = new CastBaseMapCreator <TBaseSource, TBaseDest, TSource, TDest>();

                //Base map happens first
                map.AddFirst(baseMap);

                return(this);
            }
        private Expression <Func <TSource, TDest> > CreateDefaultMap()
        {
            var sourceType = typeof(TSource);
            var destType   = typeof(TDest);

            var propertyBindings = new Dictionary <string, MemberAssignment>();

            foreach (var propertyBinding in LinqMapper.GetPropertyBindings(sourceType, destType, this.SourceParameter)
                     .Where(m => !this._ignoreMembers.Contains(m.Member.Name)))
            {
                propertyBindings.Add(propertyBinding.Member.Name, propertyBinding);
            }

            var newExp = ExpressionNewCreator <TDest> .NewExpression;
            var body   = (Expression)Expression.MemberInit(newExp, propertyBindings.Values);

            return(Expression.Lambda <Func <TSource, TDest> >(body, this.SourceParameter));
        }
        public ExtendedMapCreator(Expression <Func <TSource, TDest> > customMapping = null)
        {
            IList <string>      ignoreMembers;
            ParameterExpression sourceParam = null;

            if (customMapping != null)
            {
                sourceParam   = customMapping.Parameters[0];
                ignoreMembers = LinqMapper.GetBindingExpressions(customMapping).Select(e => e.Member.Name).ToList();
            }
            else
            {
                sourceParam   = Expression.Parameter(typeof(TSource), "src");
                ignoreMembers = ArrayExtensions.EmptyArray <string>();
            }

            _defaultCreator = new DefaultMapCreator <TSource, TDest>(sourceParam, ignoreMembers);

            if (customMapping != null)
            {
                this.Add(new StaticMapCreator <TSource, TDest>(sourceParam, customMapping));
            }
        }
        protected override Expression VisitMethodCall(MethodCallExpression expression)
        {
            bool optionalInvoke;

            if (isInvokeMapExpression(expression, out optionalInvoke))
            {
                var invokeMapExp     = expression as MethodCallExpression;
                var innerExp         = invokeMapExp.Arguments[0]; // e.g. src.Member.Property
                var genericArguments = invokeMapExp.Method.GetGenericArguments();

                var existingMap = invokeMapExp.Arguments.Count == 1 ? LinqMapper.GetMapInternal(genericArguments[0], genericArguments[1], resetCallContext: false, context: _context)
                                    : invokeMapExp.Arguments[1];

                var invokeMethodInfo = LinqKitInvokeTemplate.MakeGenericMethod(genericArguments[0], genericArguments[1]);
                var invokeExp        = Expression.Call(invokeMethodInfo, existingMap, innerExp);
                this.ExpressionWasReplaced = true;

                var expandedInvoke = invokeExp.Expand();

                if (optionalInvoke)
                {
                    // condition test: (src.Member.Property == null)
                    var conditionTest = Expression.MakeBinary(ExpressionType.Equal, innerExp, _NullObjectExpression);

                    // src.Member.Property == null ? (TDest) null : [TSource to TDest Expression]
                    var conditionalExp = Expression.Condition(conditionTest, Expression.Constant(null, expandedInvoke.Type), expandedInvoke, expandedInvoke.Type);

                    return(conditionalExp);
                }
                else
                {
                    return(expandedInvoke);
                }
            }

            return(base.VisitMethodCall(expression));
        }
Esempio n. 13
0
 public QueryMaterializer(IQueryable <TSource> source)
 {
     _source = source;
     _materializerContext = LinqMapper.CreateContext();
 }