业务层中的C#唯一属性验证

时间:2018-09-06 17:32:15

标签: c# validation unique-constraint unique-key business-layer

我在业务层中使用FluentValidation和PostSharp。 我的数据库有一个 Admin 表,该表中的 UserName 列是唯一的。 我想用“ 方面”检查唯一性。 我的代码如下。

AdminValidator

public class AdminValidator : AbstractValidator<Admin>
{
    public AdminValidator(IEnumerable<Admin> admins)
    {
        RuleFor(x => x.Fullname).MaximumLength(50);
        RuleFor(x => x.Username).Matches(@"^\S{3,15}$").IsUnique(admins);
        RuleFor(x => x.Password).Matches(@"^\S{5,20}$");
    }
}

IsUnique 扩展方法

public static class Extensions
{
    public static IRuleBuilderOptions<TItem, TProperty> IsUnique<TItem, TProperty>(
        this IRuleBuilder<TItem, TProperty> ruleBuilder, IEnumerable<TItem> items)
        where TItem : class
    {
        return ruleBuilder.SetValidator(new UniqueValidator<TItem>(items));
    }
}

public class UniqueValidator<T> : PropertyValidator
    where T : class
{
    private readonly IEnumerable<T> _items;

    public UniqueValidator(IEnumerable<T> items)
        : base("{PropertyName} must be unique")
    {
        _items = items;
    }

    protected override bool IsValid(PropertyValidatorContext context)
    {
        var editedItem = context.Instance as T;
        var newValue = context.PropertyValue as string;
        var property = typeof(T).GetTypeInfo().GetDeclaredProperty(context.PropertyName);
        return _items.All(item =>
            item.Equals(editedItem) || property.GetValue(item).ToString() != newValue);
    }
}

FluentValidation方面

[Serializable]
public class FluentValidationAspect : OnMethodBoundaryAspect
{
    private readonly Type _validatorType;
    private readonly object[] _parameters;

    public FluentValidationAspect(Type validatorType, params object[] parameters)
    {
        _validatorType = validatorType;
        _parameters = parameters;
    }

    public override void OnEntry(MethodExecutionArgs args)
    {
        var validator = (IValidator)Activator.CreateInstance(_validatorType, _parameters);
        var entityType = _validatorType.BaseType?.GetGenericArguments()[0];
        var entities = args.Arguments.Where(x => x.GetType() == entityType);

        foreach (var entity in entities)
            ValidatorTool.FluentValidate(validator, entity);
    }
}

和AdminManager

public class AdminManager : IAdminService
{
    private readonly IAdminDal _adminDal;

    public AdminManager(IAdminDal adminDal)
    {
        _adminDal = adminDal;
    }

    public Admin GetByUsername(string username)
        => string.IsNullOrEmpty(username) ? null : _adminDal.Get(x => x.Username.Equals(username));

    [FluentValidationAspect(typeof(AdminValidator))]    // I need to pass IEnumerable<Admin> as a second parameter.
    public void Add(Admin admin) => _adminDal.Add(admin);

    [FluentValidationAspect(typeof(AdminValidator))]    // 
    public void Update(Admin admin) => _adminDal.Update(admin);

    public void DeleteById(int id) => _adminDal.Delete(new Admin { Id = id });
}

正如我在评论行中提到的,我需要将IEnumerable传递给FluentValidationAspect。 但是,动态参数不能传递给属性。 结果,我在这里被封锁了。 检查唯一性的最佳方法是什么?

预先感谢您的帮助? 最好的问候...

1 个答案:

答案 0 :(得分:0)

您不能在“属性”中传递动态值,因此必须在方法主体中进行验证。

public void Add(Admin admin)
{
    var admins = GetAll():
    var val = new AdminValidator(admins);

    validator.ValidateAndThrow(admin);
     _adminDal.Add(admin);


}