Example #1
0
        private Func <IDataReader, T> CreateFactory <T>(IDataReader reader)
        {
            Type type = typeof(T);

            if (type.IsValueType)
            {
                return(new Func <IDataReader, T>(FactoryForValueType <T>));
            }
            else
            {
                TableInfo tableInfo = _databaseMapper.GetTableInfo <T>();
                var       injector  = _databaseMapper.GetInjector <T>();

                var dynamicMethod = new DynamicMethod(string.Format("korm_factory_{0}", _factoriesCache.Count),
                                                      type, new Type[] { typeof(IDataReader) }, true);
                var ilGenerator = dynamicMethod.GetILGenerator();

                ConstructorInfo ctor = GetConstructor(type);
                ilGenerator.Emit(OpCodes.Newobj, ctor);

                EmitReaderFields(reader, tableInfo, ilGenerator, injector);

                CallOnAfterMaterialize(tableInfo, ilGenerator);

                ilGenerator.Emit(OpCodes.Ret);

                return(dynamicMethod.CreateDelegate(Expression.GetFuncType(typeof(IDataReader), type)) as Func <IDataReader, T>);
            }
        }
        private Func <IDataReader, T> CreateFactory <T>(IDataReader reader)
        {
            Type type = typeof(T);

            if (type.IsValueType)
            {
                return(new Func <IDataReader, T>(FactoryForValueType <T>));
            }
            else
            {
                TableInfo tableInfo = _databaseMapper.GetTableInfo <T>();
                IInjector injector  = _databaseMapper.GetInjector <T>();
                (ConstructorInfo ctor, bool isDefault) = GetConstructor(type);

                if (isDefault)
                {
                    return(CreateFactoryForPropertySetters <T>(reader, tableInfo, injector, ctor));
                }
                else
                {
                    return(RecordModelFactory.CreateFactoryForRecords <T>(reader, tableInfo, injector, ctor));
                }
            }
        }