Пример #1
0
 public void CheckForUninstanciables()
 {
     foreach (Object mappingobj in _mappings)
     {
         IStructureMap mapping = mappingobj as IStructureMap;
         if (mapping == null)
         {
             continue;
         }
         ConstructorInfo constructor = null;
         if (mapping.Injecting.Length > 0)
         {
             constructor = mapping.Using.GetConstructor(mapping.Injecting);
         }
         if (constructor == null)
         {
             constructor = mapping.Using.GetConstructor(new[] { typeof(IStructureBroker) });
         }
         if (constructor == null)
         {
             constructor = mapping.Using.GetConstructor(new Type[0]);
         }
         if (constructor == null)
         {
             throw new UninstanciableStructureException(mapping.Using);
         }
     }
 }
Пример #2
0
        private static void SetupIdDataChecker(List <ParameterExpression> variables,
                                               List <Expression> bodyExpressions,
                                               ParameterExpression dataParameter,
                                               List <Expression> mappingBodyExpressions,
                                               IStructureMap mapper)
        {
            //get and assign dictionaries
            Tuple <ParameterExpression, Type> dictParamAndVariable =
                GetDictionaryVariableForUniqueCheck(mapper.IdPropertyType, mapper.SourceType);
            BinaryExpression dictAssignment = Expression.Assign(dictParamAndVariable.Item1,
                                                                Expression.New(dictParamAndVariable.Item2));

            variables.Add(dictParamAndVariable.Item1);
            bodyExpressions.Add(dictAssignment);
            //get and assign id evaluators
            Tuple <ParameterExpression, ConstantExpression> propertyEvaluatorParamAndValue =
                GetIdEvaluatorExpression(mapper);
            BinaryExpression evaulatorAssignment = Expression.Assign(propertyEvaluatorParamAndValue.Item1,
                                                                     propertyEvaluatorParamAndValue.Item2);

            variables.Add(propertyEvaluatorParamAndValue.Item1);
            bodyExpressions.Add(evaulatorAssignment);
            // declare data parameters and assign them to inner fake mapping function declaration
            MethodCallExpression checkerExpression = BuildUniquenessCheckerExpression(mapper, dictParamAndVariable.Item1,
                                                                                      propertyEvaluatorParamAndValue.Item1, dataParameter);

            mappingBodyExpressions.Add(Expression.Assign(dataParameter, checkerExpression));
        }
Пример #3
0
        public Object InstanciateFor(Type type)
        {
            IStructureMap mapping = this.Find(type);

            if (mapping == null)
            {
                return(null);
            }
            ConstructorInfo constructor = null;

            if (mapping.Injecting.Length > 0)
            {
                constructor = mapping.Using.GetConstructor(mapping.Injecting);
                if (constructor != null)
                {
                    constructor.Invoke(this.InstanciateFor(mapping.Injecting));
                }
            }
            if (constructor == null)
            {
                constructor = mapping.Using.GetConstructor(new[] { typeof(IStructureBroker) });
            }
            if (constructor == null)
            {
                constructor = mapping.Using.GetConstructor(new Type[0]);
            }
            return(constructor.Invoke(new object[0]));
        }
Пример #4
0
        private static MethodCallExpression BuildUniquenessCheckerExpression(IStructureMap mapper,
                                                                             ParameterExpression dictionary, ParameterExpression idPropertyEvaluator, ParameterExpression data)
        {
            MethodInfo getUniqueDataFunction =
                ExpressionHelper.MakeGenericMethod(() => GetUniqueData <string, string>(null, null, null),
                                                   mapper.IdPropertyType, mapper.SourceType);

            return(Expression.Call(getUniqueDataFunction, dictionary, idPropertyEvaluator, data));
        }
Пример #5
0
        private static Expression GetIdEvaluatorFromMapper <TSource, TDestination>(IStructureMap mapper)
            where TSource : class
        {
            IStructureMapItem <TSource, TDestination> idProperty =
                (mapper as StructureMap <TSource, TDestination>).GetIdPropertyItem();
            MethodInfo methodInfo =
                ExpressionHelper.MakeGenericMethod(() => GetIdEvaluatorFromMappingItem <string, string, string>(null),
                                                   typeof(TSource), mapper.DestinationType, idProperty.ResultType);

            return(methodInfo.Invoke(null, new object[] { idProperty }) as Expression);
        }
Пример #6
0
 public void CheckForCircularDependencies()
 {
     foreach (Object mappingObj in _mappings)
     {
         IStructureMap mapping = mappingObj as IStructureMap;
         if (mapping == null)
         {
             continue;
         }
         this.CheckForCircularDependencies(mapping, new ArrayList());
     }
 }
Пример #7
0
        public IStructureMap For(Type type)
        {
            IStructureMap found = this.Find(type);

            if (found != null)
            {
                return(found);
            }
            StructureMap mapping = new StructureMap(type);

            this._mappings.Add(mapping);
            return(mapping);
        }
Пример #8
0
        private void CheckForCircularDependencies(IStructureMap map, ArrayList upperMapStack)
        {
            ArrayList mapStack = upperMapStack.Clone() as ArrayList;

            if (mapStack == null)
            {
                return;
            }
            if (mapStack.Contains(map))
            {
                mapStack.Add(map);
                throw new CirclularDependancyStructureException(mapStack);
            }
            mapStack.Add(map);
            foreach (Type type in map.Injecting)
            {
                IStructureMap mapping = this.Find(type);
                if (mapping == null)
                {
                    continue;
                }
                this.CheckForCircularDependencies(mapping, mapStack);
            }
        }
Пример #9
0
        BuildFunction <TReturn, TFunc>()
        {
            var        mappingDescriptor    = new MappingFunctionDescriptor(typeof(TFunc));
            MethodInfo queryMethodSignature = GetSqlMapperQueryMethod(mappingDescriptor);

            // variables that are visible in main scope
            var variables = new List <ParameterExpression>();
            //body of main query method
            var methodBodyExpressions = new List <Expression>();
            ////query method incoming parameters
            //var queryMethodParameters = new List<ParameterExpression>();
            // parameters for fake map function
            var mappingParameters = new List <ParameterExpression>();
            // body for fake map function
            var mappingBodyExpressions = new List <Expression>();

            // declare and assign dictionaries and id evaluators for mapped types
            foreach (Type type in mappingDescriptor.GenericTypes.Take(mappingDescriptor.GenericTypes.Length - 1))
            {
                IStructureMap mapper = LeanMapper.GetMapper(type);
                mappingParameters.Add(Expression.Parameter(type));
                if (mapper == null)
                {
                    continue;
                }
                SetupIdDataChecker(variables, methodBodyExpressions, mappingParameters.Last(), mappingBodyExpressions,
                                   mapper);
            }

            // declare parameters for main query method (they will be passed down to inner SqlMapper.Query method call)
            ParameterExpression cnnP            = Expression.Parameter(typeof(IDbConnection), "cnn");
            ParameterExpression sqlP            = Expression.Parameter(typeof(string), "sql");
            ParameterExpression mapParam        = Expression.Parameter(mappingDescriptor.MappingType, "map");
            ParameterExpression paramP          = Expression.Parameter(typeof(object), "param");
            ParameterExpression transactionP    = Expression.Parameter(typeof(IDbTransaction), "transaction");
            ParameterExpression bufferedP       = Expression.Parameter(typeof(bool), "buffered");
            ParameterExpression splitOnP        = Expression.Parameter(typeof(string), "splitOn");
            ParameterExpression commandTimeoutP = Expression.Parameter(typeof(int?), "commandTimeout");
            ParameterExpression commandTypeP    = Expression.Parameter(typeof(CommandType?), "commandType");
            ParameterExpression newMapper       = Expression.Parameter(mappingDescriptor.MappingType, "newMapper");

            methodBodyExpressions.Add(Expression.Assign(newMapper,
                                                        FinishFakeMappingFunction <TReturn, TFunc>(mapParam, mappingParameters, mappingBodyExpressions)));
            variables.Add(newMapper);
            //call REAL Query Method
            MethodCallExpression callRealDapperMethod = Expression.Call(queryMethodSignature, cnnP, sqlP, newMapper,
                                                                        paramP, transactionP, bufferedP, splitOnP, commandTimeoutP, commandTypeP);

            LabelTarget     returnFinalTarget      = Expression.Label(typeof(IEnumerable <TReturn>));
            GotoExpression  returnFinalValue       = Expression.Return(returnFinalTarget, callRealDapperMethod);
            LabelExpression rerturnFinalExpression = Expression.Label(returnFinalTarget,
                                                                      Expression.Default(typeof(IEnumerable <TReturn>)));

            methodBodyExpressions.Add(returnFinalValue);
            methodBodyExpressions.Add(rerturnFinalExpression);

            return
                (Expression
                 .Lambda
                 <Func <IDbConnection, string, TFunc, dynamic, IDbTransaction, bool, string, int?, CommandType?,
                        IEnumerable <TReturn> > >(
                     Expression.Block(variables, methodBodyExpressions),
                     cnnP, sqlP, mapParam, paramP, transactionP, bufferedP, splitOnP, commandTimeoutP,
                     commandTypeP).Compile());
        }
Пример #10
0
        private static Tuple <ParameterExpression, ConstantExpression> GetIdEvaluatorExpression(IStructureMap mapper)
        {
            MethodInfo methodInfo =
                ExpressionHelper.MakeGenericMethod(() => GetIdEvaluatorFromMapper <string, string>(null),
                                                   mapper.SourceType, mapper.DestinationType);
            var idPropertyEvaluatorConstant = methodInfo.Invoke(null, new object[] { mapper }) as ConstantExpression;

            if (idPropertyEvaluatorConstant == null)
            {
                throw new InvalidCastException("cant't cast idPropertyEvaluatorConstant to ConstantExpression");
            }
            ParameterExpression idPropertyEvaluatorParameter = Expression.Parameter(idPropertyEvaluatorConstant.Type);

            return(new Tuple <ParameterExpression, ConstantExpression>(idPropertyEvaluatorParameter,
                                                                       idPropertyEvaluatorConstant));
        }