public bool Equals(TypeMapping other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.Name, Name) && other.ModelType == ModelType && Equals(other.Key, Key) && Equals(other.Text, Text); }
private static TypeMapping CreateTypeMapping(Type type, NamespaceMapping @namespace, TypeMappingConfiguration configuration) { var mapping = new TypeMapping { ID = Guid.NewGuid().ToString("N"), ModelType = type, Namespace = @namespace, Configuration = configuration }; var attributes = type.GetCustomAttributes(true); var displayName = attributes.OfType<DisplayNameAttribute>().FirstOrDefault(); mapping.Name = (displayName != null && !string.IsNullOrEmpty(displayName.DisplayName)) ? displayName.DisplayName : type.Name; var scaffold = attributes.OfType<ScaffoldTableAttribute>().FirstOrDefault(); mapping.Visible = (scaffold == null || scaffold.Scaffold) && !type.GetCustomAttributes(false).OfType<ScriptOnlyAttribute>().Any(); var properties = OrderProperties(type, TypeDescriptor.GetProperties(type).OfType<PropertyDescriptor>()); var ignoredNames = new List<string> { "Equals", "GetHashCode", "ToString", "GetType", "ReferenceEquals" }; foreach (var property in properties) { ignoredNames.Add("get_" + property.Name); ignoredNames.Add("set_" + property.Name); } mapping.Properties = properties.Where(p => !p.Attributes.OfType<ScriptOnlyAttribute>().Any()); mapping.Key = properties.FirstOrDefault(p => p.Attributes.OfType<KeyAttribute>().Any()); mapping.Text = properties.FirstOrDefault(p => p.Attributes.OfType<TextAttribute>().Any()); var constructors = type.GetConstructors() .Where(ctor => ctor.IsPublic && ctor.GetParameters().Any()) .ToList(); mapping.Constructors = constructors.Select(m => CreateMethodMapping(mapping, m, constructors.IndexOf(m))).ToList(); mapping.StaticMethods = type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static) .Where(m => m.IsStatic && m.IsPublic && !ignoredNames.Any(nm => nm == m.Name)) .GroupBy(m => m.Name) .SelectMany(g => g.Select(m => CreateMethodMapping(mapping, m, g.ToList().IndexOf(m)))) .ToList(); mapping.InstanceMethods = type.GetMethods() .Where(m => !m.IsStatic && m.IsPublic && !ignoredNames.Any(nm => nm == m.Name)) .GroupBy(m => m.Name) .SelectMany(g => g.Select(m => CreateMethodMapping(mapping, m, g.ToList().IndexOf(m)))) .ToList(); var queryAttributes = type.GetCustomAttributes(false).OfType<QueryAttribute>(); if (queryAttributes.Any()) { mapping.Queries = queryAttributes .OrderBy(q => q.Name) .Select(q => CreateQueryMapping(q, mapping)) .ToList(); } else mapping.Queries = new[] { new QueryAttribute() }.Select(q => CreateQueryMapping(q, mapping)).ToList(); return mapping; }
private static MethodMapping CreateMethodMapping(TypeMapping type, MethodBase method, int index) { var display = method.GetCustomAttributes(false).OfType<DisplayAttribute>().SingleOrDefault(); return new MethodMapping { ID = Guid.NewGuid().ToString("N"), Name = display == null ? method.IsConstructor ? "New" : method.Name : display.GetName(), MethodName = method.Name, Parameters = method.GetParameters(), Index = index, Type = type, ReturnType = method is ConstructorInfo ? typeof(void) : ((MethodInfo)method).ReturnType, UnderlineAction = method.IsConstructor ? RunningObjectsAction.Create : RunningObjectsAction.Execute, Visible = !ScriptOnlyAttribute.Contains(method), Method = method }; }
private static QueryMapping CreateQueryMapping(QueryAttribute query, TypeMapping type) { return new QueryMapping { ID = query.Id ?? Guid.NewGuid().ToString("N"), Name = string.IsNullOrEmpty(query.Name) ? "All Items" : query.Name, Type = type, Visible = true }; }
public ModelDescriptor(TypeMapping mapping) { ModelMapping = mapping; }