コード例 #1
0
        public void FluentControllerBuilder_FluentActionWithCustomAttributesOnClassInConfig()
        {
            var actionCollection = FluentActionCollection.DefineActions(
                actions =>
            {
                actions.Configure(config =>
                {
                    config.WithCustomAttributeOnClass <MyCustomAttribute>();
                });

                actions
                .RouteGet("/users", "ListUsers")
                .WithCustomAttributeOnClass <MySecondCustomAttribute>()
                .UsingService <IUserService>()
                .To(userService => userService.ListUsers());

                actions
                .RoutePost("/users", "AddUser")
                .UsingService <IUserService>()
                .UsingBody <UserItem>()
                .To((userService, user) => userService.AddUser(user));

                actions
                .RouteGet("/users/{userId}", "GetUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .To((userService, userId) => userService.GetUserById(userId));

                actions
                .RoutePut("/users/{userId}", "UpdateUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .UsingBody <UserItem>()
                .To((userService, userId, user) => userService.UpdateUser(userId, user));

                actions
                .RouteDelete("/users/{userId}", "RemoveUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .To((userService, userId) => userService.RemoveUser(userId));
            }
                );

            foreach (var action in actionCollection.Where(action => action.Id != "ListUsers"))
            {
                Assert.Equal(0, action.Definition.CustomAttributes.Count);
                Assert.Equal(1, action.Definition.CustomAttributesOnClass.Count);
            }

            Assert.Equal(0, actionCollection.Single(action => action.Id == "ListUsers").Definition.CustomAttributes.Count);
            Assert.Equal(2, actionCollection.Single(action => action.Id == "ListUsers").Definition.CustomAttributesOnClass.Count);
        }
コード例 #2
0
        public void FluentControllerBuilder_FluentActionCollectionWithGroupBy()
        {
            var actionCollection = FluentActionCollection.DefineActions(
                actions =>
            {
                actions.Configure(config =>
                {
                    config.GroupBy("CustomGroupName");
                });

                actions
                .RouteGet("/users", "ListUsers")
                .UsingService <IUserService>()
                .To(userService => userService.ListUsers());

                actions
                .RoutePost("/users", "AddUser")
                .UsingService <IUserService>()
                .UsingBody <UserItem>()
                .To((userService, user) => userService.AddUser(user));

                actions
                .RouteGet("/users/{userId}", "GetUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .To((userService, userId) => userService.GetUserById(userId));

                actions
                .RoutePut("/users/{userId}", "UpdateUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .UsingBody <UserItem>()
                .To((userService, userId, user) => userService.UpdateUser(userId, user));

                actions
                .RouteDelete("/users/{userId}", "RemoveUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .To((userService, userId) => userService.RemoveUser(userId));
            }
                );

            Assert.Equal("CustomGroupName", actionCollection.Config.GroupName);

            foreach (var action in actionCollection)
            {
                Assert.Equal("CustomGroupName", action.Definition.GroupName);
            }
        }
コード例 #3
0
        public void FluentControllerBuilder_FluentActionCollectionWithParentTypeInConfig()
        {
            var actionCollection = FluentActionCollection.DefineActions(
                actions =>
            {
                actions.Configure(config =>
                {
                    config.InheritingFrom <BaseController>();
                });

                actions
                .RouteGet("/users", "ListUsers")
                .UsingService <IUserService>()
                .To(userService => userService.ListUsers());

                actions
                .RoutePost("/users", "AddUser")
                .UsingService <IUserService>()
                .UsingBody <UserItem>()
                .To((userService, user) => userService.AddUser(user));

                actions
                .RouteGet("/users/{userId}", "GetUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .To((userService, userId) => userService.GetUserById(userId));

                actions
                .RoutePut("/users/{userId}", "UpdateUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .UsingBody <UserItem>()
                .To((userService, userId, user) => userService.UpdateUser(userId, user));

                actions
                .RouteDelete("/users/{userId}", "RemoveUser")
                .UsingService <IUserService>()
                .UsingRouteParameter <int>("userId")
                .To((userService, userId) => userService.RemoveUser(userId));
            }
                );

            foreach (var action in actionCollection)
            {
                Assert.Equal(typeof(BaseController), action.Definition.ParentType);
            }
        }
        public void FluentControllerBuilder_FluentActionUsingParentFromConfigReturnsString()
        {
            var collection = FluentActionCollection.DefineActions(actions =>
            {
                actions.Configure(config =>
                {
                    config.InheritingFrom <BaseController>();
                });

                actions.RouteGet("/route/url")
                .UsingParent <BaseController>()
                .To(parent => parent.Hello());
            });

            BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                collection.FluentActions[0],
                typeof(ControllerWithParentReturnsString),
                null);
        }
コード例 #5
0
        public void FluentControllerBuilder_FluentActionWithParentTypeInConfigReturnsString()
        {
            var actionCollection = FluentActionCollection.DefineActions(
                actions =>
            {
                actions.Configure(config =>
                {
                    config.InheritingFrom(typeof(BaseController));
                });

                actions.Add(
                    new FluentAction("/route/url", HttpMethod.Get)
                    .InheritingFrom <BaseController>()
                    .To(() => $"hello")
                    );
            }
                );

            BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                actionCollection.FluentActions[0],
                typeof(ControllerWithParentTypeReturnsString),
                null);
        }
コード例 #6
0
        public void FluentActionCollection_DefineActions_Config_AppendAsync_ReturnsStringContainer()
        {
            var actionCollection = FluentActionCollection.DefineActions(
                actions =>
            {
                actions.Configure(config =>
                {
                    config.Append(action => action
                                  .UsingResult()
                                  .To(async result => { await Task.Delay(1); return(new StringContainer {
                            Value = result.ToString()
                        }); })
                                  );
                });

                actions
                .RouteGet("/hello")
                .UsingQueryStringParameter <string>("name")
                .To(name => $"Hello {name}!");

                actions
                .RouteGet("/helloAsync")
                .UsingQueryStringParameter <string>("name")
                .To(async name => { await Task.Delay(1); return($"Hello {name}!"); });

                actions
                .RouteGet("/helloAsyncWithDo")
                .Do(() => { /* Doing nothing */ })
                .UsingQueryStringParameter <string>("name")
                .To(async name => { await Task.Delay(1); return($"Hello {name}!"); });

                actions
                .RouteGet("/helloAsyncWithAsyncDo")
                .Do(async() => { await Task.Delay(1); })
                .UsingQueryStringParameter <string>("name")
                .To(async name => { await Task.Delay(1); return($"Hello {name}!"); });

                actions
                .RouteGet("/hi/{name}")
                .UsingRouteParameter <string>("name")
                .To(name => $"Hi {name}!")
                .UsingResult()
                .To(greeting => $"{greeting} How are you?");

                actions
                .RouteGet("/userCount")
                .UsingService <IUserService>()
                .To(userService => userService.ListUsers().Count());

                actions
                .RouteGet("/toView")
                .ToView("~/path/to/view");
            }
                );

            foreach (var action in actionCollection)
            {
                switch (action.RouteTemplate)
                {
                case "/hello":
                    BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                        action,
                        typeof(AppendAsync_HelloController),
                        new object[] { "Bob" });
                    break;

                case "/helloAsync":
                    BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                        action,
                        typeof(AppendAsync_HelloAsyncController),
                        new object[] { "Bob" });
                    break;

                case "/helloAsyncWithDo":
                    BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                        action,
                        typeof(AppendAsync_HelloAsyncWithDoController),
                        new object[] { "Bob" });
                    break;

                case "/helloAsyncWithAsyncDo":
                    BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                        action,
                        typeof(AppendAsync_HelloAsyncWithAsyncDoController),
                        new object[] { "Bob" });
                    break;

                case "/hi/{name}":
                    BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                        action,
                        typeof(AppendAsync_HiController),
                        new object[] { "Bob" });
                    break;

                case "/userCount":
                    BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                        action,
                        typeof(AppendAsync_UserCountController),
                        new object[] { new UserService() });
                    break;

                case "/toView":
                    BuilderTestUtils.BuildActionAndCompareToStaticActionWithResult(
                        action,
                        typeof(AppendAsync_ToViewController),
                        new object[0]);
                    break;

                default:
                    throw new Exception($"Could not find controller type to compare with for action {action}");
                }
            }
        }