public override void Visit(ActionTreeNode node)
		{
			CodeTypeDeclaration type = _typeStack.Peek();
			List<Type> actionArgumentTypes = new List<Type>();

			CodeMemberMethod method = new CodeMemberMethod();
			method.Name = node.Name;
			method.ReturnType = _source[typeof (IControllerActionReference)];
			method.Attributes = MemberAttributes.Public;
			method.CustomAttributes.Add(_source.DebuggerAttribute);
			List<CodeExpression> actionArguments = CreateActionArgumentsAndAddParameters(method, node, actionArgumentTypes);
			method.Statements.Add(
				new CodeMethodReturnStatement(CreateNewActionReference(node, actionArguments, actionArgumentTypes)));
			type.Members.Add(method);


			if (actionArguments.Count > 0 && _occurences[node.Name] == 1)
			{
				method = new CodeMemberMethod();
				method.Comments.Add(
					new CodeCommentStatement("Empty argument Action... Not sure if we want to pass MethodInformation to these..."));
				method.Name = node.Name;
				method.ReturnType = _source[typeof (IArgumentlessControllerActionReference)];
				method.Attributes = MemberAttributes.Public;
				method.CustomAttributes.Add(_source.DebuggerAttribute);
				method.Statements.Add(
					new CodeMethodReturnStatement(CreateNewActionReference(node, new List<CodeExpression>(), actionArgumentTypes)));
				type.Members.Add(method);
			}

			base.Visit(node);
		}
    public void VisitActionNode_NoParameters_CreatesMethod()
    {
      ActionTreeNode node = new ActionTreeNode("Index");
      _controller.AddChild(node);

      _mocks.ReplayAll();
      _generator.Visit(_controller);
      _mocks.VerifyAll();

      CodeDomAssert.AssertHasField(_source.Ccu.Namespaces[0].Types[0], "_services");
      CodeDomAssert.AssertHasMethod(_source.Ccu.Namespaces[0].Types[0], "Index");
    }
		public void VisitRouteNode_NoParameters_CreatesMethod()
		{
			RouteTreeNode node = new StaticRouteTreeNode("Index", "index");
			ActionTreeNode actionTreeNode = new ActionTreeNode("action");
			actionTreeNode.AddChild(node);
			controller.AddChild(actionTreeNode);

			mocks.ReplayAll();
			generator.Visit(controller);
			mocks.VerifyAll();

			CodeDomAssert.AssertHasField(source.Ccu.Namespaces[0].Types[0], "_services");
			CodeDomAssert.AssertHasMethod(source.Ccu.Namespaces[0].Types[2], "Index");
		}
		protected CodeExpression CreateNewActionReference(ActionTreeNode node, List<CodeExpression> actionArguments,
		                                                  List<Type> actionArgumentTypes)
		{
			List<CodeExpression> actionArgumentRuntimeTypes = new List<CodeExpression>();
			foreach (Type type in actionArgumentTypes)
			{
				actionArgumentRuntimeTypes.Add(new CodeTypeOfExpression(_source[type]));
			}

			CodeExpression createMethodSignature = new CodeObjectCreateExpression(
				_source[typeof (MethodSignature)],
				new CodeExpression[]
					{
						new CodeTypeOfExpression(node.Controller.FullName),
						new CodePrimitiveExpression(node.Name),
						new CodeArrayCreateExpression(_source[typeof (Type)], actionArgumentRuntimeTypes.ToArray())
					}
				);

			CodeExpression[] constructionArguments = new CodeExpression[]
				{
					new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
					                                 _naming.ToMemberVariableName(_serviceIdentifier)),
					new CodeTypeOfExpression(node.Controller.FullName),
					new CodePrimitiveExpression(node.Controller.Area),
					new CodePrimitiveExpression(_naming.ToControllerName(node.Controller.Name)),
					new CodePrimitiveExpression(node.Name),
					createMethodSignature,
					new CodeArrayCreateExpression(_source[typeof (ActionArgument)], actionArguments.ToArray())
				};

			return new CodeMethodInvokeExpression(
				new CodeMethodReferenceExpression(
					new CodePropertyReferenceExpression(
						new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
						                                 _naming.ToMemberVariableName(_serviceIdentifier)),
						"ControllerReferenceFactory"),
					"CreateActionReference"),
				constructionArguments
				);
		}
		protected List<CodeExpression> CreateActionArgumentsAndAddParameters(CodeMemberMethod method, ActionTreeNode node,
		                                                                     List<Type> actionArgumentTypes)
		{
			List<CodeExpression> actionArguments = new List<CodeExpression>();

			int index = 0;

			List<TreeNode> parameters = node.Children.FindAll(delegate(TreeNode t) { return t is ParameterTreeNode; });

			foreach (ParameterTreeNode parameterInfo in parameters)
			{
				CodeParameterDeclarationExpression newParameter = new CodeParameterDeclarationExpression();
				newParameter.Name = parameterInfo.Name;
				newParameter.Type = _source[parameterInfo.Type];
				method.Parameters.Add(newParameter);

				actionArgumentTypes.Add(parameterInfo.Type);

				CodeObjectCreateExpression argumentCreate =
					new CodeObjectCreateExpression(_source[typeof (ActionArgument)], new CodeExpression[]
					                                                                 	{
					                                                                 		new CodePrimitiveExpression(index++),
					                                                                 		new CodePrimitiveExpression(parameterInfo.Name),
					                                                                 		new CodeTypeOfExpression(newParameter.Type),
					                                                                 		new CodeArgumentReferenceExpression(
					                                                                 			parameterInfo.Name)
					                                                                 	});

				actionArguments.Add(argumentCreate);
			}
			return actionArguments;
		}
		public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
		{
			if ((methodDeclaration.Modifier & Modifiers.Public) != Modifiers.Public)
			{
				return null;
			}

			ControllerTreeNode controllerNode = (ControllerTreeNode) _treeService.Peek;

			if (controllerNode is WizardControllerTreeNode)
			{
				Type wizardControllerInterface = typeof (IWizardController);

				MethodInfo[] methodInfos = wizardControllerInterface.GetMethods(BindingFlags.Public | BindingFlags.Instance);

				if ((methodDeclaration.Name == "GetSteps") && (methodDeclaration.Body.Children.Count > 0))
				{
					(controllerNode as WizardControllerTreeNode).WizardStepPages = GetWizardStepPages(methodDeclaration.Body);

					return null;
				}
				else if (
					Array.Exists(methodInfos, delegate(MethodInfo methodInfo) { return methodInfo.Name == methodDeclaration.Name; }))
					return null;
			}

			ActionTreeNode action = new ActionTreeNode(methodDeclaration.Name);

			foreach (ParameterDeclarationExpression parameter in methodDeclaration.Parameters)
			{
				Type type = TypeResolver.Resolve(parameter.TypeReference, true);
				action.AddChild(new ParameterTreeNode(parameter.ParameterName, type));
			}

			foreach (AttributeSection attributeSection in methodDeclaration.Attributes)
			{
				List<Attribute> attributes = attributeSection.Attributes.FindAll(
					delegate(Attribute attribute) { return attribute.Name == "StaticRoute"; });

				foreach (Attribute attribute in attributes)
				{
					PrimitiveExpression name = (PrimitiveExpression) attribute.PositionalArguments[0];
					PrimitiveExpression pattern = (PrimitiveExpression) attribute.PositionalArguments[1];

					StaticRouteTreeNode routeTreeNode = new StaticRouteTreeNode((string) name.Value, (string) pattern.Value);
					action.AddChild(routeTreeNode);
				}

				attributes = attributeSection.Attributes.FindAll(
					delegate(Attribute attribute) { return attribute.Name == "PatternRoute"; });

				foreach (Attribute attribute in attributes)
				{
					PrimitiveExpression name = (PrimitiveExpression)attribute.PositionalArguments[0];
					PrimitiveExpression pattern = (PrimitiveExpression)attribute.PositionalArguments[1];
					string[] defaults = new string[attribute.PositionalArguments.Count - 2];

					for(int i = 0; i < defaults.Length; i++)
						defaults[i] = (string) ((PrimitiveExpression) attribute.PositionalArguments[2 + i]).Value;

					PatternRouteTreeNode routeTreeNode = new PatternRouteTreeNode((string)name.Value, (string)pattern.Value, defaults);
					action.AddChild(routeTreeNode);
				}
			}

			controllerNode.AddChild(action, true);

			return base.VisitMethodDeclaration(methodDeclaration, data);
		}
Beispiel #7
0
		public virtual void Visit(ActionTreeNode node)
		{
			Accept(node.Children);
		}
    public void VisitActionNode_OneParameters_CreatesMethod()
    {
      ActionTreeNode node = new ActionTreeNode("Index");
      _controller.AddChild(node);
      node.AddChild(new ParameterTreeNode("id", typeof(Int32)));

      using (_mocks.Unordered())
      {
      }

      _mocks.ReplayAll();
      _generator.Visit(_controller);
      _mocks.VerifyAll();

      CodeDomAssert.AssertHasField(_source.Ccu.Namespaces[0].Types[0], "_services");
      CodeDomAssert.AssertHasMethod(_source.Ccu.Namespaces[0].Types[0], "Index");
    }
		public void VisitRouteNode_OneParameters_CreatesMethod()
		{
			RouteTreeNode node = new StaticRouteTreeNode("AuthenticateLogIn", "login/authenticate/<userName:string>/<password:string>");
			ActionTreeNode actionTreeNode = new ActionTreeNode("action");
			actionTreeNode.AddChild(node);
			controller.AddChild(actionTreeNode);
			node.AddChild(new ParameterTreeNode("userName", typeof(string)));

			using (mocks.Unordered())
			{
			}

			mocks.ReplayAll();
			generator.Visit(controller);
			mocks.VerifyAll();

			CodeDomAssert.AssertHasField(source.Ccu.Namespaces[0].Types[0], "_services");
			CodeDomAssert.AssertHasMethod(source.Ccu.Namespaces[0].Types[2], "AuthenticateLogIn");
		}