Exemplo n.º 1
0
        public IObjectReaderFactory Compile(SqlExpression expression, Type elementType)
        {
            object                   mapping = this.services.Context.Mapping.Identity;
            DataLoadOptions          options = this.services.Context.LoadOptions;
            IObjectReaderFactory     factory = null;
            ObjectReaderFactoryCache cache   = null;
            bool canBeCompared = SqlProjectionComparer.CanBeCompared(expression);

            if (canBeCompared)
            {
                cache = (ObjectReaderFactoryCache)Thread.GetData(cacheSlot);
                if (cache == null)
                {
                    cache = new ObjectReaderFactoryCache(maxReaderCacheSize);
                    Thread.SetData(cacheSlot, cache);
                }
                factory = cache.GetFactory(elementType, this.dataReaderType, mapping, options, expression);
            }
            if (factory == null)
            {
                DynamicTypeGenerator gen = new DynamicTypeGenerator(this, elementType);
#if DEBUG
                if (ObjectReaderCompiler.CaptureModule != null)
                {
                    this.CompileCapturedMethod(gen, expression, elementType);
                }
#endif
                DynamicMethod dm            = this.CompileDynamicMethod(gen, expression, elementType);
                Type          fnMatType     = typeof(Func <,>).MakeGenericType(typeof(ObjectMaterializer <>).MakeGenericType(this.dataReaderType), elementType);
                var           fnMaterialize = (Delegate)dm.CreateDelegate(fnMatType);

                Type factoryType = typeof(ObjectReaderFactory <,>).MakeGenericType(this.dataReaderType, elementType);
                factory = (IObjectReaderFactory)Activator.CreateInstance(
                    factoryType, BindingFlags.Instance | BindingFlags.NonPublic, null,
                    new object[] { fnMaterialize, gen.NamedColumns, gen.Globals, gen.Locals }, null
                    );

                if (canBeCompared)
                {
                    expression = new SourceExpressionRemover().VisitExpression(expression);
                    cache.AddFactory(elementType, this.dataReaderType, mapping, options, expression, factory);
                }
            }
            return(factory);
        }
Exemplo n.º 2
0
        public IObjectReaderFactory Compile(SqlExpression expression, Type elementType)
        {
            //-------------------------SOURCE CODE--------------------------
            object identity = this.services.Context.Mapping.Identity;
            //--------------------------------------------------------------
            //var bf = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty;
            //object identity = typeof(MetaModel).InvokeMember("Identity", bf, null, services.Context.Mapping, null);
            //--------------------------------------------------------------
            DataLoadOptions      loadOptions = services.Context.LoadOptions;
            IObjectReaderFactory factory     = null;
            ReaderFactoryCache   data        = null;
            bool flag = SqlProjectionComparer.CanBeCompared(expression);

            if (flag)
            {
                data = (ReaderFactoryCache)Thread.GetData(cacheSlot);
                if (data == null)
                {
                    data = new ReaderFactoryCache(maxReaderCacheSize);
                    Thread.SetData(cacheSlot, data);
                }
                factory = data.GetFactory(elementType, dataReaderType, identity, loadOptions, expression);
            }
            if (factory == null)
            {
                var           gen          = new Generator(this, elementType);
                DynamicMethod method       = CompileDynamicMethod(gen, expression, elementType);
                var           t            = typeof(ObjectMaterializer <>).MakeGenericType(new[] { dataReaderType });
                var           delegateType = typeof(Func <,>).MakeGenericType(new[] { t, elementType });
                var           delegate2    = method.CreateDelegate(delegateType);

                factory = (IObjectReaderFactory)
                          Activator.CreateInstance(typeof(ObjectReaderFactory <,>).MakeGenericType(new[] { dataReaderType, elementType }),
                                                   BindingFlags.NonPublic | BindingFlags.Instance, null,
                                                   new object[] { delegate2, gen.NamedColumns, gen.Globals, gen.Locals }, null);
                if (flag)
                {
                    expression = new SourceExpressionRemover().VisitExpression(expression);
                    data.AddFactory(elementType, dataReaderType, identity, loadOptions, expression, factory);
                }
            }
            return(factory);
        }