Exemplo n.º 1
0
        private void GenerateSetKeyValueDirect()
        {
            var method = _result.DefineMethod("SetKeyValueDirect", MethodAttr,
                                              null, new Type[] { objectType, objectType });

            _result.DefineMethodOverride(method, handlerBaseSetKeyValueDirect);
            var processor = new ILBuilder(method.GetILGenerator());

            if (_info.KeyMembers.Length == 1)
            {
                var h = _info.KeyMembers[0];
                processor.LoadArg(1).Cast(_model);
                processor.LoadArg(2);
                var fh = _info.KeyMembers[0];
                if (fh.MemberType == typeof(long))
                {
                    processor.Call(convertToInt64);
                }
                else if (fh.MemberType == typeof(int))
                {
                    processor.Call(convertToInt32);
                }
                else if (fh.MemberType == typeof(Guid))
                {
                    processor.Unbox(h.MemberType);
                }
                else
                {
                    processor.Cast(h.MemberType);
                }
                processor.SetMember(h);
            }

            processor.Return();
        }
Exemplo n.º 2
0
        private static void GenerateSetValue(Type model, TypeBuilder builder, TinyMember info, Type memberType)
        {
            var method = builder.DefineMethod("SetValue", CtMethodAttr,
                                              null, new Type[] { objectType, objectType });
            var processor = new ILBuilder(method.GetILGenerator());

            processor.LoadArg(1).Cast(model).LoadArg(2).CastOrUnbox(memberType);
            processor.SetMember(info);
            processor.Return();
        }
Exemplo n.º 3
0
		private static void GenerateSetValue(Type model, TypeBuilder builder, TinyMember info, Type memberType)
		{
			var method = builder.DefineMethod("SetValue", CtMethodAttr, 
				null, new Type[]{objectType, objectType});
			var processor = new ILBuilder(method.GetILGenerator());

			processor.LoadArg(1).Cast(model).LoadArg(2).CastOrUnbox(memberType);
			processor.SetMember(info);
			processor.Return();
		}
Exemplo n.º 4
0
        private void GenerateLoadSimpleValues(bool byIndex)
        {
            var name   = byIndex ? "LoadSimpleValuesByIndex" : "LoadSimpleValuesByName";
            var method = _result.DefineMethod(name, MethodAttr,
                                              null, new Type[] { objectType, iDataReaderType });

            _result.DefineMethodOverride(method, byIndex
                ? handlerBaseLoadSimpleValuesByIndex
                : handlerBaseLoadSimpleValuesByName);
            var processor = new ILBuilder(method.GetILGenerator());

            // User u = (User)o;
            processor.DeclareLocal(_model);
            processor.LoadArg(1).Cast(_model).SetLoc(0);
            // set values
            int n = 0;

            foreach (var f in _info.SimpleMembers)
            {
                processor.LoadLoc(0);
                if (f.Is.AllowNull)
                {
                    processor.LoadArg(0);
                }
                processor.LoadArg(2);
                if (byIndex)
                {
                    processor.LoadInt(n);
                }
                else
                {
                    processor.LoadArg(2).LoadString(f.Name).CallVirtual(dataReaderGetOrdinal);
                }
                var mi1 = DataReaderEmitHelper.GetMethodInfo(f.MemberType);
                if (f.Is.AllowNull || mi1 == null)
                {
                    processor.CallVirtual(dataReaderMethodInt);
                    if (f.Is.AllowNull)
                    {
                        SetSecendArgForGetNullable(f, processor);
                        processor.Call(handlerBaseTypeGetNullable);
                    }
                    // cast or unbox
                    processor.CastOrUnbox(f.MemberType);
                }
                else
                {
                    processor.CallVirtual(mi1);
                }
                processor.SetMember(f);
                n++;
            }

            processor.Return();
        }
Exemplo n.º 5
0
        private void GenerateCreateInstance()
        {
            var ctor   = _model.GetConstructor(noArgs);
            var method = _result.DefineMethod("CreateInstance", CtMethodAttr, objectType, noArgs);

            _result.DefineMethodOverride(method, handlerBaseCreateInstance);
            var processor = new ILBuilder(method.GetILGenerator());

            processor.NewObj(ctor);
            processor.Return();
        }
Exemplo n.º 6
0
        private void GenerateSetValuesForInsertDirect()
        {
            //TODO: implements this
            var method = _result.DefineMethod("SetValuesForInsertDirect", MethodAttr,
                                              null, new Type[] { keyOpValueListType, objectType });

            _result.DefineMethodOverride(method, handlerBaseSetValuesForInsertDirect);
            var processor = new ILBuilder(method.GetILGenerator());

            GenerateSetValuesDirect(processor,
                                    m => m.Is.UpdatedOn,
                                    m => m.Is.CreatedOn || m.Is.SavedOn || m.Is.Count);

            processor.Return();
        }
Exemplo n.º 7
0
        private void GenerateConstructor()
        {
            _ctor = _result.DefineConstructor(MethodAttributes.Public,
                                              CallingConventions.Standard, new Type[] { });
            var ctorBuilder = new ILBuilder(_ctor.GetILGenerator());

            ctorBuilder.LoadArg(0);
            ctorBuilder.Call(objectCtor);
            ctorBuilder.DeclareLocal(typeof(Type));
            ctorBuilder.LoadToken(_model);
            ctorBuilder.Call(getTypeFromHandle);
            ctorBuilder.SetLoc(0);

            GenerateCtorInit(ctorBuilder);
            ctorBuilder.Return();
        }
Exemplo n.º 8
0
        private static void GenerateGetValue(Type model, TypeBuilder builder, TinyMember info, Type memberType)
        {
            var method = builder.DefineMethod("GetValue", CtMethodAttr,
                                              objectType, new Type[] { objectType });
            var processor = new ILBuilder(method.GetILGenerator());

            processor.DeclareLocal(objectType);
            processor.LoadArg(1).Cast(model);
            processor.GetMember(info);
            if (memberType.IsValueType)
            {
                processor.Box(memberType);
            }
            processor.SetLoc(0);
            processor.LoadLoc(0);
            processor.Return();
        }
Exemplo n.º 9
0
        private void GenerateGetKeyValuesDirect()
        {
            var method = _result.DefineMethod("GetKeyValuesDirect", MethodAttr,
                                              null, new Type[] { dictionaryStringObjectType, objectType });

            _result.DefineMethodOverride(method, handlerBaseGetKeyValuesDirect);
            var processor = new ILBuilder(method.GetILGenerator());

            // User u = (User)o;
            processor.DeclareLocal(_model);
            processor.LoadArg(2).Cast(_model).SetLoc(0);
            // set values
            foreach (var f in _info.KeyMembers)
            {
                processor.LoadArg(1).LoadString(f.Name).LoadLoc(0);
                processor.GetMember(f);
                processor.Box(f.MemberType).CallVirtual(dictionaryStringObjectAdd);
            }

            processor.Return();
        }
Exemplo n.º 10
0
        private void GenerateSetValuesForUpdateDirect()
        {
            //TODO: implements this
            var method = _result.DefineMethod("SetValuesForUpdateDirect", MethodAttr,
                                              null, new Type[] { keyOpValueListType, objectType });

            _result.DefineMethodOverride(method, handlerBaseSetValuesForUpdateDirect);
            var processor = new ILBuilder(method.GetILGenerator());

            if (_model.BaseType == typeof(DbObjectSmartUpdate))
            {
                GenerateSetValuesForPartialUpdate(processor);
            }
            else
            {
                GenerateSetValuesDirect(processor,
                                        m => m.Is.CreatedOn || m.Is.Key,
                                        m => m.Is.UpdatedOn || m.Is.SavedOn || m.Is.Count);
            }

            processor.Return();
        }
Exemplo n.º 11
0
        private void GenerateGetKeyValueDirect()
        {
            var method = _result.DefineMethod("GetKeyValueDirect", MethodAttr,
                                              objectType, new Type[] { objectType });

            _result.DefineMethodOverride(method, handlerBaseGetKeyValueDirect);
            var processor = new ILBuilder(method.GetILGenerator());

            if (_info.KeyMembers.Length == 1)
            {
                var h = _info.KeyMembers[0];
                processor.LoadArg(1).Cast(_model);
                processor.GetMember(h);
                processor.Box(h.MemberType);
            }
            else
            {
                processor.LoadNull();
            }

            processor.Return();
        }
Exemplo n.º 12
0
        private void GenerateCtorInit(ILBuilder ctorBuilder)
        {
            var method = _result.DefineMethod("CtorInit", CtMethodAttr, null, dbObjectSmartUpdateArgs);

            _result.DefineMethodOverride(method, handlerBaseCtorInit);
            var processor = new ILBuilder(method.GetILGenerator());

            foreach (var f in _info.Members)
            {
                if (!(f.Is.RelationField || f.Is.LazyLoad))
                {
                    continue;
                }
                if (f.MemberInfo.IsProperty)
                {
                    var pi     = (PropertyInfo)f.MemberInfo.GetMemberInfo();
                    var setter = pi.GetSetMethod(true);
                    if (setter.IsPrivate)
                    {
                        GenerateNewMemberWithPrivateSetter(f, processor, ctorBuilder);
                    }
                    else
                    {
                        processor.LoadArg(1);
                        GenerateNewMember(f, processor);
                        processor.CallVirtual(setter);
                    }
                }
                else
                {
                    processor.LoadArg(1);
                    GenerateNewMember(f, processor);
                    processor.SetField((FieldInfo)f.MemberInfo.GetMemberInfo());
                }
            }
            processor.Return();
        }
Exemplo n.º 13
0
        private void GenerateSetValuesForSelectDirectDirect(bool noLazy)
        {
            var methodName = noLazy ? "SetValuesForSelectDirectNoLazy" : "SetValuesForSelectDirect";
            var method     = _result.DefineMethod(methodName, MethodAttr,
                                                  null, new Type[] { listKeyValuePairStringStringType });

            _result.DefineMethodOverride(method, noLazy
                ? handlerBaseSetValuesForSelectDirectNoLazy
                : handlerBaseSetValuesForSelectDirect);
            var processor = new ILBuilder(method.GetILGenerator());

            foreach (var f in _info.Members)
            {
                if (!f.Is.HasOne && !f.Is.HasMany && !f.Is.HasAndBelongsToMany)
                {
                    if (noLazy || !f.Is.LazyLoad)
                    {
                        processor.LoadArg(1);

                        processor.LoadString(f.Name);
                        if (f.Name != f.MemberInfo.Name)
                        {
                            processor.LoadString(f.MemberInfo.Name);
                        }
                        else
                        {
                            processor.LoadNull();
                        }
                        processor.NewObj(keyValuePairStringStringCtor);

                        processor.CallVirtual(listKeyValuePairStringStringAdd);
                    }
                }
            }

            processor.Return();
        }
Exemplo n.º 14
0
        private void GenerateLoadRelationValues(bool useIndex, bool noLazy)
        {
            int    index      = _info.SimpleMembers.Length;
            string methodName = useIndex ? "LoadRelationValuesByIndex" : "LoadRelationValuesByName";

            if (noLazy)
            {
                methodName = methodName + "NoLazy";
            }

            var method = _result.DefineMethod(methodName, MethodAttr,
                                              null, new Type[] { objectType, iDataReaderType });

            _result.DefineMethodOverride(method, useIndex
                ? (noLazy ? handlerBaseLoadRelationValuesByIndexNoLazy : handlerBaseLoadRelationValuesByIndex)
                : (noLazy ? handlerBaseLoadRelationValuesByNameNoLazy : handlerBaseLoadRelationValuesByName));
            var processor = new ILBuilder(method.GetILGenerator());

            if (_info.RelationMembers.Length > 0)
            {
                // User u = (User)o;
                processor.DeclareLocal(_model);
                processor.LoadArg(1).Cast(_model).SetLoc(0);
                // set values
                foreach (var f in _info.RelationMembers)
                {
                    if (f.Is.LazyLoad)
                    {
                        if (noLazy)
                        {
                            processor.LoadLoc(0);
                            processor.GetMember(f);
                            processor.LoadArg(2);
                            if (useIndex)
                            {
                                processor.LoadInt(index++).CallVirtual(dataReaderMethodInt);
                            }
                            else
                            {
                                processor.LoadString(f.Name).CallVirtual(dataReaderMethodString);
                            }
                            processor.LoadInt(0);
                            processor.CallVirtual(lazyLoadingInterfaceWrite);
                        }
                    }
                    else if (f.Is.BelongsTo)
                    {
                        processor.LoadLoc(0);
                        processor.GetMember(f);
                        processor.LoadArg(2);
                        if (useIndex)
                        {
                            processor.LoadInt(index++).CallVirtual(dataReaderMethodInt);
                        }
                        else
                        {
                            processor.LoadString(f.Name).CallVirtual(dataReaderMethodString);
                        }
                        processor.CallVirtual(belongsToInterfaceSetForeignKey);
                    }
                }
            }

            processor.Return();
        }
Exemplo n.º 15
0
		private static void GenerateGetValue(Type model, TypeBuilder builder, TinyMember info, Type memberType)
		{
			var method = builder.DefineMethod("GetValue", CtMethodAttr, 
				objectType, new Type[]{objectType});
			var processor = new ILBuilder(method.GetILGenerator());

			processor.DeclareLocal(objectType);
			processor.LoadArg(1).Cast(model);
			processor.GetMember(info);
			if (memberType.IsValueType)
			{
				processor.Box(memberType);
			}
			processor.SetLoc(0);
			processor.LoadLoc(0);
			processor.Return();
		}