コード例 #1
0
        public void RegisterAll(object mapper, Type interfaces, Type[] implementedInterfaces)
        {
            lock (_sync)
            {
                Type propertiesMapperType = typeof(IPropertiesMapper <,>);
                foreach (Type interfaceType in implementedInterfaces.Where(i => i.GUID == propertiesMapperType.GUID))
                {
                    Type[] genericArgs = interfaceType.GetGenericArguments();

                    var pairId = PairId.GetId(genericArgs[0], genericArgs[1]);

                    var  methodInfo      = interfaces.GetInterfaceMap(interfaceType).InterfaceMethods.Single();
                    Type delegateType    = typeof(Action <,>).MakeGenericType(genericArgs[0], genericArgs[1]);
                    var  factoryDelegate = Delegate.CreateDelegate(delegateType, mapper, methodInfo, true);

                    if (_cache.ContainsKey(pairId))
                    {
                        _cache[pairId] = factoryDelegate;
                    }
                    else
                    {
                        _cache.Add(pairId, factoryDelegate);
                    }
                }
            }
        }
コード例 #2
0
 public void Register <TSource, TDest>(IPropertiesMapper <TSource, TDest> mapper)
 {
     lock (_sync)
     {
         PairId id = PairId.GetId <TSource, TDest>();
         if (_cache.ContainsKey(id))
         {
             _cache[id] = (Action <TSource, TDest>)mapper.MapProperties;
         }
         else
         {
             _cache.Add(PairId.GetId <TSource, TDest>(), (Action <TSource, TDest>)mapper.MapProperties);
         }
     }
 }
コード例 #3
0
        public bool TryGetMapperFromCache <TSource, TDest>(out Action <TSource, TDest> mapper)
        {
            PairId   pairId = PairId.GetId <TSource, TDest>();
            Delegate mapperObject;

            if (_cache.TryGetValue(pairId, out mapperObject))
            {
                mapper = (Action <TSource, TDest>)mapperObject;
                return(true);
            }
            else
            {
                mapper = null;
                return(false);
            }
        }
コード例 #4
0
        public Action <TSource, TDest> GetMapper <TSource, TDest>(TSource source, TDest dest)
        {
            PairId pairId = PairId.GetId <TSource, TDest>();

            Delegate mappingDelegate;

            if (_cache.TryGetValue(pairId, out mappingDelegate))
            {
                return((Action <TSource, TDest>)mappingDelegate);
            }

            lock (_sync)
            {
                IPropertiesMapper <TSource, TDest> mapper;
                Action <TSource, TDest>            mappingAction;

                if (_cache.TryGetValue(pairId, out mappingDelegate))
                {
                    mappingAction = (Action <TSource, TDest>)mappingDelegate;
                }
                else
                {
                    if ((mapper = dest as IPropertiesMapper <TSource, TDest>) != null)
                    {
                        mappingAction = mapper.MapProperties;
                        _cache.Add(pairId, mappingAction);
                    }
                    else
                    {
                        if ((mapper = source as IPropertiesMapper <TSource, TDest>) != null)
                        {
                            mappingAction = mapper.MapProperties;
                            _cache.Add(pairId, mappingAction);
                        }
                        else
                        {
                            throw new NotSupportedException(
                                      String.Format("Convert expression for {0}->{1} does not exist.",
                                                    typeof(TSource).FullName, typeof(TDest).FullName));
                        }
                    }
                }

                return(mappingAction);
            }
        }
コード例 #5
0
        public void Register <TSource, TDest, TParam1, TParam2>(ISelectDynamicExpression <TSource, TDest, TParam1, TParam2> resolver)
        {
            PairId id = PairId.GetId <TSource, TDest>();

            var factory = (Func <TParam1, TParam2, Expression <Func <TSource, TDest> > >)resolver.GetSelectExpression;

            lock (_sync)
            {
                if (_dynamicExpressionFactories.ContainsKey(id))
                {
                    _dynamicExpressionFactories[id] = factory;
                }
                else
                {
                    _dynamicExpressionFactories.Add(id, factory);
                }
            }
        }
コード例 #6
0
        public void Register <TSource, TDest>(ISelectExpression <TSource, TDest> resolver)
        {
            PairId id = PairId.GetId <TSource, TDest>();

            var factory = (Func <Expression <Func <TSource, TDest> > >)resolver.GetSelectExpression;

            lock (_sync)
            {
                if (_expressionFactories.ContainsKey(id))
                {
                    _expressionFactories[id] = factory;
                    TryRemoveExpressions(id);
                }
                else
                {
                    _expressionFactories.Add(id, factory);
                }
            }
        }
コード例 #7
0
        private bool TryGetOrActivate <TSource, TDest, TParam1, TParam2>(TParam1 param1, TParam2 param2, Func <ISelectExpression <TSource, TDest, TParam1, TParam2> > activator, out Expression <Func <TSource, TDest> > expression)
        {
            PairId id = PairId.GetId <TSource, TDest>();

            Dictionary <PairId, LambdaExpression> expressions = GetOrAddParamExpressionsDictionary(param1, param2, add: false);

            Delegate         factoryDelegate;
            LambdaExpression lambdaExpression;

            if (expressions != null && expressions.TryGetValue(id, out lambdaExpression))
            {
                expression = (Expression <Func <TSource, TDest> >)lambdaExpression;

                return(true);
            }
            else if (_dynamicExpressionFactories.TryGetValue(id, out factoryDelegate))
            {
                expression = ((Func <TParam1, TParam2, Expression <Func <TSource, TDest> > >)factoryDelegate)(param1, param2);

                return(true);
            }

            lock (_sync)
            {
                expressions = expressions ?? GetOrAddParamExpressionsDictionary(param1, param2, add: true);

                if (expressions.TryGetValue(id, out lambdaExpression))
                {
                    expression = (Expression <Func <TSource, TDest> >)lambdaExpression;

                    return(true);
                }

                Func <TParam1, TParam2, Expression <Func <TSource, TDest> > > factory;

                if (_expressionFactories.TryGetValue(id, out factoryDelegate))
                {
                    factory    = (Func <TParam1, TParam2, Expression <Func <TSource, TDest> > >)factoryDelegate;
                    expression = factory(param1, param2);
                    expressions.Add(id, expression);

                    TryRemoveFactory(id);

                    return(true);
                }

                if (_dynamicExpressionFactories.TryGetValue(id, out factoryDelegate))
                {
                    factory    = (Func <TParam1, TParam2, Expression <Func <TSource, TDest> > >)factoryDelegate;
                    expression = factory(param1, param2);

                    return(true);
                }

                if (activator != null)
                {
                    ISelectExpression <TSource, TDest, TParam1, TParam2> selectResolver = activator();
                    factory = selectResolver.GetSelectExpression;

                    if (selectResolver is ISelectDynamicExpression <TSource, TDest, TParam1, TParam2> )
                    {
                        _dynamicExpressionFactories.Add(id, factory);
                        expression = factory(param1, param2);

                        return(true);
                    }
                    else
                    {
                        _expressionFactories.Add(id, factory);
                        expression = factory(param1, param2);
                        expressions.Add(id, expression);

                        TryRemoveFactory(id);

                        return(true);
                    }
                }
            }

            expression = null;
            return(false);
        }
コード例 #8
0
        public void RegisterAll(object mapper, Type mapperType, Type[] implementedInterfaces)
        {
            Type selectExpressionType        = typeof(ISelectExpression <,>);
            Type dynamicSelectExpressionType = typeof(ISelectDynamicExpression <,>);

            var selectExpressions = implementedInterfaces.Where(i => i.GUID == selectExpressionType.GUID || i.GUID == dynamicSelectExpressionType.GUID)
                                    .Select(i =>
            {
                var genericArgs = i.GetGenericArguments();
                return(new
                {
                    InterfaceType = i,
                    GenericArguments = genericArgs,
                    PairId = PairId.GetId(genericArgs[0], genericArgs[1])
                });
            })
                                    .GroupBy(i => i.PairId)
                                    .Select(
                g => g.FirstOrDefault(i => i.InterfaceType.GUID == dynamicSelectExpressionType.GUID)
                ?? g.First(i => i.InterfaceType.GUID == selectExpressionType.GUID)
                )
                                    .Select(interfaceDescription =>
            {
                var methodInfo = mapperType.GetInterfaceMap(interfaceDescription.InterfaceType).InterfaceMethods.SingleOrDefault()
                                 ?? interfaceDescription.InterfaceType.GetInterfaces().SelectMany(i => mapperType.GetInterfaceMap(i).TargetMethods).Single();

                Type delegateType = typeof(Func <>).MakeGenericType(
                    typeof(Expression <>).MakeGenericType(
                        typeof(Func <,>).MakeGenericType(interfaceDescription.PairId.SourceId, interfaceDescription.PairId.DestId)
                        )
                    );

                var factoryDelegate = Delegate.CreateDelegate(delegateType, mapper, methodInfo, true);

                return(new
                {
                    InterfaceType = interfaceDescription.InterfaceType,
                    PairId = interfaceDescription.PairId,
                    FactoryDelegate = factoryDelegate
                });
            })
                                    .ToList();

            lock (_sync)
            {
                foreach (var selectExpression in selectExpressions)
                {
                    Dictionary <PairId, Delegate> cacheDictionary;
                    if (selectExpression.InterfaceType.GUID == dynamicSelectExpressionType.GUID)
                    {
                        cacheDictionary = _dynamicExpressionFactories;
                    }
                    else if (selectExpression.InterfaceType.GUID == selectExpressionType.GUID)
                    {
                        cacheDictionary = _expressionFactories;
                    }
                    else
                    {
                        throw new NotSupportedException(String.Format("The interface {0} caching is not supported.", selectExpression.InterfaceType.FullName));
                    }

                    if (cacheDictionary.ContainsKey(selectExpression.PairId))
                    {
                        cacheDictionary[selectExpression.PairId] = selectExpression.FactoryDelegate;
                    }
                    else
                    {
                        cacheDictionary.Add(selectExpression.PairId, selectExpression.FactoryDelegate);
                    }
                }
            }
        }