private static string GetOrderByString(PropertyInformation pi) { var pd = pi.PropertyDefinition; CustomAttribute ca; switch (pi.FieldType) { case FieldType.HasOne: ca = pd.GetCustomAttribute(KnownTypesHandler.HasOneAttribute); break; case FieldType.HasMany: ca = pd.GetCustomAttribute(KnownTypesHandler.HasManyAttribute); break; case FieldType.HasAndBelongsToMany: ca = pd.GetCustomAttribute(KnownTypesHandler.HasAndBelongsToManyAttribute); break; default: throw new ApplicationException("Impossiable"); } var value = (string)ca.GetField("OrderBy"); return(value); }
public TypeDefinition Generate() { _startIndex = _model.Properties.Count; foreach (var property in _composedOfType.Properties) { var name = "$" + _instanceName + "$" + property.Name; var dbn = _instanceName + property.Name; var pd = TypeFactory.CreateProperty(name, PropAttr, property.PropertyType, _handler); foreach (var attribute in property.CustomAttributes) { pd.CustomAttributes.Add(attribute); } if (pd.GetCustomAttribute(KnownTypesHandler.DbColumnAttribute) == null) { pd.CustomAttributes.Add(_handler.GetDbColumn(dbn)); } _model.Properties.Add(pd); _model.Methods.Add(pd.GetMethod); _model.Methods.Add(pd.SetMethod); var pi = new PropertyInformation { PropertyDefinition = pd, FieldType = FieldType.Normal }; var pp = new PropertyProcessor(pi, _model, _handler); pp.Process(); } GenerateClass(); return(_result); }
private static void ProcessPropertySetExclude(PropertyInformation pi, IlBuilder processor) { processor.LoadArg(0); processor.LoadArg(1); processor.SetField(pi.FieldDefinition); processor.Return(); processor.Append(); }
private void ProcessProperty(PropertyInformation pi) { var pp = new PropertyProcessor(pi, _model, _handler); pp.Process(); if (pi.IsComposedOf) { ProcessComposedOfAttribute(pi); } }
private static void CheckIsPublicGetAndPrivateSet(PropertyInformation pi) { if (pi.PropertyDefinition.SetMethod.IsPublic) { Throw(pi, "The setter of property [{0}] should be private. Example:\npublic string Name { get; private set; }"); } if (!pi.PropertyDefinition.GetMethod.IsPublic) { Throw(pi, "The getter of property [{0}] should be public"); } }
private void CheckNormalProperty(PropertyInformation pi) { var t = pi.PropertyDefinition.PropertyType; if (!t.IsValueType && !t.IsArray && t.FullName != KnownTypesHandler.String) { if (!pi.IsExclude && !pi.IsComposedOf && (pi.FieldType == FieldType.Normal || pi.FieldType == FieldType.LazyLoad)) { Throw(pi, "The property '{0}' should define as relation field and can not set lazy load attribute"); } } }
private void ProcessComposedOfAttribute(PropertyInformation pi) { var gen = new ComposedOfClassGenerator(_model, pi, _handler); _coTypes.Add(new KeyValuePair <TypeDefinition, FieldDefinition>(gen.Generate(), pi.FieldDefinition)); foreach (var attribute in pi.PropertyDefinition.CustomAttributes) { if (attribute.AttributeType.FullName == KnownTypesHandler.ComposedOfAttribute) { pi.PropertyDefinition.CustomAttributes.Remove(attribute); break; } } pi.PropertyDefinition.CustomAttributes.Add(_handler.GetExclude()); }
private static void CheckComposedOfProperty(PropertyInformation pi) { if (pi.IsComposedOf) { CheckIsPublicGetAndPrivateSet(pi); var t = pi.PropertyDefinition.PropertyType; if (t.IsValueType || t.IsArray || t.FullName == KnownTypesHandler.String) { Throw(pi, "Property [{0}] mark as ComposedOf but the type is wrong"); } if (pi.FieldType != FieldType.Normal) { Throw(pi, "Property [{0}] could not mark as ComposedOf and relations"); } } }
public ComposedOfClassGenerator(TypeDefinition model, PropertyInformation pi, KnownTypesHandler handler) { this._composedOfType = pi.PropertyDefinition.PropertyType.Resolve(); if (!_composedOfType.IsInterface) { throw new DataException("ComposedOf type must be interface."); } if (!_composedOfType.HasProperties) { throw new DataException("ComposedOf type must has properties."); } this._model = model; this._handler = handler; this._instanceName = pi.PropertyDefinition.Name; _result = TypeFactory.CreateType(handler, model, _composedOfType); }
private static void CheckPropertyAccessable(PropertyInformation pi) { if (pi.IsSpecialForeignKey) { CheckIsPublicGetAndPrivateSet(pi); } else if (pi.FieldType == FieldType.HasMany || pi.FieldType == FieldType.HasAndBelongsToMany) { if (!pi.PropertyDefinition.PropertyType.Name.StartsWith("IList`1")) { Throw(pi, "Property [{0}] should be IList<T>"); } CheckIsPublicGetAndPrivateSet(pi); } else if (!pi.IsComposedOf) { if (!pi.PropertyDefinition.SetMethod.IsPublic || !pi.PropertyDefinition.GetMethod.IsSpecialName) { Throw(pi, "The getter/setter of property [{0}] should be public"); } } }
private void ProcessGenericPropertyInConstructor(PropertyInformation pi, IlBuilder processor) { processor.LoadArg(0).LoadArg(0); MethodReference ci1; var ft = (GenericInstanceType)pi.FieldDefinition.FieldType; if (pi.IsHasOne || pi.IsHasMany || pi.IsHasAndBelongsToMany) { ci1 = ft.GetConstructor(typeof(DbObjectSmartUpdate), typeof(string), typeof(string)); var ob = GetOrderByString(pi); if (string.IsNullOrEmpty(ob)) { processor.LoadNull(); } else { processor.LoadString(ob); } } else { ci1 = ft.GetConstructor(typeof(DbObjectSmartUpdate), typeof(string)); } if (pi.IsLazyLoad) { processor.LoadString(pi.ColumnName); } else { processor.Nop().Nop().Nop().Nop().LoadString(pi.FieldDefinition.Name); } var ctor = _handler.Import(ci1); ctor.DeclaringType = ft; //NOTE: might be a bug of Cecil processor.NewObj(ctor); processor.SetField(pi.FieldDefinition); }
private void ProcessSpecialForeignKey(PropertyInformation property) { // process set var processor = PropertyProcessor.PreProcessPropertyMethod(property.PropertyDefinition.SetMethod); PropertyProcessor.ProcessPropertySetElse(processor); // remove field _model.Fields.Remove(property.FieldDefinition); // process get var name = property.PropertyDefinition.Name; name = name.Substring(0, name.Length - 2); var pi = FindProperty(name); processor = PropertyProcessor.PreProcessPropertyMethod(property.PropertyDefinition.GetMethod); processor.LoadArg(0); processor.LoadField(pi.FieldDefinition); processor.CallVirtual(_handler.BelongsToInterfaceGetForeignKey); processor.CastOrUnbox(property.PropertyDefinition.PropertyType, _handler); processor.Return(); processor.Append(); // add exclude property.PropertyDefinition.CustomAttributes.Add(_handler.GetExclude()); }
public PropertyProcessor(PropertyInformation pi, TypeDefinition model, KnownTypesHandler handler) { this._pi = pi; this._model = model; this._handler = handler; }
private void CheckProperty(PropertyInformation pi) { CheckNormalProperty(pi); CheckComposedOfProperty(pi); CheckPropertyAccessable(pi); }
private static void Throw(PropertyInformation pi, string stringTemplate) { throw new DataException(string.Format(stringTemplate, pi.PropertyDefinition.Name)); }