-
Notifications
You must be signed in to change notification settings - Fork 11
/
ValidationAspect.cs
35 lines (33 loc) · 1.11 KB
/
ValidationAspect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Castle.DynamicProxy;
using Core.CrossCuttingConcerns.Validation;
using Core.Utilities.Interceptors;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Core.Aspects.Autofac.Validation
{
public class ValidationAspect : MethodInterception
{
private Type _validatorType;
public ValidationAspect(Type validatorType)
{
if (!typeof(IValidator).IsAssignableFrom(validatorType))
{
throw new System.Exception("Bu bir doğrulama sınıfı değil!");
}
_validatorType = validatorType;
}
protected override void OnBefore(IInvocation invocation)
{
var validator = (IValidator)Activator.CreateInstance(_validatorType);
var entityType = _validatorType.BaseType.GetGenericArguments()[0];
var entities = invocation.Arguments.Where(t => t.GetType() == entityType);
foreach (var entity in entities)
{
ValidationTool.Validate(validator, entity);
}
}
}
}