Exemplo n.º 1
0
        private UserVM CreateUserVM(
            Func <UserVM, Department, bool> filter = null,
            Department[] allDepartments            = null,
            Func <User, IEnumerable <Department> > allDepartmentsSelector = null,
            Department selectedDepartment = null,
            User sourceUser = null,
            Action <SingleSelectionDescriptorBuilder <UserVM, Department, DepartmentVM> > descriptorConfigurationAction = null
            )
        {
            if (allDepartments != null && allDepartmentsSelector != null)
            {
                throw new ArgumentException();
            }

            sourceUser = sourceUser ?? new User();
            if (selectedDepartment != null)
            {
                sourceUser.Department = selectedDepartment;
            }

            UserVMDescriptor descriptor = VMDescriptorBuilder
                                          .OfType <UserVMDescriptor>()
                                          .For <UserVM>()
                                          .WithProperties((d, c) => {
                var u = c.GetPropertyBuilder();

                var builder = u.SingleSelection(x => x.Source.Department).EnableUndo();

                if (filter != null)
                {
                    builder = builder.WithFilter(filter);
                }

                if (allDepartments != null)
                {
                    builder = builder.WithItems(x => allDepartments);
                }

                if (allDepartmentsSelector != null)
                {
                    builder = builder.WithItems(x => allDepartmentsSelector(x.Source));
                }

                d.Name = u.Property.MapsTo(x => x.Name);
                //d.Department = builder.Of<DepartmentVM>(DepartmentVM.ClassDescriptor);
                d.Department = builder.Of <DepartmentVM>(descriptorConfigurationAction);
            })
                                          .WithValidators(b => {
                b
                .When(x => true)
                .OnlyExistingItemsAreSelected(x => x.Department);
            })
                                          .Build();

            var vm = new UserVM(descriptor);

            vm.InitializeFrom(sourceUser);
            return(vm);
        }
Exemplo n.º 2
0
        private UserVM CreateUserVM(
            Func <UserVM, Group, bool> filter = null,
            Group[] allGroups          = null,
            List <Group> allGroupsList = null,
            Func <User, IEnumerable <Group> > allGroupsSelector      = null,
            Func <User, ICollection <Group> > selectedGroupsSelector = null,
            Action <RootValidatorBuilder <UserVM, UserVM, UserVMDescriptor> > validatorBuilder = null,
            bool isSelectedRaisesPropertyChanged = true,
            params Group[] selectedGroups
            )
        {
            if (allGroups != null && allGroupsSelector != null)
            {
                throw new ArgumentException();
            }

            validatorBuilder = validatorBuilder ?? (b => {
                b.ValidateDescendant(x => x.Groups).CheckCollection(x => x.SelectedItems).Custom(args => {
                });
                b
                .When(x => true)
                .OnlyExistingItemsAreSelected(x => x.Groups);
            });

            var sourceUser = new User(selectedGroups);

            var descriptorBuilder = VMDescriptorBuilder
                                    .OfType <UserVMDescriptor>()
                                    .For <UserVM>()
                                    .WithProperties((d, c) => {
                var u = c.GetPropertyBuilder();

                var builder = selectedGroupsSelector != null ?
                              u.MultiSelection(x => selectedGroupsSelector(x.Source)) :
                              u.MultiSelection(x => x.Source.Groups);

                if (filter != null)
                {
                    builder = builder.WithFilter(filter);
                }

                if (allGroups != null)
                {
                    builder = builder.WithItems(x => allGroups);
                }

                if (allGroupsList != null)
                {
                    builder = builder.WithItems(x => allGroupsList);
                }

                if (allGroupsSelector != null)
                {
                    builder = builder.WithItems(x => allGroupsSelector(x.Source));
                }

                builder = builder.EnableUndo();

                d.Name   = u.Property.MapsTo(x => x.Name);
                d.Groups = builder.Of <GroupVM>(isSelectedRaisesPropertyChanged: isSelectedRaisesPropertyChanged);
            });

            if (validatorBuilder != null)
            {
                descriptorBuilder = descriptorBuilder.WithValidators(validatorBuilder);
            }

            UserVMDescriptor descriptor = descriptorBuilder.Build();

            var vm = new UserVM(descriptor);

            vm.InitializeFrom(sourceUser);
            return(vm);
        }