コード例 #1
0
        protected override void OnDefineRules()
        {
            // Example of simple Fluent rules

            Property <string>(vm => vm.Code).Is.Required().Otherwise("Code is required.");
            Property <string>(vm => vm.Code).Is.Valid(ViewModelInstance.CodeIsUnique()).Otherwise("Code already exist.");
            Property <string>(vm => vm.Code).Has.MaxLength(3).Otherwise("Code length must not eceed 3 characters.");
            // NOTE : Above rules can be defined in one sentence like below, if you want them to share the same error message
            //Property(vm => vm.Code).Is.Required().And.Has.MaxLength(3).Otherwise("Code is required and its length must not eceed 3 characters.");
            Property <int>(vm => vm.Age).Is.BetweenValues(1, 50).Otherwise("Age must be between 1 and 50.");
            Property <Country>(vm => vm.Country).Is.EqualTo(Database.GetCountry(4)).Otherwise("Country other than Madagascar is not allowed.");

            // Example of more complex Fluent rules
            Property <DateTime>(vm => vm.DateOfBirth)
            .Is.GreaterThan(new DateTime(1990, 01, 01))
            .Otherwise("Date of Birth must be after 1990");
            Property <DateTime>(vm => vm.DateOfBirth)
            .Is.LessThanOrEqualTo(vm => vm.DateOfHire)
            .Otherwise("Date of Birth must preced the Date of Hire");
            Property <DateTime>(vm => vm.DateOfHire)
            .Is.BetweenPoperties(vm => vm.DateOfBirth, vm => vm.DateOfDeath)
            .Otherwise("Date of Hire must be between Date of Birth and Date of Death.");
        }
コード例 #2
0
        private void BindToModelInstance(EAV.Model.IModelContainer container, EAV.Model.IModelSubject subject, EAV.Model.IModelInstance parentInstance, EAV.Model.IModelInstance instance, ViewModelInstance viewParentInstance, ViewModelInstance viewInstance)
        {
            if (instance == null && viewInstance == null)
            {
                return;
            }

            if (instance != null && viewInstance == null)
            {
                if (instance.ObjectState == ObjectState.New)
                {
                    if (parentInstance != null)
                    {
                        parentInstance.ChildInstances.Remove(instance as IModelChildInstance);
                    }
                    else
                    {
                        subject.Instances.Remove(instance as IModelRootInstance);
                    }
                }
                else
                {
                    instance.MarkDeleted();
                }

                return;
            }
            else if (instance == null && viewInstance != null)
            {
                if (parentInstance != null)
                {
                    instance = new ModelChildInstance()
                    {
                        Container = container, InstanceID = viewInstance.InstanceID, ParentInstance = parentInstance
                    };
                }
                else
                {
                    instance = new ModelRootInstance()
                    {
                        Container = container, InstanceID = viewInstance.InstanceID, Subject = subject
                    };
                }
            }

            foreach (ModelAttribute attribute in container.Attributes)
            {
                BindToModelValue(attribute, instance, instance.Values.SingleOrDefault(it => it.AttributeID == attribute.AttributeID), viewInstance.Values.SingleOrDefault(it => it.AttributeID == attribute.AttributeID));
            }

            foreach (IModelContainer childContainer in container.ChildContainers)
            {
                var dataInstanceSubset = childContainer.Instances.Where(it => it.ParentInstanceID == instance.InstanceID);
                var viewInstanceSubset = viewInstance.ChildContainers.Where(it => it.ContainerID == childContainer.ContainerID).SelectMany(it => it.Instances).Where(it => it.ParentInstanceID == viewInstance.InstanceID);

                var dataInstances = dataInstanceSubset.GroupJoin(viewInstanceSubset, left => left.InstanceID, right => right.InstanceID, (left, right) => new { ModelInstance = left, ViewInstance = right.FirstOrDefault() }).ToArray();
                var viewInstances = viewInstanceSubset.GroupJoin(dataInstanceSubset, left => left.InstanceID, right => right.InstanceID, (left, right) => new { ModelInstance = right.FirstOrDefault(), ViewInstance = left }).ToArray();

                foreach (var item in dataInstances.Union(viewInstances))
                {
                    BindToModelInstance(childContainer, subject, instance, item.ModelInstance, viewInstance, item.ViewInstance);
                }
            }
        }