/// <summary> /// Checks if a property is the id of the given type. /// </summary> /// <param name="p">Property to be checked against the type.</param> /// <param name="type">For own and the object's ID supply their type. For the related object supply the type with which /// it has a relation.</param> /// <param name="idOption">IdOptions: Own, Objects or Related.</param> /// <returns></returns> public static bool IsId(this PropertyInfo p, Type type, IdOptions idOption) { bool withId = p.Name.EndsWith("Id"); switch (idOption) { case IdOptions.Own: return(withId && ((p.Name.StartsWith(type.Name) && p.Name.Length == 2 + type.Name.Length) || (p.Name.Length == 2)) && (p.DeclaringType.Equals(type) || p.DeclaringType.IsAssignableFrom(type))); case IdOptions.Objects: return(withId && p.Name.StartsWith(type.Name) && p.Name.Length == 2 + type.Name.Length && !p.DeclaringType.Equals(type)); case IdOptions.Related: return(withId && !p.Name.StartsWith(type.Name) && !p.DeclaringType.Equals(type)); default: throw new ArgumentException("Parameter 'idOption' is not set correctly"); } }
/// <summary> /// Finds the ID property of the type or its related object. /// </summary> /// <param name="objectsType">For the object's ID, supply their type. For the related object supply the type with which /// it has a relation.</param> /// <param name="idOption"></param> /// <returns></returns> public static PropertyInfo GetId(this Type type, Type objectsType, IdOptions idOption) { PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); var result = properties.FirstOrDefault(x => x.IsId(objectsType, idOption)); if (result == null) { throw new ArgumentException("Given type does not have any Id property following EF Core naming rules."); } else { return(result); } }
public void AddPrefix(string prefix, IdOptions flags) { base.AddPrefixFlag(prefix, (short)flags); }
public IdentifierTerminal(string name, IdOptions flags) : this(name, "_", "_") { Options = flags; }
public IdentifierTerminal(string name, IdOptions options) : this(name, "_", "_") { Options = options; }
public static BnfiTermConversion <string> CreateIdentifier(string name, IdOptions options) { return(new IdentifierTerminal(name, options).IntroIdentifier()); }
/// <summary> /// Checks if a type has and id property of the type given in the parameter. /// </summary> /// <param name="type">For own and the object's ID supply their type. For the related object supply the type with which /// it has a relation.</param> /// <param name="idOption"></param> /// <returns></returns> static public bool HasId(this Type type, IdOptions idOption = IdOptions.Own) { return(type .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy) .Any(p => p.IsId(type, idOption))); }
public void AddPrefix(string prefix, IdOptions options) { AddPrefixFlag(prefix, (short)options); }