Example #1
0
        private PropertyInfo GetAdhocProperty(Type type, string name)
        {
            var props = type.GetProperties();

            foreach (PropertyInfo prop in props)
            {
                if (prop.Name == name)
                {
                    return(prop);
                }
                else
                {
                    var jsonNetAttrs = prop.GetCustomAttributes(JsonNetMetadata.JsonPropertyAttributeType, false);
                    if (jsonNetAttrs.Any())
                    {
                        var nameFromAttr = JsonNetMetadata.GetPropertyNameFromAttribute(jsonNetAttrs[0]);
                        if (nameFromAttr == name)
                        {
                            return(prop);
                        }
                    }
                }
            }

            return(null);
        }
Example #2
0
        internal DtoType(Type dtoType, EntityType concreteType)
        {
            this.Type = dtoType;
            this.ConcreteEntityType = concreteType;
            this.Fields             = new List <DtoField>();
            this.Links = new List <DtoLink>();

            //// walk...
            //foreach (DtoFieldAttribute attr in type.GetCustomAttributes(typeof(DtoFieldAttribute), true))
            //{
            //    var useName = attr.Name;
            //    if (attr.Name == IdKey)
            //        useName = this.ConcreteEntityType.GetKeyFields()[0].Name;

            //    var prop = props.Where(v => string.Compare(v.Name, useName, true) == 0).FirstOrDefault();
            //    if (prop == null)
            //        throw new InvalidOperationException(string.Format("Property '{0}' was not found on type '{1}'.", attr.Name, concreteType.Name));

            //    // add...
            //    this.Fields.Add(new DtoField(useName, attr.JsonName, prop));
            //}

            // look for any properties that have json properties...
            var dtoProps = dtoType.GetProperties();

            foreach (var dtoProp in dtoProps)
            {
                try
                {
                    // get...
                    var attrs = (DtoFieldAttribute[])dtoProp.GetCustomAttributes(typeof(DtoFieldAttribute), false);
                    if (attrs.Any())
                    {
                        var member = concreteType.GetMember(dtoProp.Name, OnNotFound.ThrowException);
                        this.Fields.Add(new DtoEntityMemberField(dtoProp.Name, attrs[0].JsonName, dtoProp, member));
                    }
                    else
                    {
                        var linkAttrs = (DtoLinkAttribute[])dtoProp.GetCustomAttributes(typeof(DtoLinkAttribute), false);
                        if (linkAttrs.Any())
                        {
                            var link = (ChildToParentEntityLink)concreteType.Links.GetLink(dtoProp.Name, OnNotFound.ThrowException);
                            this.Links.Add(new DtoLink(dtoProp.Name, linkAttrs[0].JsonName, dtoProp, link));
                        }
                        else
                        {
                            var jsonNetAttrs = dtoProp.GetCustomAttributes(JsonNetMetadata.JsonPropertyAttributeType, false);
                            if (jsonNetAttrs.Any())
                            {
                                var name = JsonNetMetadata.GetPropertyNameFromAttribute(jsonNetAttrs[0]);
                                this.Fields.Add(new DtoAdHocField(dtoProp.Name, name, dtoProp));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException(string.Format("Failed when processing '{0}'.", dtoProp.Name), ex);
                }
            }

            //  fixup the links...
            foreach (var link in this.Links)
            {
                var field    = link.Link.GetLinkFields()[0];
                var dtoField = this.GetFieldByName(field.Name, false);
                if (dtoField == null)
                {
                    throw new InvalidCastException(string.Format("DTO link '{0}' on '{1}' is non-functional as the referred field '{2}' is not defined on the DTO.",
                                                                 link.Name, this.Type.Name, field.Name));
                }

                // set...
                link.ReferenceField = dtoField;
            }
        }