public EntityMap BuildMap <T>()
        {
            var result = new EntityMap();
            var type   = typeof(T);

            result.Table = type.Name;

            var tableNameAttribute = type.GetCustomAttributes(typeof(TableNameAttribute), true)?.SingleOrDefault();

            if (tableNameAttribute != null)
            {
                result.Table = ((TableNameAttribute)tableNameAttribute).TableName;
            }

            result.EntityType = type;
            foreach (var prop in type.GetProperties())
            {
                //Convention: the primary key name is always 'Id'
                //todo: add mapping logic customizable
                var isPrimaryKey = string.Equals(prop.Name, "Id", StringComparison.InvariantCultureIgnoreCase);
                var columnName   = prop.Name;
                var map          = new EntityPropertyMap
                {
                    IsPrimaryKey = isPrimaryKey,
                    Property     = prop,
                    ColumnName   = columnName,
                };

                var columnAttribute = prop.GetCustomAttributes(typeof(ColumnAttribute), true)?.SingleOrDefault();

                if (columnAttribute != null)
                {
                    var columnAttr = (ColumnAttribute)columnAttribute;
                    if (columnAttr.Ignore)
                    {
                        continue;
                    }
                    if (!string.IsNullOrWhiteSpace(columnAttr.ColumnName))
                    {
                        map.ColumnName = columnAttr.ColumnName;
                    }

                    if (columnAttr.IsPrimary)
                    {
                        map.IsPrimaryKey = true;
                    }
                }

                result.Properties.Add(map.ColumnName.ToLower(), map);
            }

            return(result);
        }
Exemple #2
0
        private ICollection <EntityPropertyMap> GetProperties(string filecontent)
        {
            ICollection <EntityPropertyMap> properties = new List <EntityPropertyMap>();

            string[] splitPublic = filecontent.Split("public");

            foreach (string line in splitPublic)
            {
                //remove line with "class"
                if (line.Contains("class"))
                {
                    continue;
                }

                //remove line with "()" method
                if (line.Contains("() {"))
                {
                    continue;
                }

                //remove line with "using"
                if (line.Contains("using"))
                {
                    continue;
                }

                //remove get; set;
                string[] splitcolch = line.Split("{");

                string[] content = splitcolch[0].Split(" ", StringSplitOptions.RemoveEmptyEntries);

                EntityPropertyMap prop = new EntityPropertyMap
                {
                    Type = content[0],
                    Name = content[1]
                };
                properties.Add(prop);
            }
            return(properties);
        }
Exemple #3
0
 protected virtual object GetPropertyValue <T>(EntityPropertyMap property, T item)
 {
     return(property.Property.GetValue(item));
 }