- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在使用验证应用程序块的 4.1 版。我对它比较陌生,所以我想知道它是否有能力抽象出配置的命名空间和程序集,或者以其他方式提供它们存在的正确验证?
我们最近遇到了一个问题,即有人移动了一个类并且没有使用新的命名空间更新验证配置文件。因此,验证不再应用于对象。应用程序块似乎只是忽略了这些差异。不幸的是,这在正常的 QA 周期中没有被发现。是否有任何内置方法可以保护我们自己免受 future 此类变化的影响?我在此期间所做的是加载配置 xml,提取所有程序集和定义的命名空间并验证它们是否都存在。
最佳答案
编写一组单元测试以查看对象是否得到正确验证。
另一种选择是以流畅的方式动态构建 vab 配置。这将为您提供编译时支持和重构安全性。
这只有一个问题:vab has no support for this out of the box .你必须自己写这个。创建具有此类功能的 VAB contrib 项目已在我的待办事项列表中多年,但我从未有时间这样做。不过也没有那么难。这是我几年前写的代码(但一直没有时间完成它):
public interface IValidationConfigurationMemberCreator<TEntity>
{
ValidationConfigurationBuilderRuleset<TEntity> BuilderRuleset { get; }
}
public static class ValidationConfigurationBuilderExtensions
{
public static ValidationConfigurationBuilderProperty<TEntity, TField> ForField<TEntity, TField>(
this IValidationConfigurationMemberCreator<TEntity> memberCreator,
Expression<Func<TEntity, TField>> fieldSelector)
{
string fieldName = ExtractFieldName(fieldSelector);
return new ValidationConfigurationBuilderProperty<TEntity, TField>(memberCreator.BuilderRuleset,
fieldName);
}
public static ValidationConfigurationBuilderProperty<TEntity, TProperty>
ForProperty<TEntity, TProperty>(
this IValidationConfigurationMemberCreator<TEntity> memberCreator,
Expression<Func<TEntity, TProperty>> propertySelector)
{
string propertyName = ExtractPropertyName(propertySelector);
return new ValidationConfigurationBuilderProperty<TEntity, TProperty>(memberCreator.BuilderRuleset,
propertyName);
}
private static string ExtractPropertyName(LambdaExpression propertySelector)
{
if (propertySelector == null)
{
throw new ArgumentNullException("propertySelector");
}
var body = propertySelector.Body as MemberExpression;
if (body == null || body.Member.MemberType != MemberTypes.Property)
{
throw new ArgumentException("The given expression should return a property.",
"propertySelector");
}
return body.Member.Name;
}
private static string ExtractFieldName(LambdaExpression fieldSelector)
{
if (fieldSelector == null)
{
throw new ArgumentNullException("fieldSelector");
}
var body = fieldSelector.Body as MemberExpression;
if (body == null || body.Member.MemberType != MemberTypes.Field)
{
throw new ArgumentException("The given expression should return a field.",
"fieldSelector");
}
return body.Member.Name;
}
public static ValidationConfigurationBuilderMember<TEntity, TMember> AddRangeValidator<TEntity, TMember>(
this ValidationConfigurationBuilderMember<TEntity, TMember> memberBuilder,
RangeData<TMember> rangeData) where TMember : IComparable
{
memberBuilder.AddValidator(rangeData.CreateValidator());
return memberBuilder;
}
}
public class ValidationConfigurationBuilder : IConfigurationSource
{
private readonly ValidationSettings settings;
private readonly HashSet<string> alternativeRulesetNames = new HashSet<string>(StringComparer.Ordinal);
private string defaultRulesetName;
public ValidationConfigurationBuilder()
{
this.settings = new ValidationSettings();
}
public event EventHandler<ConfigurationSourceChangedEventArgs> SourceChanged;
public void RegisterDefaultRulesetForAllTypes(string rulesetName)
{
if (string.IsNullOrEmpty(rulesetName))
{
throw new ArgumentNullException("rulesetName");
}
if (this.settings.Types.Count > 0)
{
throw new InvalidOperationException("Registeringen rulesets for all types is not possible " +
"after types are registered.");
}
this.defaultRulesetName = rulesetName;
}
public void RegisterAlternativeRulesetForAllTypes(string rulesetName)
{
if (string.IsNullOrEmpty(rulesetName))
{
throw new ArgumentNullException("rulesetName");
}
if (this.settings.Types.Count > 0)
{
throw new InvalidOperationException("Registeringen rulesets for all types is not possible " +
"after types are registered.");
}
if (this.alternativeRulesetNames.Contains(rulesetName))
{
throw new InvalidOperationException("There already is a ruleset with this name: " +
rulesetName);
}
this.alternativeRulesetNames.Add(rulesetName);
}
public ValidationConfigurationBuilderType<T> ForType<T>()
{
ValidatedTypeReference typeReference;
if (this.settings.Types.Contains(typeof(T).FullName))
{
typeReference = this.settings.Types.Get(typeof(T).FullName);
}
else
{
typeReference = new ValidatedTypeReference(typeof(T))
{
AssemblyName = typeof(T).Assembly.GetName().FullName,
};
if (this.defaultRulesetName != null)
{
typeReference.Rulesets.Add(new ValidationRulesetData(this.defaultRulesetName));
typeReference.DefaultRuleset = this.defaultRulesetName;
}
foreach (var alternativeRulesetName in this.alternativeRulesetNames)
{
typeReference.Rulesets.Add(new ValidationRulesetData(alternativeRulesetName));
}
this.settings.Types.Add(typeReference);
}
return new ValidationConfigurationBuilderType<T>(this, typeReference);
}
ConfigurationSection IConfigurationSource.GetSection(string sectionName)
{
if (sectionName == ValidationSettings.SectionName)
{
return this.settings;
}
return null;
}
#region IConfigurationSource Members
void IConfigurationSource.Add(string sectionName, ConfigurationSection configurationSection)
{
throw new NotImplementedException();
}
void IConfigurationSource.AddSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
{
throw new NotImplementedException();
}
void IConfigurationSource.Remove(string sectionName)
{
throw new NotImplementedException();
}
void IConfigurationSource.RemoveSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
{
throw new NotImplementedException();
}
void IDisposable.Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
protected virtual void Dispose(bool disposing)
{
}
}
public class ValidationConfigurationBuilderType<TEntity>
: IValidationConfigurationMemberCreator<TEntity>
{
internal ValidationConfigurationBuilderType(ValidationConfigurationBuilder builder,
ValidatedTypeReference typeReference)
{
this.Builder = builder;
this.TypeReference = typeReference;
}
ValidationConfigurationBuilderRuleset<TEntity> IValidationConfigurationMemberCreator<TEntity>.BuilderRuleset
{
get { return this.ForDefaultRuleset(); }
}
internal ValidationConfigurationBuilder Builder { get; private set; }
internal ValidatedTypeReference TypeReference { get; private set; }
public ValidationConfigurationBuilderRuleset<TEntity> ForDefaultRuleset()
{
if (string.IsNullOrEmpty(this.TypeReference.DefaultRuleset))
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"There hasn't been an default ruleset registered for the type {0}.", typeof(TEntity).FullName));
}
var defaultRuleset = this.TypeReference.Rulesets.Get(this.TypeReference.DefaultRuleset);
if (defaultRuleset == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"The default ruleset with name {0} is missing from type {1}.",
this.TypeReference.DefaultRuleset, typeof(TEntity).FullName));
}
return new ValidationConfigurationBuilderRuleset<TEntity>(this, defaultRuleset);
}
public ValidationConfigurationBuilderRuleset<TEntity> ForRuleset(string rulesetName)
{
var ruleset = this.TypeReference.Rulesets.Get(rulesetName);
if (ruleset == null)
{
ruleset = new ValidationRulesetData(rulesetName);
this.TypeReference.Rulesets.Add(ruleset);
//throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
// "The ruleset with name '{0}' has not been registered yet for type {1}.",
// rulesetName, this.TypeReference.Name));
}
return new ValidationConfigurationBuilderRuleset<TEntity>(this, ruleset);
}
internal void CreateDefaultRuleset(string rulesetName)
{
if (this.TypeReference.Rulesets.Get(rulesetName) != null)
{
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
"The ruleset with name '{0}' has already been registered yet for type {1}.",
rulesetName, this.TypeReference.Name));
}
if (!string.IsNullOrEmpty(this.TypeReference.DefaultRuleset))
{
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
"The type {0} already has a default ruleset.", this.TypeReference.Name));
}
var ruleset = new ValidationRulesetData(rulesetName);
this.TypeReference.Rulesets.Add(ruleset);
this.TypeReference.DefaultRuleset = rulesetName;
}
internal void CreateAlternativeRuleset(string rulesetName)
{
if (this.TypeReference.Rulesets.Get(rulesetName) != null)
{
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
"The ruleset with name '{0}' has already been registered yet for type {1}.",
rulesetName, this.TypeReference.Name));
}
var ruleset = new ValidationRulesetData(rulesetName);
this.TypeReference.Rulesets.Add(ruleset);
}
}
public class ValidationConfigurationBuilderRuleset<TEntity> : IValidationConfigurationMemberCreator<TEntity>
{
internal ValidationConfigurationBuilderRuleset(
ValidationConfigurationBuilderType<TEntity> builderType,
ValidationRulesetData ruleset)
{
this.BuilderType = builderType;
this.Ruleset = ruleset;
}
internal ValidationConfigurationBuilderType<TEntity> BuilderType { get; private set; }
ValidationConfigurationBuilderRuleset<TEntity>
IValidationConfigurationMemberCreator<TEntity>.BuilderRuleset { get { return this; } }
internal ValidationRulesetData Ruleset { get; private set; }
internal ValidationConfigurationBuilderRuleset<TEntity> AddValidator(ValidatorData validator)
{
if (validator == null)
{
throw new ArgumentNullException("validator");
}
// 'Name' is the default value when the validator has not been given a name.
if (validator.Name == "Name")
{
// In that case we set the name to something more specific.
validator.Name = typeof(TEntity).Name + "_" + validator.Type.Name;
}
var validators = this.Ruleset.Validators;
// When that specific name already exist, we add a number to that name to ensure uniqueness.
if (validators.Contains(validator.Name))
{
validator.Name += "_" + validators.Count.ToString();
}
validators.Add(validator);
return this;
}
}
public abstract class ValidationConfigurationBuilderMember<TEntity, TMember>
: IValidationConfigurationMemberCreator<TEntity>
{
private readonly ValidationConfigurationBuilderRuleset<TEntity> builderRuleset;
internal ValidationConfigurationBuilderMember(
ValidationConfigurationBuilderRuleset<TEntity> builderRuleset, string memberName)
{
this.builderRuleset = builderRuleset;
this.MemberName = memberName;
}
ValidationConfigurationBuilderRuleset<TEntity>
IValidationConfigurationMemberCreator<TEntity>.BuilderRuleset { get { return this.builderRuleset; } }
internal ValidationConfigurationBuilderRuleset<TEntity> BuilderRuleset
{
get { return this.builderRuleset; }
}
internal string MemberName { get; private set; }
public ValidationConfigurationBuilderMember<TEntity, TMember> AddValidator(Validator validator)
{
if (validator == null)
{
throw new ArgumentNullException("validator");
}
return AddValidator(new SingletonValidatorData<TEntity>(validator));
}
public ValidationConfigurationBuilderMember<TEntity, TMember> AddValidator(ValidatorData validatorData)
{
if (validatorData == null)
{
throw new ArgumentNullException("validatorData");
}
var memberReference = this.GetOrCreateMemberReference();
// 'Name' is the default value when the validator has not been given a name.
if (validatorData.Name == "Name")
{
// In that case we set the name to something more specific.
validatorData.Name = typeof(TEntity).Name + "_" +
this.BuilderRuleset.Ruleset.Name + "_" + validatorData.Type.Name;
}
// When that specific name already exist, we add a number to that name to ensure uniqueness.
if (memberReference.Validators.Contains(validatorData.Name))
{
validatorData.Name += "_" + memberReference.Validators.Count.ToString();
}
memberReference.Validators.Add(validatorData);
return this;
}
internal abstract ValidatedMemberReference GetOrCreateMemberReference();
}
public class ValidationConfigurationBuilderProperty<TEntity, TProperty>
: ValidationConfigurationBuilderMember<TEntity, TProperty>
{
internal ValidationConfigurationBuilderProperty(
ValidationConfigurationBuilderRuleset<TEntity> builderRuleset, string propertyName)
: base(builderRuleset, propertyName)
{
}
internal override ValidatedMemberReference GetOrCreateMemberReference()
{
var properties = this.BuilderRuleset.Ruleset.Properties;
var propertyReference = properties.Get(this.MemberName);
if (propertyReference == null)
{
propertyReference = new ValidatedPropertyReference(this.MemberName);
properties.Add(propertyReference);
}
return propertyReference;
}
}
public class ValidationConfigurationBuilderField<TEntity, TField>
: ValidationConfigurationBuilderMember<TEntity, TField>
{
internal ValidationConfigurationBuilderField(
ValidationConfigurationBuilderRuleset<TEntity> builderRuleset, string fieldName)
: base(builderRuleset, fieldName)
{
}
internal override ValidatedMemberReference GetOrCreateMemberReference()
{
var fields = this.BuilderRuleset.Ruleset.Fields;
var fieldReference = fields.Get(this.MemberName);
if (fieldReference == null)
{
fieldReference = new ValidatedFieldReference(this.MemberName);
fields.Add(fieldReference);
}
return fieldReference;
}
}
internal class SingletonValidatorData<TEntity> : ValidatorData
{
private readonly Validator validator;
public SingletonValidatorData(Validator validator)
: base(typeof(TEntity).Name + "ValidatorData", typeof(TEntity))
{
this.validator = validator;
}
protected override Validator DoCreateValidator(Type targetType)
{
return this.validator;
}
}
[DebuggerDisplay("Range (LowerBound: {LowerBound}, LowerBoundType: {LowerBoundType}, UpperBound: {UpperBound}, UpperBoundType: {UpperBoundType})")]
public class RangeData<T> where T : IComparable
{
private T lowerBound;
private RangeBoundaryType lowerBoundType;
private bool lowerBoundTypeSet;
private T upperBound;
private RangeBoundaryType upperBoundType;
private bool upperBoundTypeSet;
public T LowerBound
{
get
{
return this.lowerBound;
}
set
{
this.lowerBound = value;
if (!this.lowerBoundTypeSet)
{
this.lowerBoundType = RangeBoundaryType.Inclusive;
}
}
}
public RangeBoundaryType LowerBoundType
{
get
{
return this.lowerBoundType;
}
set
{
this.lowerBoundType = value;
this.lowerBoundTypeSet = true;
}
}
public T UpperBound
{
get
{
return this.upperBound;
}
set
{
this.upperBound = value;
if (!this.upperBoundTypeSet)
{
this.upperBoundType = RangeBoundaryType.Inclusive;
}
}
}
public RangeBoundaryType UpperBoundType
{
get
{
return this.upperBoundType;
}
set
{
this.upperBoundType = value;
this.upperBoundTypeSet = true;
}
}
public bool Negated { get; set; }
public virtual string MessageTemplate { get; set; }
public virtual string MessageTemplateResourceName { get; set; }
public virtual string MessageTemplateResourceTypeName { get; set; }
public virtual string Tag { get; set; }
internal RangeValidator CreateValidator()
{
return new RangeValidator(this.LowerBound, this.LowerBoundType, this.UpperBound,
this.UpperBoundType, this.GetMessageTemplate(), this.Negated)
{
Tag = this.Tag,
};
}
internal string GetMessageTemplate()
{
if (!string.IsNullOrEmpty(this.MessageTemplate))
{
return this.MessageTemplate;
}
Type messageTemplateResourceType = this.GetMessageTemplateResourceType();
if (messageTemplateResourceType != null)
{
return ResourceStringLoader.LoadString(messageTemplateResourceType.FullName,
this.MessageTemplateResourceName, messageTemplateResourceType.Assembly);
}
return null;
}
private Type GetMessageTemplateResourceType()
{
if (!string.IsNullOrEmpty(this.MessageTemplateResourceTypeName))
{
return Type.GetType(this.MessageTemplateResourceTypeName);
}
return null;
}
}
const string DefaultRuleset = "Default";
const string AlternativeRuleset = "Alternative";
var builder = new ValidationConfigurationBuilder();
builder.RegisterDefaultRulesetForAllTypes(DefaultRuleset);
builder.ForType<Person>()
.ForProperty(p => p.Age)
.AddRangeValidator(new RangeData<int>()
{
LowerBound = 18,
MessageTemplate = "This is an adult system",
})
.ForProperty(p => p.FirstName)
.AddValidator(new NotNullValidator())
.AddValidator(new StringLengthValidatorData()
{
LowerBound = 1,
LowerBoundType = RangeBoundaryType.Inclusive,
UpperBound = 100,
UpperBoundType = RangeBoundaryType.Inclusive
})
.ForProperty(p => p.LastName)
.AddValidator(new NotNullValidator())
.ForProperty(p => p.Friends)
.AddValidator(new ObjectCollectionValidator());
builder.ForType<Person>().ForRuleset(AlternativeRuleset)
.ForProperty(p => p.FirstName)
.AddValidator(new NotNullValidator())
.AddValidator(new StringLengthValidatorData()
{
LowerBound = 1,
LowerBoundType = RangeBoundaryType.Inclusive,
UpperBound = 10,
UpperBoundType = RangeBoundaryType.Inclusive
});
ValidationConfigurationBuilder
是
IConfigurationSource
您可以将其提供给
ValidationFactory.CreateValidator
实例。下面是一个例子:
var validator = ValidationFactory.CreateValidator<Person>(builder);
var instance = new Person();
var results = validator.Validate(instance);
关于c# - 验证 VAB 配置文件中的程序集和命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8900510/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!