public IEnumerable<AbstractField> Index(object val, PropertyDescriptorCollection properties, Field.Store defaultStorage) { return from property in properties.Cast<PropertyDescriptor>() where property.Name != Constants.DocumentIdFieldName from field in CreateFields(property.Name, property.GetValue(val), defaultStorage) select field; }
public static async Task <int> AddUpdateRecord <T>(WebApiProxy proxy, T ModelObject, bool IsNewObject = false) where T : class { Dictionary <string, string> ProcedureInfo = GetProcedureInfo(ModelObject); System.ComponentModel.PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties(ModelObject); Dictionary <string, object> dicParams = pdc.Cast <PropertyDescriptor>().Where(item => ProcedureInfo.ContainsKey(item.Name)).ToDictionary(key => ProcedureInfo[key.Name], value => value.GetValue(ModelObject)); if (IsNewObject) { dicParams["@Id"] = -1; } if (dicParams.ContainsKey("@Logo")) { dicParams["@Logo"] = 1014; } DataSet dataSet = await proxy.ExecuteDataset(ProcedureInfo["ProcedureName"], dicParams); if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count > 0) { if (dataSet.Tables[0].Rows != null && dataSet.Tables[0].Rows.Count > 0) { int ID = Convert.ToInt32(dataSet.Tables[0].Rows[0][0].ToString()); return(ID); } } return(-1); }
private void getModel <TModel>(TModel result, System.ComponentModel.PropertyDescriptorCollection props, string parentContext) where TModel : class, new() { // loop each property in the passed in model foreach (var p in props.Cast <PropertyDescriptor>()) { // get the property name... if we are in a hierarchal model structure, prepend the parent's name and seperate with an underscore var propName = string.IsNullOrEmpty(parentContext) ? p.Name : parentContext + '_' + p.Name; try { // test to see if this is a nested model of base type "DnsPluginModel" if ((p.PropertyType.BaseType != null) && (p.PropertyType.BaseType.Name.Equals("DnsPluginModel"))) { // yes, get its child properties var childProps = p.GetChildProperties(); if (childProps.Count > 0) { // get the actual instance (NOT the type!) of the current result's named (parentContext) child model that we just // got child properties for so we can pass it in as the "subresult" DnsPluginModel childInstanceSubresult = result.GetType().GetProperty(p.Name).GetValue(result, null) as DnsPluginModel; if (childInstanceSubresult != null) { getModel <DnsPluginModel>(childInstanceSubresult, childProps, propName); } } } } catch { continue; } // pull out the named (propName) value from the response's values, also determine if validation should be skipped for the property. //skipping validation should only be done when the field contains html fragments. bool skipValidation = false; foreach (var att in p.Attributes) { if (att is Lpp.Objects.ValidationAttributes.SkipHttpValidationAttribute) { skipValidation = true; break; } } var v = ((System.Web.Mvc.ValueProviderCollection)Values).GetValue(propName, skipValidation); if (v == null) { continue; } // convert the value v to the target result's property's type object value; try { value = v.ConvertTo(p.PropertyType); } catch { continue; } // given the property p, plug the value into the result (into the result's property) p.SetValue(result, value); } }
public static IEnumerable<AbstractField> Index(object val, PropertyDescriptorCollection properties, IndexDefinition indexDefinition, Field.Store defaultStorage) { return (from property in properties.Cast<PropertyDescriptor>() let name = property.Name where name != Constants.DocumentIdFieldName let value = property.GetValue(val) from field in CreateFields(name, value, indexDefinition, defaultStorage) select field); }
public static IEnumerable<AbstractField> Index(object val, PropertyDescriptorCollection properties, IndexDefinition indexDefinition, Field.Store defaultStorage) { return (from property in properties.Cast<PropertyDescriptor>() let name = property.Name where name != "__document_id" let value = property.GetValue(val) where value != null select Createfield(name, value, indexDefinition, defaultStorage)); }
public IEnumerable<Field> Index(object val, PropertyDescriptorCollection properties, IndexDefinition indexDefinition) { return (from property in properties.Cast<PropertyDescriptor>() let name = property.Name where name != "__document_id" let value = property.GetValue(val) where value != null select new Field(name, ToIndexableString(value, indexDefinition.GetIndex(name)), indexDefinition.GetStorage(name), indexDefinition.GetIndex(name))); }
PropertyDescriptorCollection Patch(PropertyDescriptorCollection original) { return new PropertyDescriptorCollection(original.Cast<PropertyDescriptor>().Select(x => { var control = instance as Control; if(control != null && (BindingOperations.IsBoundProperty(control, x) || !SerializeHelper.CanSerializeProperty(control, x))) return new BoundPropertyDescriptor(x); return x; }).ToArray()); }
PropertyDescriptorCollection Filter(PropertyDescriptorCollection properties) { PropertyDescriptor property = properties[_parent._propertyName]; if (property != null) { if ((properties as System.Collections.IDictionary).IsReadOnly) { properties = new PropertyDescriptorCollection(properties.Cast<PropertyDescriptor>().ToArray()); } properties.Remove(property); properties.Add(new ShadowPropertyDescriptor(_parent, property)); } return properties; }
public static int Delete <T>(WebApiProxy proxy, T ModelObject) where T : class { try { Dictionary <string, string> ProcedureInfo = GetProcedureInfo(ModelObject, true); System.ComponentModel.PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties(ModelObject); Dictionary <string, object> dicParams = pdc.Cast <PropertyDescriptor>().Where(item => ProcedureInfo.ContainsKey(item.Name)).ToDictionary(key => ProcedureInfo[key.Name], value => value.GetValue(ModelObject)); proxy.ExecuteNonQuery(ProcedureInfo["ProcedureName"], dicParams); return(1); } catch (SqlException ex) { throw new HttpException(500, ex.ToString()); } }
/// <summary> /// This method validates the association attributes for the specified entity type /// </summary> /// <param name="entityType">Type of entity to validate its association attributes for</param> /// <param name="entityProperties">collection of entity property descriptors</param> private void ValidateEntityAssociations(Type entityType, PropertyDescriptorCollection entityProperties) { foreach (PropertyDescriptor pd in entityProperties) { // validate the association attribute (if any) AssociationAttribute assocAttrib = pd.Attributes[typeof(AssociationAttribute)] as AssociationAttribute; if (assocAttrib == null) { continue; } string assocName = assocAttrib.Name; if (string.IsNullOrEmpty(assocName)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_NameCannotBeNullOrEmpty, pd.Name, entityType)); } if (string.IsNullOrEmpty(assocAttrib.ThisKey)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_StringCannotBeNullOrEmpty, assocName, entityType, "ThisKey")); } if (string.IsNullOrEmpty(assocAttrib.OtherKey)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_StringCannotBeNullOrEmpty, assocName, entityType, "OtherKey")); } // The number of keys in 'this' and 'other' must be the same if (assocAttrib.ThisKeyMembers.Count() != assocAttrib.OtherKeyMembers.Count()) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_Key_Count_Mismatch, assocName, entityType, assocAttrib.ThisKey, assocAttrib.OtherKey)); } // check that all ThisKey members exist on this entity type foreach (string thisKey in assocAttrib.ThisKeyMembers) { if (entityProperties[thisKey] == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_ThisKeyNotFound, assocName, entityType, thisKey)); } } // Verify that the association name is unique. In inheritance scenarios, self-referencing associations // on the base type should be inheritable by the derived types. Type otherEntityType = TypeUtility.GetElementType(pd.PropertyType); int otherMemberCount = entityProperties.Cast<PropertyDescriptor>().Count(p => p.Name != pd.Name && p.Attributes.OfType<AssociationAttribute>().Any(a => a.Name == assocAttrib.Name)); bool isSelfReference = otherEntityType.IsAssignableFrom(entityType); if ((!isSelfReference && otherMemberCount > 0) || (isSelfReference && otherMemberCount > 1)) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_NonUniqueAssociationName, assocName, entityType)); } // Verify that the type of FK associations return singletons. if (assocAttrib.IsForeignKey && (otherEntityType != pd.PropertyType)) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Resource.InvalidAssociation_FKNotSingleton, assocName, entityType)); } // Associations are not allowed to be marked as [Required], because we don't guarantee // that we set the association on the server. In many cases it's possible that we simply // associate entities based on FKs. if (pd.Attributes[typeof(RequiredAttribute)] != null) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resource.Entity_RequiredAssociationNotAllowed, entityType, pd.Name)); } // Throw if the association member has a explicit RoundtripOriginalAttribute on it if (pd.ExplicitAttributes()[typeof(RoundtripOriginalAttribute)] != null) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_RoundTripOriginal, pd.Name, entityType)); } // if the other entity is also exposed by the service, perform additional validation if (this._entityTypes.Contains(otherEntityType)) { PropertyDescriptorCollection otherEntityProperties = TypeDescriptor.GetProperties(otherEntityType); PropertyDescriptor otherMember = otherEntityProperties.Cast<PropertyDescriptor>().FirstOrDefault(p => p.Name != pd.Name && p.Attributes.OfType<AssociationAttribute>().Any(a => a.Name == assocName)); if (otherMember != null) { // Bi-directional association // make sure IsForeignKey is set to true on one and only one side of the association AssociationAttribute otherAssocAttrib = (AssociationAttribute)otherMember.Attributes[typeof(AssociationAttribute)]; if (otherAssocAttrib != null && !((assocAttrib.IsForeignKey != otherAssocAttrib.IsForeignKey) && (assocAttrib.IsForeignKey || otherAssocAttrib.IsForeignKey))) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_IsFKInvalid, assocName, entityType)); } Type otherMemberEntityType = TypeUtility.GetElementType(otherMember.PropertyType); // Verify that the type of the corresponding association points back to this entity // The type of the corresponding association can be one of the parents of the entity, but it cannot be one of its children. if (!otherMemberEntityType.IsAssignableFrom(entityType)) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Resource.InvalidAssociation_TypesDoNotAlign, assocName, entityType, otherEntityType)); } } // check that the OtherKey members exist on the other entity type foreach (string otherKey in assocAttrib.OtherKeyMembers) { if (otherEntityProperties[otherKey] == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.InvalidAssociation_OtherKeyNotFound, assocName, entityType, otherKey, otherEntityType)); } } } else { // Disallow attempts to place [Association] on simple types if (TypeUtility.IsPredefinedType(otherEntityType)) { // Association attributes cannot be attached to properties whose types are not entities throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.Association_Not_Entity_Type, pd.Name, entityType.Name, otherEntityType.Name)); } } } }