private static void WriteResource(HttpContext context, StreamWriter writer, TypeMappingConfiguration type) { var url = context.Request.Url; // TODO: find a better way to define when it's local or remote var domain = (context.Request["local"] == "1") ? string.Empty : string.Format("//{0}\\:{1}", url.Host, url.Port); var baseUrl = domain + VirtualPathUtility.ToAbsolute("~/"); writer.WriteLine("ro.factory('{0}', function($resource) {{ return $resource('{1}api/{0}/:id/:methodName/:index', {{id:'@id'}}, {{", type.UnderlineType.PartialName(), baseUrl); writer.WriteLine("'all': {method:'GET', isArray:true}, "); writer.WriteLine("'get': {method:'GET'}, "); writer.WriteLine("'insert': {method:'POST'}, "); writer.WriteLine("'update': {method:'PUT'}, "); writer.WriteLine("'delete': {method:'DELETE'}, "); var typeMapping = ModelMappingManager.MappingFor(type.UnderlineType); // write the instance methods foreach (var instanceMethod in typeMapping.InstanceMethods) WriteMethod(writer, instanceMethod); // write the static methods foreach (var staticMethod in typeMapping.StaticMethods) WriteMethod(writer, staticMethod, true); writer.WriteLine("})});"); writer.WriteLine(""); }
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<System.ComponentModel.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; }