Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            Pluralize.NET.Core.Pluralizer plu = new Pluralize.NET.Core.Pluralizer();

            string original = "wife";

            original = "man";
            original = "woman";
            original = "mountainman";
            original = "mountainwoman";
            original = "mountainwoman";
            original = "truss";
            original = "city";
            original = "boy";
            original = "potato";
            original = "analysis";
            original = "ellipsis";
            original = "criterion";
            original = "sheep";
            original = "phenomenon";
            original = "focus";
            original = "cactus";

            original = "gas"; // bug - fixed
            original = "fez"; // bug - fixed

            original = "puppy";
            original = "mountainfez"; // bug - doesn't recognize ending
            original = "noblegas";    // bug - doesn't recognize ending

            // original = original.Trim();
            string plural   = plu.Pluralize(original);
            string singular = plu.Singularize(plural);

            System.Console.WriteLine(plural);
            System.Console.WriteLine(singular);

            AsyncSafety.Test();
            ThreadSafety.Test();

            System.Console.WriteLine(" --- Press any key to continue --- ");
            System.Console.ReadKey();
        }
Exemplo n.º 2
0
        public virtual DBTableMapping getTableMappingFor <T>() where T : new()
        {
            var    result        = new DBTableMapping(this.DbDelimiterFormatString);
            var    item          = new T();
            var    itemType      = item.GetType();
            var    properties    = itemType.GetProperties();
            string replaceString = "[^a-zA-Z0-9]";
            var    rgx           = new Regex(replaceString);

            // Set up the default, trying to simple map type name to table name:
            string flattenedItemTypeName = rgx.Replace(itemType.Name.ToLower(), "");
            string plural      = new Pluralize.NET.Core.Pluralizer().Pluralize(flattenedItemTypeName);
            var    dbTableName = this.DbTableNames.FirstOrDefault(t => rgx.Replace(t.ToLower(), "") == flattenedItemTypeName);

            if (dbTableName == null)
            {
                dbTableName = this.DbTableNames.FirstOrDefault(t => rgx.Replace(t.ToLower(), "") == plural);
            }
            // Override the default if the user specified a name with an attribute:
            var tableNameAttribute = itemType.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(DbTableAttribute)) as DbTableAttribute;

            if (tableNameAttribute != null)
            {
                dbTableName = tableNameAttribute.Name;
            }
            // If it's still null, there is no mapping found:
            if (dbTableName == null)
            {
                throw new Exception(string.Format("Could not map class '{0}' to a table in the database.", itemType.Name));
            }
            result.DBTableName    = dbTableName;
            result.MappedTypeName = itemType.Name;
            var dbColumnInfo = from c in this.DbColumnsList where c.TableName == dbTableName select c;

            foreach (var property in properties)
            {
                var             propertyType          = property.PropertyType;
                string          flattenedPropertyName = rgx.Replace(property.Name.ToLower(), "");
                DbColumnMapping columnMapping         = dbColumnInfo.FirstOrDefault(c => rgx.Replace(c.ColumnName.ToLower(), "") == flattenedPropertyName);
                if (columnMapping != null)
                {
                    columnMapping.PropertyName = property.Name;
                    columnMapping.DataType     = propertyType;
                }
                // Look for a custom column name attribute:
                DbColumnAttribute mappedColumnAttribute = null;
                var attribute = property.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(DbColumnAttribute));
                if (attribute != null)
                {
                    // Use the column name found in the attribute:
                    mappedColumnAttribute = attribute as DbColumnAttribute;
                    string matchColumnName = mappedColumnAttribute.Name;
                    columnMapping = dbColumnInfo.FirstOrDefault(c => c.ColumnName == matchColumnName);
                    columnMapping.PropertyName = property.Name;
                    columnMapping.DataType     = propertyType;
                }
                if (columnMapping != null)
                {
                    result.ColumnMappings.Add(columnMapping);
                    if (columnMapping.IsPrimaryKey)
                    {
                        result.PrimaryKeyMapping.Add(columnMapping);
                    }
                }
            }
            if (result.PrimaryKeyMapping.Count == 0)
            {
                string keyNotDefinedMessageFormat = ""
                                                    + "No primary key mapping found in table '{0}' for type '{1}'. "
                                                    + "Please define a property which maps to a table primary key for objects of this type.";
                throw new Exception(string.Format(keyNotDefinedMessageFormat, dbTableName, itemType.Name));
            }
            return(result);
        }