How can I create a generic UniqueValidationAttribute in C# and DataAnnotation?

后端 未结 4 664
感动是毒
感动是毒 2021-02-03 15:44

I\'m trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute

I want this to be generic as in I c

4条回答
  •  情深已故
    2021-02-03 16:10

    I edited this one..and it works perfectly with DI..:D

    public class UniqueAttribute : ValidationAttribute
    {
        public UniqueAttribute(Type dataContextType, Type entityType, string propertyName)
        {
            DataContextType = dataContextType;
            EntityType = entityType;
            PropertyName = propertyName;
        }
    
    
        public Type DataContextType { get; private set; }
    
    
        public Type EntityType { get; private set; }
    
    
        public string PropertyName { get; private set; }
    
    
        public override bool IsValid(object value)
        {
            // Construct the data context
            //ConstructorInfo constructor = DataContextType.GetConstructor(new Type[0]);
            //DataContext dataContext = (DataContext)constructor.Invoke(new object[0]);
            var repository = DependencyResolver.Current.GetService(DataContextType);
            var data = repository.GetType().InvokeMember("GetAll", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, repository, null);
    
            // Get the table
            //ITable table = dataContext.GetTable(EntityType);
    
    
            // Get the property
            PropertyInfo propertyInfo = EntityType.GetProperty(PropertyName);
    
    
            // Our ultimate goal is an expression of:
            //   "entity => entity.PropertyName == value"
    
    
            // Expression: "value"
            object convertedValue = Convert.ChangeType(value, propertyInfo.PropertyType);
            var rhs = Expression.Constant(convertedValue);
    
    
            // Expression: "entity"
            var parameter = Expression.Parameter(EntityType, "entity");
    
    
            // Expression: "entity.PropertyName"
            var property = Expression.MakeMemberAccess(parameter, propertyInfo);
    
    
            // Expression: "entity.PropertyName == value"
            var equal = Expression.Equal(property, rhs);
    
    
            // Expression: "entity => entity.PropertyName == value"
            var lambda = Expression.Lambda(equal, parameter).Compile();
    
            // Instantiate the count method with the right TSource (our entity type)
            MethodInfo countMethod = QueryableCountMethod.MakeGenericMethod(EntityType);
    
            // Execute Count() and say "you're valid if you have none matching"
            int count = (int)countMethod.Invoke(null, new object[] { data, lambda });
            return count == 0;
        }
    
    
        // Gets Queryable.Count(IQueryable, Expression>)
        //private static MethodInfo QueryableCountMethod = typeof(Enumerable).GetMethods().First(m => m.Name == "Count" && m.GetParameters().Length == 2);
        private static MethodInfo QueryableCountMethod = typeof(System.Linq.Enumerable).GetMethods().Single(
            method => method.Name == "Count" && method.IsStatic && method.GetParameters().Length == 2);
    }
    

提交回复
热议问题