Exemple #1
0
        private void GenerateSetValuesForUpdateDirect()
        {
            //TODO: implements this
            var method = new MethodDefinition("SetValuesForUpdateDirect", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.SetValuesForUpdateDirect);
            method.Parameters.Add(new ParameterDefinition("values", ParameterAttributes.None, _handler.KeyOpValueListType));
            method.Parameters.Add(new ParameterDefinition("o", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

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


            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #2
0
        private PropertyDefinition GenerateProperty(PropertyDefinition property)
        {
            const MethodAttributes attr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName |
                                          MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final;

            var p = TypeFactory.CreateProperty(property.Name, attr, property.PropertyType, _handler);

            var getter = new IlBuilder(p.GetMethod.Body);

            getter.DeclareLocal(property.PropertyType);
            getter.LoadArg(0);
            getter.LoadField(_result.Fields[0]);
            getter.Call(_model.Properties[_startIndex].GetMethod);
            getter.SetLoc(0);
            var i = getter.Processor.Create(OpCodes.Ldloc_0);

            getter.Br_S(i);
            getter.Instructions.Add(i);
            getter.Return();
            getter.Append();

            var setter = new IlBuilder(p.SetMethod.Body);

            setter.LoadArg(0);
            setter.LoadField(_result.Fields[0]);
            setter.LoadArg(1);
            setter.Call(_model.Properties[_startIndex].SetMethod);
            setter.Return();
            setter.Append();

            _startIndex++;
            return(p);
        }
 private static void ProcessPropertySetExclude(PropertyInformation pi, IlBuilder processor)
 {
     processor.LoadArg(0);
     processor.LoadArg(1);
     processor.SetField(pi.FieldDefinition);
     processor.Return();
     processor.Append();
 }
Exemple #4
0
        private void GenerateConstructor()
        {
            var method    = new MethodDefinition(".ctor", CtorAttr, _handler.VoidType);
            var processor = new IlBuilder(method.Body);

            processor.LoadArg(0);
            processor.Call(_handler.ModelHandlerBaseTypeCtor);
            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #5
0
        private void GenerateConstructor(TypeDefinition type)
        {
            var method    = new MethodDefinition(".ctor", CtorAttr, _handler.VoidType);
            var processor = new IlBuilder(method.Body);

            processor.LoadArg(0);
            processor.Call(_handler.ObjectTypeCtor);
            processor.Return();
            processor.Append();
            type.Methods.Add(method);
        }
        private void ProcessPropertySetHasOneBelongsToLazyLoad(IlBuilder processor)
        {
            processor.LoadArg(0);
            processor.LoadField(_pi.FieldDefinition);
            processor.LoadArg(1);
            var sv       = _pi.FieldDefinition.FieldType.GetMethod("set_Value");
            var setValue = _handler.Import(sv);

            processor.CallVirtual(setValue);
            processor.Return();
            processor.Append();
        }
Exemple #7
0
        private void GenerateCreateInstance()
        {
            var ctor   = _model.GetConstructor();
            var method = new MethodDefinition("CreateInstance", CtMethodAttr, _handler.ObjectType);

            method.Overrides.Add(_handler.CreateInstance);
            var processor = new IlBuilder(method.Body);

            processor.NewObj(ctor);
            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
 private void ProcessPropertySetNormal(IlBuilder processor)
 {
     processor.Return();
     processor.Append();
     if (ProcessorSettings.AddCompareToSetProperty)
     {
         ProcessPropertySetNormalCompare(processor);
     }
     processor.LoadArg(0);
     processor.LoadArg(1);
     processor.SetField(_pi.FieldDefinition);
     processor.InsertBefore(_pi.PropertyDefinition.SetMethod.Body.Instructions.LastItem());
 }
Exemple #9
0
        private void GenerateSetValue(TypeDefinition type, MemberHandler member, TypeReference memberType)
        {
            var method = new MethodDefinition("SetValue", CtMethodAttr, _handler.VoidType);

            method.Parameters.Add(new ParameterDefinition("obj", ParameterAttributes.None, _handler.ObjectType));
            method.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

            processor.LoadArg(1).Cast(_model).LoadArg(2).CastOrUnbox(memberType, _handler);
            processor.SetMember(member, _handler);
            processor.Return();
            processor.Append();
            type.Methods.Add(method);
        }
Exemple #10
0
        private void GenerateConstructor()
        {
            var method = new MethodDefinition(".ctor", CtorAttr, _handler.VoidType);

            method.Parameters.Add(new ParameterDefinition("owner", ParameterAttributes.None, _model));
            var processor = new IlBuilder(method.Body);

            processor.LoadArg(0);
            processor.Call(_handler.ObjectTypeCtor);
            processor.LoadArg(0);
            processor.LoadArg(1);
            processor.SetField(_result.Fields[0]);
            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #11
0
 private void ProcessSerializable()
 {
     if (_model.IsSerializable && !_model.IsAbstract)
     {
         _model.Interfaces.Add(_handler.SerializableInterface);
         var method = new MethodDefinition("System.Runtime.Serialization.ISerializable.GetObjectData", FlagGetObjectData, _handler.VoidType);
         method.Overrides.Add(_handler.SerializableGetObjectData);
         method.Parameters.Add(new ParameterDefinition("info", ParameterAttributes.None, _handler.SerializationInfoType));
         method.Parameters.Add(new ParameterDefinition("context", ParameterAttributes.None, _handler.StreamingContextType));
         var processor = new IlBuilder(method.Body);
         processor.LoadArg(0).LoadArg(1).LoadArg(2).Call(_handler.DynamicObjectReferenceSerializeObject);
         processor.Return();
         processor.Append();
         _model.Methods.Add(method);
     }
 }
Exemple #12
0
        private void GenerateLoadSimpleValuesByIndex()
        {
            var method = new MethodDefinition("LoadSimpleValuesByIndex", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.LoadSimpleValuesByIndex);
            method.Parameters.Add(new ParameterDefinition("o", ParameterAttributes.None, _handler.ObjectType));
            method.Parameters.Add(new ParameterDefinition("dr", ParameterAttributes.None, _handler.DataReaderInterface));
            var processor = new IlBuilder(method.Body);

            // 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).LoadInt(n);
                var mi1 = Helper.GetMethodInfo(f.MemberType);
                if (f.Is.AllowNull || mi1 == null)
                {
                    processor.CallVirtual(_handler.GetDataReaderMethodInt());
                    if (f.Is.AllowNull)
                    {
                        SetSecendArgForGetNullable(f, processor);
                        processor.Call(_handler.ModelHandlerBaseTypeGetNullable);
                    }
                    // cast or unbox
                    processor.CastOrUnbox(_handler.Import(f.MemberType), _handler);
                }
                else
                {
                    processor.CallVirtual(_handler.Import(mi1));
                }
                processor.SetMember(f, _handler);
                n++;
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #13
0
        private void GenerateGetValue(TypeDefinition type, MemberHandler member, TypeReference memberType)
        {
            var method = new MethodDefinition("GetValue", CtMethodAttr, _handler.ObjectType);

            method.Parameters.Add(new ParameterDefinition("obj", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

            processor.DeclareLocal(_handler.ObjectType);
            processor.LoadArg(1).Cast(_model);
            processor.GetMember(member, _handler);
            if (member.MemberInfo.MemberType.IsValueType)
            {
                processor.Box(memberType);
            }
            processor.SetLoc(0);
            processor.LoadLoc(0);
            processor.Return();
            processor.Append();
            type.Methods.Add(method);
        }
Exemple #14
0
        private void GenerateSetKeyValueDirect()
        {
            var method = new MethodDefinition("SetKeyValueDirect", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.SetKeyValueDirect);
            method.Parameters.Add(new ParameterDefinition("obj", ParameterAttributes.None, _handler.ObjectType));
            method.Parameters.Add(new ParameterDefinition("key", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

            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(_handler.ConvertToInt64);
                }
                else if (fh.MemberType == typeof(int))
                {
                    processor.Call(_handler.ConvertToInt32);
                }
                else if (fh.MemberType == typeof(Guid))
                {
                    processor.Unbox(_handler.Import(h.MemberType));
                }
                else
                {
                    processor.Cast(_handler.Import(h.MemberType));
                }
                processor.SetMember(h, _handler);
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #15
0
        private void GenerateSetValuesForSelectDirectDirect(string methodName, bool noLazy)
        {
            var method = new MethodDefinition(methodName, MethodAttr, _handler.VoidType);

            method.Overrides.Add(noLazy ? _handler.SetValuesForSelectDirectNoLazy : _handler.SetValuesForSelectDirect);
            method.Parameters.Add(new ParameterDefinition("keys", ParameterAttributes.None, _handler.ListKeyValuePairStringStringType));
            var processor = new IlBuilder(method.Body);

            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(_handler.KeyValuePairStringStringCtor);

                        processor.CallVirtual(_handler.ListKeyValuePairStringStringAdd);
                    }
                }
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #16
0
        private void GenerateGetKeyValuesDirect()
        {
            var method = new MethodDefinition("GetKeyValuesDirect", MethodAttr, _handler.VoidType);

            method.Overrides.Add(_handler.GetKeyValuesDirect);
            method.Parameters.Add(new ParameterDefinition("dic", ParameterAttributes.None, _handler.DictionaryStringObjectType));
            method.Parameters.Add(new ParameterDefinition("o", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

            // 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, _handler);
                processor.Box(_handler.Import(f.MemberType)).CallVirtual(_handler.DictionaryStringObjectAdd);
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #17
0
        private void GenerateGetKeyValueDirect()
        {
            var method = new MethodDefinition("GetKeyValueDirect", MethodAttr, _handler.ObjectType);

            method.Overrides.Add(_handler.GetKeyValueDirect);
            method.Parameters.Add(new ParameterDefinition("o", ParameterAttributes.None, _handler.ObjectType));
            var processor = new IlBuilder(method.Body);

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

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }
Exemple #18
0
 public static void ProcessPropertySetElse(IlBuilder processor)
 {
     processor.Return();
     processor.Append();
 }
Exemple #19
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 = new MethodDefinition(methodName, MethodAttr, _handler.VoidType);

            method.Overrides.Add(useIndex
                ? (noLazy ? _handler.LoadRelationValuesByIndexNoLazy : _handler.LoadRelationValuesByIndex)
                : (noLazy ? _handler.LoadRelationValuesByNameNoLazy : _handler.LoadRelationValuesByName));
            method.Parameters.Add(new ParameterDefinition("o", ParameterAttributes.None, _handler.ObjectType));
            method.Parameters.Add(new ParameterDefinition("dr", ParameterAttributes.None, _handler.DataReaderInterface));
            var processor = new IlBuilder(method.Body);

            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, _handler);
                            processor.LoadArg(2);
                            if (useIndex)
                            {
                                processor.LoadInt(index++).CallVirtual(_handler.GetDataReaderMethodInt());
                            }
                            else
                            {
                                processor.LoadString(f.Name).CallVirtual(_handler.GetDataReaderMethodString());
                            }
                            processor.LoadInt(0);
                            processor.CallVirtual(_handler.LazyLoadingInterfaceWrite);
                        }
                    }
                    else if (f.Is.BelongsTo)
                    {
                        processor.LoadLoc(0);
                        processor.GetMember(f, _handler);
                        processor.LoadArg(2);
                        if (useIndex)
                        {
                            processor.LoadInt(index++).CallVirtual(_handler.GetDataReaderMethodInt());
                        }
                        else
                        {
                            processor.LoadString(f.Name).CallVirtual(_handler.GetDataReaderMethodString());
                        }
                        processor.CallVirtual(_handler.BelongsToInterfaceSetForeignKey);
                    }
                }
            }

            processor.Return();
            processor.Append();
            _result.Methods.Add(method);
        }