示例#1
0
		public void Create()
		{

			var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
			foreach (var propertyInfo in propertyInfos)
			{
				var ignoreAttributes = propertyInfo.GetCustomAttributes(typeof(IgnorePersistentAttribute), true);
				if (ignoreAttributes.Length > 0)
				{
					continue;
				}
				allPropertiesName.Add(propertyInfo.Name);
				var propertyType = propertyInfo.PropertyType;
				if (propertyType.IsPrimitive || propertyType == typeof(DateTime) || propertyType == typeof(decimal) || propertyType == typeof(string))
				{
					var mappingInfo = new MappingInfo() { PropertyInfo = propertyInfo };
					mappingInfo.MappingField = propertyInfo.Name;
					if (propertyInfo.Name == "Id")
					{
						mappingInfo.AutoIncrement = true;
						mappingInfo.NotNull = true;
					}
					else
					{
						if (propertyType == typeof(byte) || propertyType == typeof(sbyte) || propertyType == typeof(bool)
						|| propertyType == typeof(short) || propertyType == typeof(ushort)
						|| propertyType == typeof(int) || propertyType == typeof(uint)
						|| propertyType == typeof(float) || propertyType == typeof(long)
						|| propertyType == typeof(ulong) || propertyType == typeof(double)
						|| propertyType == typeof(decimal))
						{
							mappingInfo.DefaultValue = 0;
						}
						else if (propertyType == typeof(char))
						{
							mappingInfo.DefaultValue = "\'\0\'";
						}
						else if (propertyType == typeof(DateTime))
						{
							mappingInfo.DefaultValue = "\'0001-01-01 12:00:00\'";
						}
						else if (propertyType == typeof(string))
						{
							mappingInfo.DefaultValue = "\'\'";
						}
					}
					var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
					if (propertyAttributes.Length > 0)
					{
						var propertyAttribute = (PropertyAttribute)propertyAttributes[0];
						mappingInfo.MappingField = string.IsNullOrEmpty(propertyAttribute.MappingColumn)
													? propertyInfo.Name
													: propertyAttribute.MappingColumn;
						if (propertyInfo.Name == "Id")
						{
							primaryKey = propertyInfo;
							mappingPrimaryKey = mappingInfo.MappingField;
						}
						mappingInfo.NotNull = propertyAttribute.NotNull;
						mappingInfo.AutoIncrement = propertyAttribute.AutoIncrement;
						if (propertyAttribute.DefaultValue != null)
						{
							mappingInfo.DefaultValue = propertyAttribute.DefaultValue;
						}
						mappingInfo.Indexing = propertyAttribute.Indexing;
						mappingInfo.Unique = propertyAttribute.Unique;
					}
					properties.Add(propertyInfo.Name, mappingInfo);
				}
				else if (propertyType.IsGenericType)
				{
					var typeDefinition = propertyType.GetGenericTypeDefinition();
					var argument = propertyType.GetGenericArguments()[0];
					if ((typeDefinition == typeof(IList<>) || typeDefinition == typeof(List<>)) && argument.IsClass && !(argument == typeof(string)))
					{
						var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
						if (propertyAttributes.Length > 0)
						{
							var propertyAttribute = (PropertyAttribute) propertyAttributes[0];
							if (propertyAttribute.Attach)
							{
								var mappingInfo = new MappingInfo()
								{
									AutoIncrement = false,
									DefaultValue = null,
									Indexing = PropertyAttribute.NO_INDEX,
									NotNull = false,
									MappingField = propertyInfo.Name,
									Unique = false,
									PropertyInfo = propertyInfo
								};
								if (propertyAttribute.MappingColumn != null)
								{
									mappingInfo.MappingField = propertyAttribute.MappingColumn;
								}
								properties.Add(propertyInfo.Name, mappingInfo);
								continue;
							}
						}
						var relationInfo = new RelationInfo() { OriginalMetadata = this, PropertyInfo = propertyInfo };
						relationProperties.Add(propertyInfo.Name, relationInfo);
					}
					else if ((typeDefinition == typeof(IDictionary<,>) || typeDefinition == typeof(Dictionary<,>))
								|| (typeDefinition == typeof(IList<>) || typeDefinition == typeof(List<>)))
					{
						var mappingInfo = new MappingInfo()
							{
								AutoIncrement = false,
								DefaultValue = null,
								Indexing = PropertyAttribute.NO_INDEX,
								NotNull = false,
								MappingField = propertyInfo.Name,
								Unique = false,
								PropertyInfo = propertyInfo
							};
						properties.Add(propertyInfo.Name, mappingInfo);
						var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
						if (propertyAttributes.Length > 0)
						{
							var propertyAttribute = (PropertyAttribute)propertyAttributes[0];
							if (propertyAttribute.MappingColumn != null)
							{
								mappingInfo.MappingField = propertyAttribute.MappingColumn;
							}
						}
					}
				}
                else if (propertyType.IsArray)
                {

                    var mappingInfo = new MappingInfo()
                    {
                        AutoIncrement = false,
                        DefaultValue = null,
                        Indexing = PropertyAttribute.NO_INDEX,
                        NotNull = false,
                        MappingField = propertyInfo.Name,
                        Unique = false,
                        PropertyInfo = propertyInfo
                    };
                    var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
                    if (propertyAttributes.Length > 0)
                    {
                        var propertyAttribute = (PropertyAttribute)propertyAttributes[0];
                        if (propertyAttribute.MappingColumn != null)
                        {
                            mappingInfo.MappingField = propertyAttribute.MappingColumn;
                        }
                    }
                    properties.Add(propertyInfo.Name, mappingInfo);
                }
                else if (propertyType.IsClass)
                {
                    var relationInfo = new RelationInfo() { OriginalMetadata = this, PropertyInfo = propertyInfo };
                    relationProperties.Add(propertyInfo.Name, relationInfo);
                }
			}
		}
示例#2
0
        public void Create()
        {
            var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var propertyInfo in propertyInfos)
            {
                var ignoreAttributes = propertyInfo.GetCustomAttributes(typeof(IgnorePersistentAttribute), true);
                if (ignoreAttributes.Length > 0)
                {
                    continue;
                }
                allPropertiesName.Add(propertyInfo.Name);
                var propertyType = propertyInfo.PropertyType;
                if (propertyType.IsPrimitive || propertyType == typeof(DateTime) || propertyType == typeof(decimal) || propertyType == typeof(string))
                {
                    var mappingInfo = new MappingInfo()
                    {
                        PropertyInfo = propertyInfo
                    };
                    mappingInfo.MappingField = propertyInfo.Name;
                    if (propertyInfo.Name == "Id")
                    {
                        mappingInfo.AutoIncrement = true;
                        mappingInfo.NotNull       = true;
                    }
                    else
                    {
                        if (propertyType == typeof(byte) || propertyType == typeof(sbyte) || propertyType == typeof(bool) ||
                            propertyType == typeof(short) || propertyType == typeof(ushort) ||
                            propertyType == typeof(int) || propertyType == typeof(uint) ||
                            propertyType == typeof(float) || propertyType == typeof(long) ||
                            propertyType == typeof(ulong) || propertyType == typeof(double) ||
                            propertyType == typeof(decimal))
                        {
                            mappingInfo.DefaultValue = 0;
                        }
                        else if (propertyType == typeof(char))
                        {
                            mappingInfo.DefaultValue = "\'\0\'";
                        }
                        else if (propertyType == typeof(DateTime))
                        {
                            mappingInfo.DefaultValue = "\'0001-01-01 12:00:00\'";
                        }
                        else if (propertyType == typeof(string))
                        {
                            mappingInfo.DefaultValue = "\'\'";
                        }
                    }
                    var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
                    if (propertyAttributes.Length > 0)
                    {
                        var propertyAttribute = (PropertyAttribute)propertyAttributes[0];
                        mappingInfo.MappingField = string.IsNullOrEmpty(propertyAttribute.MappingColumn)
                                                                                                        ? propertyInfo.Name
                                                                                                        : propertyAttribute.MappingColumn;
                        if (propertyInfo.Name == "Id")
                        {
                            primaryKey        = propertyInfo;
                            mappingPrimaryKey = mappingInfo.MappingField;
                        }
                        mappingInfo.NotNull       = propertyAttribute.NotNull;
                        mappingInfo.AutoIncrement = propertyAttribute.AutoIncrement;
                        if (propertyAttribute.DefaultValue != null)
                        {
                            mappingInfo.DefaultValue = propertyAttribute.DefaultValue;
                        }
                        mappingInfo.Indexing = propertyAttribute.Indexing;
                        mappingInfo.Unique   = propertyAttribute.Unique;
                    }
                    properties.Add(propertyInfo.Name, mappingInfo);
                }
                else if (propertyType.IsGenericType)
                {
                    var typeDefinition = propertyType.GetGenericTypeDefinition();
                    var argument       = propertyType.GetGenericArguments()[0];
                    if ((typeDefinition == typeof(IList <>) || typeDefinition == typeof(List <>)) && argument.IsClass && !(argument == typeof(string)))
                    {
                        var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
                        if (propertyAttributes.Length > 0)
                        {
                            var propertyAttribute = (PropertyAttribute)propertyAttributes[0];
                            if (propertyAttribute.Attach)
                            {
                                var mappingInfo = new MappingInfo()
                                {
                                    AutoIncrement = false,
                                    DefaultValue  = null,
                                    Indexing      = PropertyAttribute.NO_INDEX,
                                    NotNull       = false,
                                    MappingField  = propertyInfo.Name,
                                    Unique        = false,
                                    PropertyInfo  = propertyInfo
                                };
                                if (propertyAttribute.MappingColumn != null)
                                {
                                    mappingInfo.MappingField = propertyAttribute.MappingColumn;
                                }
                                properties.Add(propertyInfo.Name, mappingInfo);
                                continue;
                            }
                        }
                        var relationInfo = new RelationInfo()
                        {
                            OriginalMetadata = this, PropertyInfo = propertyInfo
                        };
                        relationProperties.Add(propertyInfo.Name, relationInfo);
                    }
                    else if ((typeDefinition == typeof(IDictionary <,>) || typeDefinition == typeof(Dictionary <,>)) ||
                             (typeDefinition == typeof(IList <>) || typeDefinition == typeof(List <>)))
                    {
                        var mappingInfo = new MappingInfo()
                        {
                            AutoIncrement = false,
                            DefaultValue  = null,
                            Indexing      = PropertyAttribute.NO_INDEX,
                            NotNull       = false,
                            MappingField  = propertyInfo.Name,
                            Unique        = false,
                            PropertyInfo  = propertyInfo
                        };
                        properties.Add(propertyInfo.Name, mappingInfo);
                        var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
                        if (propertyAttributes.Length > 0)
                        {
                            var propertyAttribute = (PropertyAttribute)propertyAttributes[0];
                            if (propertyAttribute.MappingColumn != null)
                            {
                                mappingInfo.MappingField = propertyAttribute.MappingColumn;
                            }
                        }
                    }
                }
                else if (propertyType.IsArray)
                {
                    var mappingInfo = new MappingInfo()
                    {
                        AutoIncrement = false,
                        DefaultValue  = null,
                        Indexing      = PropertyAttribute.NO_INDEX,
                        NotNull       = false,
                        MappingField  = propertyInfo.Name,
                        Unique        = false,
                        PropertyInfo  = propertyInfo
                    };
                    var propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true);
                    if (propertyAttributes.Length > 0)
                    {
                        var propertyAttribute = (PropertyAttribute)propertyAttributes[0];
                        if (propertyAttribute.MappingColumn != null)
                        {
                            mappingInfo.MappingField = propertyAttribute.MappingColumn;
                        }
                    }
                    properties.Add(propertyInfo.Name, mappingInfo);
                }
                else if (propertyType.IsClass)
                {
                    var relationInfo = new RelationInfo()
                    {
                        OriginalMetadata = this, PropertyInfo = propertyInfo
                    };
                    relationProperties.Add(propertyInfo.Name, relationInfo);
                }
            }
        }