public void GetDtoAttributes()
        {
            var dtoAttrs = DtoAttribute.GetDtoAttributes(typeof(DummyViewModel));

            Assert.IsNotNull(dtoAttrs);
            Assert.IsFalse(dtoAttrs.Any());

            dtoAttrs = DtoAttribute.GetDtoAttributes(typeof(Dummy2ViewModel));
            Assert.IsNotNull(dtoAttrs);
            Assert.IsTrue(dtoAttrs.Any());
            Assert.IsTrue(dtoAttrs.Count() == 1);
            Assert.IsNotNull(dtoAttrs.Single().Type);
            Assert.AreEqual(dtoAttrs.Single().Type, typeof(string));

            dtoAttrs = DtoAttribute.GetDtoAttributes(typeof(Dummy3ViewModel));
            Assert.IsNotNull(dtoAttrs);
            Assert.IsTrue(dtoAttrs.Any());
            Assert.IsTrue(dtoAttrs.Count() == 1);
            Assert.AreEqual(dtoAttrs.Single().Type, typeof(AwesomeTransferDataObject));

            var dtoPropAttrs = DtoPropertyAttribute.GetDtoPropertyAttributes(typeof(Dummy3ViewModel).GetProperty(nameof(Dummy3ViewModel.NonExistent)) !);

            Assert.IsNotNull(dtoPropAttrs);
            Assert.IsTrue(dtoAttrs.Any());
            Assert.IsTrue(dtoAttrs.Count() == 1);
            Assert.AreEqual("NotExistingProperty", dtoPropAttrs.Single().PropertyName);
        }
Example #2
0
        private Table LoadFromMetadata(Type type)
        {
            TableAttribute tableAttr = type.GetCustomAttributes(true).OfType <TableAttribute>().FirstOrDefault();
            string         tname;

            if (tableAttr == null)
            {
                DtoAttribute dto = type.GetCustomAttributes(true).OfType <DtoAttribute>().FirstOrDefault();
                if (dto == null)
                {
                    throw new ArgumentException(string.Format("类型{0}不包含表信息", type));
                }
                else
                {
                    tname = type.Name;
                }
            }
            else
            {
                tname = tableAttr.Name;
            }

            string key = string.Format(columnAttrMapTableName, tname);

            return(tableCache.GetOrAdd(key, () => this.GetTableFromAttribute(tname, type)));
        }
Example #3
0
        /// <summary>
        /// Processes the type if it's a DTO
        /// </summary>
        /// <param name="type">The type.</param>
        private void ProcessDto(Type type)
        {
            DtoAttribute dtoAttribute = AttributeUtil.GetAttributeFrom <DtoAttribute>(type);

            if (dtoAttribute != null)
            {
                Type entityType = null;
                if (dtoAttribute.EntityType != null)
                {
                    entityType = dtoAttribute.EntityType;
                }
                else
                {
                    string entityName = dtoAttribute.EntityName;
                    if (string.IsNullOrEmpty(entityName))
                    {
                        entityName = this.GetEntityName(type.Name);
                    }
                    entityType = TypesManager.ResolveType(entityName);
                    if (entityType == null)
                    {
                        if (this.typeMetatables.ContainsKey(entityName.ToLower()))
                        {
                            entityName = this.typeMetatables[entityName.ToLower()].FullName;
                            entityType = TypesManager.ResolveType(entityName);
                        }
                    }
                }
                if (entityType != null)
                {
                    IMetamodelEntity metaEntity = this.Parent.MetamodelManager.RegisterEntity(entityType, type);
                    if (this.Metatables.ContainsKey(entityType.FullName))
                    {
                        MetaPersistentType metaType = this.Metatables[entityType.FullName];
                        Dictionary <MetaColumn, MetaMember> mapColumns = new Dictionary <MetaColumn, MetaMember>();
                        foreach (MetaMember member in metaType.Members)
                        {
                            MetaPrimitiveMember primitiveMember = member as MetaPrimitiveMember;
                            if (primitiveMember != null)
                            {
                                mapColumns.Add(primitiveMember.Column, member);
                            }
                        }
                        MetaTable metaTable = metaType.Table;
                        foreach (MetaColumn metaColumn in metaTable.Columns)
                        {
                            MetaMember member = mapColumns[metaColumn];
                            metaEntity.AddField(member.Name, metaColumn.IsPrimaryKey, !metaColumn.IsPrimaryKey);
                        }
                    }
                }
            }
        }
        private PropertyMeta.ClassType BuildClassType(Type propertyClass, DtoAttribute annotation)
        {
            PropertyMeta.ClassType propertyClassType = PropertyMeta.ClassType.Value;
            if (typeof(string).IsAssignableFrom(propertyClass))
            {
                propertyClassType = PropertyMeta.ClassType.Value;
            }
            else if (typeof(Dtobase).IsAssignableFrom(propertyClass))
            {
                propertyClassType = annotation.IsCustomType ? PropertyMeta.ClassType.CustomType : PropertyMeta.ClassType.Class;
            }
            else if (typeof(IEnumerable).IsAssignableFrom(propertyClass))
            {
                propertyClassType = annotation.IsCustomType ? PropertyMeta.ClassType.CustomTypeCollection : PropertyMeta.ClassType.Collection;
            }

            return(propertyClassType);
        }
        /// <summary>
        /// Creates an instance of PropertyMeta class based on property descriptor
        /// </summary>
        /// <param name="descriptor">property descriptor</param>
        /// <returns>instance of PropertyMeta class</returns>
        public PropertyMeta Create(PropertyInfo descriptor)
        {
            PropertyMeta meta       = new PropertyMeta(descriptor);
            DtoAttribute annotation = descriptor.GetCustomAttribute <DtoAttribute>();

            if (null != annotation)
            {
                meta.Valid             = true;
                meta.DtoLevel          = annotation.Value;
                meta.DtoNestedLevel    = annotation.Nested;
                meta.DtoCustomType     = annotation.IsCustomType;
                meta.DtoDynamic        = annotation.Dynamic;
                meta.PropertyClassType = this.BuildClassType(meta.PropertyClass, annotation);
                meta.InnerGenericClass = this.BuildNestedGenericClass(meta.PropertyClass);
                meta.Parser            = this.context.BuildParser(meta.Name, meta.PropertyClass, meta.PropertyClassType, annotation);
            }

            return(meta);
        }
Example #6
0
        public void Attributes()
        {
            var dtoAttr = DtoAttribute.GetDtoAttribute(typeof(DummyViewModel));

            Assert.IsNull(dtoAttr);

            dtoAttr = DtoAttribute.GetDtoAttribute(typeof(Dummy2ViewModel));
            Assert.IsNotNull(dtoAttr);
            Assert.IsNotNull(dtoAttr !.Type);
            Assert.AreEqual(dtoAttr.Type, typeof(string));

            dtoAttr = DtoAttribute.GetDtoAttribute(typeof(Dummy3ViewModel));
            Assert.IsNotNull(dtoAttr);
            Assert.AreEqual(dtoAttr !.Type, typeof(AwesomeTransferDataObject));

            var dtoPropAttr = DtoPropertyAttribute.GetDtoPropertyAttribute(typeof(Dummy3ViewModel).GetProperty(nameof(Dummy3ViewModel.NonExistent)) !);

            Assert.IsNotNull(dtoPropAttr);
            Assert.AreEqual(dtoPropAttr !.PropertyName, "NotExistingProperty");
        }
        public void GetDtoAttributesMultiple()
        {
            var dtoAttrs = DtoAttribute.GetDtoAttributes(typeof(Dummy4ViewModel));

            Assert.IsNotNull(dtoAttrs);
            Assert.IsTrue(dtoAttrs.Any());
            Assert.IsTrue(dtoAttrs.Count() == 2);

            var awesomeDtoAttr = dtoAttrs.FirstOrDefault(attr => attr.Type == typeof(AwesomeTransferDataObject));

            Assert.IsNotNull(awesomeDtoAttr);

            var averageDtoAttr = dtoAttrs.FirstOrDefault(attr => attr.Type == typeof(AverageTransferDataObject));

            Assert.IsNotNull(awesomeDtoAttr);

            var dtoPropAttrs = DtoPropertyAttribute.GetDtoPropertyAttributes(typeof(Dummy4ViewModel).GetProperty(nameof(Dummy4ViewModel.PersonAge)) !);

            Assert.IsNotNull(dtoPropAttrs);
            Assert.IsTrue(dtoPropAttrs.Any());
            Assert.IsTrue(dtoPropAttrs.Count() == 1);
            Assert.IsNotNull(dtoPropAttrs.SingleOrDefault(attr => attr.Type == typeof(AwesomeTransferDataObject)));
        }
Example #8
0
        /// <summary>
        /// Creates a property parser instance based on property class
        /// </summary>
        /// <param name="name">property name</param>
        /// <param name="propertyClass">property class</param>
        /// <param name="propertyClassType">property class type</param>
        /// <param name="annotation">DtoAttribute instance</param>
        /// <returns>parser instance</returns>
        public IParser BuildParser(string name, Type propertyClass, PropertyMeta.ClassType propertyClassType, DtoAttribute annotation)
        {
            if (PropertyMeta.ClassType.Value != propertyClassType)
            {
                return(null);
            }

            IParser parser = null;

            try
            {
                if (null != annotation.Parser)
                {
                    parser = (IParser)Activator.CreateInstance(annotation.Parser);
                    return(parser);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Unable to instantiate parser for - property:" + name + " of type:" + propertyClass.Name, ex);
            }

            Type type = propertyClass;

            if (this.STRING_TYPE == type)
            {
                parser = StandardParsers.STRING_PARSER;
            }
            else if (this.LONG_TYPE == type || this.LONG_PRIM == type)
            {
                parser = StandardParsers.LONG_PARSER;
            }
            else if (this.ULONG_TYPE == type || this.ULONG_PRIM == type)
            {
                parser = StandardParsers.ULONG_PARSER;
            }
            else if (this.INTEGER_TYPE == type || this.INTEGER_PRIM == type)
            {
                parser = StandardParsers.INT_PARSER;
            }
            else if (this.UINTEGER_TYPE == type || this.UINTEGER_PRIM == type)
            {
                parser = StandardParsers.UINT_PARSER;
            }
            else if (this.DOUBLE_TYPE == type || this.DOUBLE_PRIM == type)
            {
                parser = StandardParsers.DOUBLE_PARSER;
            }
            else if (this.DATE_TYPE == type)
            {
                parser = StandardParsers.DATETIME_PARSER;
            }
            else if (this.BOOLEAN_TYPE == type || this.BOOLEAN_PRIM == type)
            {
                parser = StandardParsers.BOOL_PARSER;
            }
            else if (this.SHORT_TYPE == type || this.SHORT_PRIM == type)
            {
                parser = StandardParsers.SHORT_PARSER;
            }
            else if (this.BYTE_TYPE == type || this.BYTE_PRIM == type)
            {
                parser = StandardParsers.BYTE_PARSER;
            }
            else if (this.SBYTE_TYPE == type || this.SBYTE_PRIM == type)
            {
                parser = StandardParsers.SBYTE_PARSER;
            }
            else if (this.FLOAT_TYPE == type || this.FLOAT_PRIM == type)
            {
                parser = StandardParsers.FLOAT_PARSER;
            }
            else if (this.CHAR_TYPE == type || this.CHAR_PRIM == type)
            {
                parser = StandardParsers.CHAR_PARSER;
            }
            else if (type.IsEnum)
            {
                parser = StandardParsers.ENUM_PARSER;
            }
            else if (this.GUID_TYPE == type || this.GUID_PRIM == type)
            {
                parser = StandardParsers.GUID_PARSER;
            }
            else if (type.GenericTypeArguments != null && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments[0].IsEnum)
            {
                parser = StandardParsers.ENUM_NULLABLE_PARSER;
            }
            else
            {
                throw new ApplicationException(
                          "Unable to find parser for property: " + name + ", of type: " + type.FullName +
                          ". Property class either must derive from Dtobase or You must setup custom parser for this property in the Dto attribute.");
            }

            return(parser);
        }