Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
NHail.ComponentModel.DataAnnotations.Fluent/AttributeConfiguration.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
59 lines (50 sloc)
1.74 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace NHail.ComponentModel.DataAnnotations.Fluent | |
{ | |
public class AttributeConfiguration<TSource> : IAttributeConfiguration<TSource> | |
{ | |
private readonly AttributeProvider<TSource> _provider; | |
public AttributeConfiguration() : this(new AttributeProvider<TSource>()) | |
{ | |
} | |
public AttributeConfiguration(AttributeProvider<TSource> provider) | |
{ | |
_provider = provider; | |
} | |
public IAttributeConfiguration<TSource> AddPropertyAttributes(string propertyName, params Attribute[] validations) | |
{ | |
if (propertyName == null) | |
{ | |
throw new ArgumentNullException(nameof(propertyName)); | |
} | |
foreach (var validation in validations) | |
{ | |
_provider.AddPropertyAttribute(propertyName, validation); | |
} | |
return this; | |
} | |
public IAttributeConfiguration<TSource> AddPropertyAttributes<TProperty>(Expression<Func<TSource, TProperty>> property, params Attribute[] validations) | |
{ | |
if (property == null) | |
{ | |
throw new ArgumentNullException(nameof(property)); | |
} | |
var propName = property.NameOf(); | |
return AddPropertyAttributes(propName, validations); | |
} | |
public IAttributeConfiguration<TSource> AddAttributes(params Attribute[] validations) | |
{ | |
foreach (var validation in validations) | |
{ | |
_provider.AddAttribute(validation); | |
} | |
return this; | |
} | |
} | |
} |