コード例 #1
0
        public void should_construct_with_resolver_and_enrich()
        {
            IMappingProvider provider = null;
            var continuationPolicy = new ContinuationPropertyMappingPolicy();
            var enumerablePolicy = new EnumerablePropertyMappingPolicy();
            var propertyPolicy = new PrimitiveOrStringPropertyMappingPolicy();
            var mappingPolicy = new MappingPolicySource(new List<IPropertyMappingPolicy> { propertyPolicy, continuationPolicy, enumerablePolicy });
            var resolver = new LambdaObjectResolver(r => r.DestinationType.Equals(typeof (Notification)), r => new Notification(r.SourceType));
            var enricher = new LambdaObjectEnricher<ModelWithErrors, Notification>((src, dest) => src
                                                                                                        .CustomErrors
                                                                                                        .Each(dest.RegisterError));
            var query = new MappingQuery(new List<IMappingPolicy> { mappingPolicy }, new List<IObjectResolver> { resolver }, new List<IObjectEnricher> { enricher });
            provider = new MappingProvider(query, new MappingContext((IMappingProvider)null));

            var model = new ModelWithErrors {CustomErrors = new List<string> {"Error 1", "Error 2"}};
            var notification = provider.Map<ModelWithErrors, Notification>(model);

            notification
                .Type
                .ShouldEqual(typeof (ModelWithErrors));

            notification
                .Messages
                .ShouldContain(m => m.Message == "Error 1");

            notification
                .Messages
                .ShouldContain(m => m.Message == "Error 2");
        }
コード例 #2
0
        public static ViewResult ViewFromErrors <T>(
            this Controller controller,
            ModelWithErrors <T> failed,
            HttpStatusCode statusCode = HttpStatusCode.BadRequest)
        {
            failed.ValidationResult.AddToModelState(controller.ModelState, null);

            var result = controller.View(failed.Model);

            result.StatusCode = (int)statusCode;
            return(result);
        }