查询具有可变对象类型的实体DbSet

时间:2018-09-21 07:32:35

标签: c# entity-framework types dbset

我有一个以MAIL_DRIVER=smtp MAIL_HOST=smtp.office365.com MAIL_PORT=587 MAIL_USERNAME=support@xxxxx.com MAIL_PASSWORD=XXXXXX MAIL_ENCRYPTION=STARTTLS 作为参数的方法。

这是我想要达到的目标的一个例子。

目标是检查ValidationContext中是否有重复项,如果是DbSet<Vehicule>,是否在Vehicule上有重复项,等等...

DbSet<Color>

这里我使用的是switch语句,该语句正在运行,但是有没有最干净的解决方案?

2 个答案:

答案 0 :(得分:1)

您可以考虑使用Polymorph代替Ifswitch case

ModelValidBaseVehicule两种类型创建基类Color

在构造函数方法中传递参数,您将在以后使用。

  1. ValidationContext v validationContext。
  2. object p有效参数。

有一个abstract ValidationResult Dosomthing()的fof子类要实现

public abstract class ModelValidBase {
    protected ValidationContext _validContext;
    protected object parameterValue;
    public ModelValidBase(ValidationContext v, object p)
    {
        _validContext = v;
        parameterValue = p;
    }
    public abstract ValidationResult Dosomthing();
}

VehiculeContext实现ModelValidBase类和override ValidationResult Dosomthing,使您的逻辑成为此类

public class VehiculeContext : ModelValidBase
{
    public VehiculeContext(ValidationContext v, object p) : base(v, p)
    {
    }

    public override ValidationResult Dosomthing()
    {
        MultiRetouchesEntities db = new MultiRetouchesEntities();
        Vehicule vehicule = (Vehicule)(_validContext.ObjectInstance) ;

        bool validateName = db.Vehicule.Any(x => x.Name == (string)parameterValue);
        if (validateName == true)
        {
            return new ValidationResult("This vehicule already exists", new string[] { "Name" });
        }

        return ValidationResult.Success;
    }
}

ColorContext实现ModelValidBase类和override ValidationResult Dosomthing,使您的逻辑成为此类

public class ColorContext : ModelValidBase
{
    public ColorContext(ValidationContext v, object p) : base(v, p)
    {
    }

    public override ValidationResult Dosomthing()
    {
        Color vehicule = (Color)_validContext;
        //Do something with db.Color, for exemple
        db.Color.Add(Color);

        return ValidationResult.Success;
    }
}

在最后一步中,创建dictionary<string, ModelValidBase>并将VehiculeContextColorContext注册到字典中。

您可以通过字符串名称获取实例,而该字符串名称可以代替if elseswitch case控制流

public class MyCustomerAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        Dictionary<string, ModelValidBase> _registerTable = new Dictionary<string, ModelValidBase>();
        _registerTable.Add("Vehicule", new VehiculeContext(validationContext,value));
        _registerTable.Add("Color", new ColorContext(validationContext, value));
        Type testType = validationContext.ObjectInstance.GetType();

        ModelValidBase excuteValid;

        if (!_registerTable.TryGetValue(testType.Name, out excuteValid))
        {
            //return a result when you didn't get context from the register table.
        }

        return excuteValid.Dosomthing();
    }
}

答案 1 :(得分:-1)

对于那些感兴趣的人,这就是我的解决方法。

要点是使用System.Linq.Dynamic

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    MultiRetouchesEntities db = new MultiRetouchesEntities();

    Type entityType = validationContext.ObjectInstance.GetType();
    string propertyName= validationContext.ObjectType.GetProperty(validationContext.MemberName).Name.ToLower();

    int nbOfEntities = db.Set(entityType).Where(propertyName + "=@0",((string)value).ToLower()).Count();

    return nbOfEntities == 0 ? ValidationResult.Success : new ValidationResult("Le nom choisi existe déjà !");
}