public static FunctionBody WithServerMethodsFrom(this FunctionBody body, Type type) { body.Property("server", p => p.WithObjectLiteral(o => { var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var method in methods) { if (method.Name.StartsWith("get_") || method.Name.StartsWith("set_")) { continue; } o.Assign(method.Name.ToCamelCase()).WithFunction(f => { f.Body.Variant("result", v => { v.WithFunctionCall(fc => fc .WithName("self.invokeServerMethod") .WithParameters("\"" + method.Name + "\"", "arguments") ); }); f.Body.Return(new Literal("result")); }); } })); return(body); }
public static Container WithReadModelConvenienceFunctions(this FunctionBody functionBody, Type type) { var excludePropertiesFrom = typeof(IReadModel); var properties = type.GetProperties(); if (excludePropertiesFrom != null) { properties = properties.Where(p => !excludePropertiesFrom.GetProperties().Select(pi => pi.Name).Contains(p.Name)).ToArray(); } foreach (var property in properties) { var functionName = string.Format("matching{0}", property.Name.ToPascalCase()); var propertyName = property.Name.ToCamelCase(); var filter = new ObjectLiteral(); filter.Assign(propertyName).WithLiteral(propertyName); functionBody.Property(functionName, p => p.WithFunction(function => function .WithParameters(propertyName) .Body .Scope("self", scope => scope.FunctionCall(f => f.WithName("instanceMatching").WithParameters(new[] { filter }) ) ) ) ); } return(functionBody); }
public static Container WithServiceMethodsFrom(this FunctionBody functionBody, Type type) { var objectMethods = typeof(object).GetMethods(); var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(m => objectMethods.Any(om => om.DeclaringType != m.DeclaringType)); foreach (var method in methods) { var functionName = method.Name.ToCamelCase(); var selfScopeCall = new Scope("self"); var parameters = method.GetParameters().Select(p => p.Name.ToCamelCase()).ToArray(); var objectLiteral = new ObjectLiteral(); foreach (var parameter in parameters) { objectLiteral.Assign(parameter).WithLiteral(parameter); } if (method.ReturnType == typeof(void)) { selfScopeCall.FunctionCall(f => f.WithName("callWithoutReturnValue").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral)); } else if (method.ReturnType.IsDictionary() || !method.ReturnType.IsEnumerable()) { selfScopeCall.FunctionCall(f => f.WithName("callWithObjectAsReturn").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral)); } else if (method.ReturnType.IsEnumerable()) { selfScopeCall.FunctionCall(f => f.WithName("callWithArrayAsReturn").WithParameters(new Literal("\"" + method.Name + "\""), objectLiteral)); } functionBody.Property(functionName, p => { p.WithFunction(function => function .WithParameters(parameters) .Body .Return(selfScopeCall) ); }); } return(functionBody); }