当前位置: 首页>>代码示例>>C#>>正文


C# ValidationContext.GetService方法代码示例

本文整理汇总了C#中System.ComponentModel.DataAnnotations.ValidationContext.GetService方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext.GetService方法的具体用法?C# ValidationContext.GetService怎么用?C# ValidationContext.GetService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.ComponentModel.DataAnnotations.ValidationContext的用法示例。


在下文中一共展示了ValidationContext.GetService方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetValidationService

 private static IValidationService GetValidationService(ValidationContext validationContext)
 {
     var service = ((IValidationService)validationContext.GetService(typeof(IValidationService)));
     if (service == null)
         service = Factories.BuildValidationService();
     return service;
 }
开发者ID:flobacher,项目名称:unobtrusive-angular-validation,代码行数:7,代码来源:ValidateObjectAttribute.cs

示例2: IsValid

 /// <summary>
 /// Validates the specified value with respect to the current validation attribute.
 /// </summary>
 /// <param name="value"></param>
 /// <param name="validationContext"></param>
 /// <returns></returns>
 protected override ValidationResult IsValid(object value, ValidationContext validationContext)
 {
     if (value == null)
         return null;
     IEntity entity = (IEntity)validationContext.ObjectInstance;
     dynamic entityContext;
     Type type = validationContext.ObjectType;
     while (type.Assembly.IsDynamic)
         type = type.BaseType;
     entityContext = validationContext.GetService(typeof(IEntityQueryable<>).MakeGenericType(type));
     if (entityContext == null)
         return null;
     if (value is string && IsCaseSensitive)
         value = ((string)value).ToLower();
     ParameterExpression parameter = Expression.Parameter(type);
     Expression left = Expression.NotEqual(Expression.Property(parameter, "Index"), Expression.Constant(entity.Index));
     Expression right;
     if (value is string && IsCaseSensitive)
         right = Expression.Equal(Expression.Call(Expression.Property(parameter, validationContext.MemberName), typeof(string).GetMethod("ToLower")), Expression.Constant(value));
     else
         right = Expression.Equal(Expression.Property(parameter, validationContext.MemberName), Expression.Constant(value));
     Expression expression = Expression.And(left, right);
     expression = Expression.Lambda(typeof(Func<,>).MakeGenericType(type, typeof(bool)), expression, parameter);
     object where = _QWhereMethod.MakeGenericMethod(type).Invoke(null, new[] { entityContext.Query(), expression });
     int count = (int)_QCountMethod.MakeGenericMethod(type).Invoke(null, new[] { where });
     if (count != 0)
         return new ValidationResult(string.Format("{0} can not be {1}, there is a same value in the database.", validationContext.MemberName, value));
     else
         return null;
 }
开发者ID:alexyjian,项目名称:ComBoost,代码行数:36,代码来源:DistinctAttribute.cs

示例3: IsValid

 protected override ValidationResult IsValid(object value, ValidationContext validationContext)
 {
     var email = value as String;
     if (email == null)
     {
         throw new ArgumentException("Property type is not String");
     }
     var repository = validationContext.GetService(typeof(IUserRepository)) as IUserRepository;
     if (repository == null)
     {
         throw new InvalidOperationException("Can't access repository");
     }
     var user = AsyncHelper.RunSynchronously(() => repository.GetUserByEmail(email));
     if (user != null)
     {
         return new ValidationResult(String.Format("This email '{0}' has taken by another user", email), new [] {validationContext.DisplayName});
     }
     return null;
 }
开发者ID:WadeOne,项目名称:EasyTeach,代码行数:19,代码来源:UniqueEmailAttribute.cs

示例4: IsValidLocation

        public static ValidationResult IsValidLocation(string location, ValidationContext validationContext)
        {
            if (!string.IsNullOrEmpty(location))
            {
                var meetingData = validationContext.GetService(typeof(IMeetingDataProvider))
                    as IMeetingDataProvider;

                if (meetingData != null)
                {
                    if (!meetingData.Locations.Any(l => l.LocationName == location))
                    {
                        return new ValidationResult(
                            "That is not a valid location",
                            new[] { validationContext.MemberName });
                    }
                }
            }

            return ValidationResult.Success;
        }
开发者ID:jeffhandley,项目名称:RIAServicesValidation,代码行数:20,代码来源:MeetingValidators.shared.cs

示例5: AdaptShouldReturnValidationContextAdapter

        public void AdaptShouldReturnValidationContextAdapter()
        {
            // arrange
            var instance = new object();
            var service = new object();
            var serviceProvider = new ServiceContainer();
            var items = new Dictionary<object, object>() { { "Test", "Test" } };
            var expected = new ValidationContext( instance, serviceProvider, items );

            expected.MemberName = "Foo";
            serviceProvider.AddService( typeof( object ), service );

            // act
            var actual = expected.Adapt();

            // assert
            Assert.Equal( expected.DisplayName, actual.DisplayName );
            Assert.Same( expected.Items["Test"], actual.Items["Test"] );
            Assert.Equal( expected.MemberName, actual.MemberName );
            Assert.Same( expected.ObjectInstance, actual.ObjectInstance );
            Assert.Equal( expected.ObjectType, actual.ObjectType );
            Assert.Same( expected.GetService( typeof( object ) ), actual.GetService( typeof( object ) ) );
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:23,代码来源:ValidationAdapterExtensionsTest.cs

示例6: IsValid

        /// <summary>
        /// 現在の検証属性に対して、指定した値を検証します。
        /// </summary>
        /// <param name="value">検証対象の値。</param>
        /// <param name="validationContext">検証操作に関するコンテキスト情報。</param>
        /// <returns>
        ///   <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> クラスのインスタンス。
        /// </returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            validator = validationContext.GetService(typeof(IPersonNameValidator)) as IPersonNameValidator;

            return base.IsValid(value, validationContext);
        }
开发者ID:miso-soup,项目名称:CustomModelValidatorTry,代码行数:14,代码来源:TwitterUserNameAttribute.cs

示例7: ValidateViewModelName

        public static ValidationResult ValidateViewModelName( string value, ValidationContext context )
        {
            Arg.NotNull( context, nameof( context ) );

            var model = context.ObjectInstance as ViewItemTemplateWizardViewModel;

            if ( model == null )
                return new ValidationResult( SR.InvalidObjectForValidation.FormatDefault( typeof( ViewItemTemplateWizardViewModel ) ) );

            switch ( model.ViewModelOption )
            {
                case 1:
                    {
                        // must have the name of the new view model
                        if ( string.IsNullOrEmpty( value ) )
                            return new ValidationResult( SR.ViewModelNameUnspecified, new[] { "ViewModelName" } );

                        var validator = context.GetService<IProjectItemNameValidator>();

                        // the specified view model name must be unique
                        if ( validator != null && !validator.IsItemNameUnique( value ) )
                            return new ValidationResult( SR.ViewModelNameNonUnique.FormatDefault( value ), new[] { "ViewModelName" } );

                        break;
                    }
                case 2:
                    {
                        // must have a selected view model
                        if ( string.IsNullOrEmpty( value ) )
                            return new ValidationResult( SR.ExistingViewModelUnspecified, new[] { "ViewModelName" } );

                        break;
                    }
            }

            return ValidationResult.Success;
        }
开发者ID:WaffleSquirrel,项目名称:More,代码行数:37,代码来源:ViewItemTemplateWizardViewModel.cs

示例8: PreventDoubleBooking

        public static ValidationResult PreventDoubleBooking(Meeting meeting, ValidationContext validationContext)
        {
            if (validationContext.Items.ContainsKey("AllowOverBooking"))
            {
                bool allowOverBooking = (bool)validationContext.Items["AllowOverBooking"];

                if (!allowOverBooking)
                {
                    var meetingData = validationContext.GetService(typeof(IMeetingDataProvider))
                        as IMeetingDataProvider;

                    if (meetingData != null)
                    {
                        var conflicts = from other in meetingData.Meetings.Except(new[] { meeting })
                                        where other.Location == meeting.Location
                                        // Check for conflicts by seeing if the times overlap in any way
                                        && (
                                            (other.Start >= meeting.Start && other.Start <= meeting.End) ||
                                            (meeting.Start >= other.Start && meeting.Start <= other.End) ||
                                            (other.End >= meeting.Start && other.End <= meeting.End) ||
                                            (meeting.End >= other.Start && meeting.End <= other.End)
                                            )
                                        select other;

                        if (conflicts.Any())
                        {
                            return new ValidationResult(
                                "The location selected is already booked at this time.",
                                new[] { "Location", "Start", "End" });
                        }
                    }
                }
            }

            return ValidationResult.Success;
        }
开发者ID:jeffhandley,项目名称:RIAServicesValidation,代码行数:36,代码来源:MeetingValidators.shared.cs


注:本文中的System.ComponentModel.DataAnnotations.ValidationContext.GetService方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。