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; }