如何在多个Controller类之间重用Action Method验证



我正在使用实体框架-6开发asp.net mvc-5 web应用程序。我已经使用实体框架映射了我的DB表,该框架生成了一个包含DBContext类的.edmx文件。目前,我有两个控制器类,一个用于管理服务器,另一个则用于管理vm。当添加/编辑服务器或虚拟机时,我想检查它们的ip&mac地址已经存在。目前,我正在对操作方法本身进行以下检查:-

public class Server : Controller{
    [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult Create(ServerJoin sj)
            {
                bool ITipunique = repository.ISITIPUnique(vmj.NetworkInfo2.FirstOrDefault().IPADDRESS);
                bool ITmacunique = repository.ISITMACUnique(vmj.NetworkInfo2.FirstOrDefault().MACADDRESS);
                bool Tipunique = repository.ISTIPUnique(vmj.NetworkInfo2.FirstOrDefault().IPADDRESS);
                bool Tmacunique = repository.ISTMACUnique(vmj.NetworkInfo2.FirstOrDefault().MACADDRESS);
                try
                {
                    if ((sj.IsIPUnique == true) && (!ITipunique || !Tipunique))
                    {
                        ModelState.AddModelError("NetworkInfo2[0].IPAddress", "Error occurred. The Same IP is already assigned.");
                    }
                    if ((sj.IsMACUnique == true) && (!ITmacunique || !Tmacunique))
                    {
                        ModelState.AddModelError("NetworkInfo2[0].MACAddress", "Error occurred. The Same MAC Address is already assigned.");
                    }

&

public class VM : Controlelr {    
    [HttpPost]
    [ValidateAntiForgeryToken]
            public ActionResult Create(VMJoin vmj)
            {
                bool ITipunique = repository.ISITIPUnique(vmj.NetworkInfo2.FirstOrDefault().IPADDRESS);
                bool ITmacunique = repository.ISITMACUnique(vmj.NetworkInfo2.FirstOrDefault().MACADDRESS);
                bool Tipunique = repository.ISTIPUnique(vmj.NetworkInfo2.FirstOrDefault().IPADDRESS);
                bool Tmacunique = repository.ISTMACUnique(vmj.NetworkInfo2.FirstOrDefault().MACADDRESS);
                try
                {
                    if ((vmj.IsIPUnique == true) && (!ITipunique || !Tipunique))
                    {
                        ModelState.AddModelError("NetworkInfo2[0].IPAddress", "Error occurred. The Same IP is already assigned.");
                    }
                    if ((vmj.IsMACUnique == true) && (!ITmacunique || !Tmacunique))
                    {
                        ModelState.AddModelError("NetworkInfo2[0].MACAddress", "Error occurred. The Same MAC Address is already assigned.");
                    }
                    if (!repository.IshypervisorServers(vmj.VirtualMachine.ServerID))
                    {
                        ModelState.AddModelError("VirtualMachine.ServerID", "Error occurred. Please select a valid hypervisor server. ");
                    }

这种方法运行良好,但我面临的问题是,我必须在所有相关的操作方法(主要是添加和编辑)和其他控制器类(服务器、vm、存储设备等)中重复这些验证,那么有没有一种方法可以更好地管理共享验证,从而促进重用和可维护性?

编辑

ServerJoin如下所示:-

public class ServerJoin : IValidatableObject
    {
        public Server Server { get; set; }
        public Resource Resource { get; set; }
        public Technology Technology { get; set; }
        public SDOrganization Site { get; set; }
        public SDOrganization Customer { get; set; }
        public NetworkInfo NetworkInfo { get; set; }
        public ICollection<NetworkInfo> NetworkInfo2 { get; set; }
        [Display(Name="Unique")]
        public bool IsMACUnique { get; set; }
        [Display(Name = "Unique")]
        public bool IsIPUnique { get; set; }
        public Nullable<double> SPEED { get; set; }
        public Nullable<Int64> PROCESSORCOUNT { get; set; }
        [Display(Name = "IP Unique")]
        public bool IsTIPUnique { get; set; }
        [Display(Name = "MAC Unique")]
        public bool IsTMACUnique { get; set; }
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Server != null)
            {

                if (Server.RackUnitID != null && Server.RackID == null)
                {
                    yield return new ValidationResult("Please select a Rack, or remove the current Rack Unit", new[] { "Server.RackUnitID" });
                }
                if (Server.RackUnitIDTo != null && Server.RackUnitID == null)
                {
                    yield return new ValidationResult("Please select a Rack From Value", new[] { "Server.RackUnitID" });
                }
                if (Server.RackUnitIDTo != null && Server.RackUnitID != null && Server.RackUnitID > Server.RackUnitIDTo)
                {
                    yield return new ValidationResult("Rack Unit From must be less than or equal Rack Unit To", new[] { "Server.RackUnitID" });
                }

            }

&

 public class VMJoin
    {
         public VirtualMachine VirtualMachine { get; set; }
         public Resource Resource { get; set; }
         public Technology Technology { get; set; }
         public SDOrganization Site { get; set; }
         public SDOrganization Customer { get; set; }
         public NetworkInfo NetworkInfo { get; set; }
         public ICollection<NetworkInfo> NetworkInfo2 { get; set; }
         public ICollection<TechnologyIP> TechnologyIP { get; set; }
         [Display(Name = "Unique")]
         public bool IsMACUnique { get; set; }
         [Display(Name = "Unique")]
         public bool IsIPUnique { get; set; }
         public Nullable<double> SPEED { get; set; }
         public TechnologyIP TechnologyIP2 { get; set; }
         [Display(Name = "IP Unique")]
         public bool IsTIPUnique  { get; set; }
         [Display(Name = "MAC Unique")]
         public bool IsTMACUnique { get; set; }
    }
}

首先为您的模型创建一个具有公共属性的基类,这也将减少具体类中的代码。

public class BaseModel
{
    public bool IsIPUnique { get; set; }
    public bool IsMACUnique { get; set; }
    .... // other common properties
}
public class ServerJoin : BaseModel
{
    .... //  properties specific to ServerJoin 
}
public class VMJoin : BaseModel
{
    .... //  properties specific to VMJoin
}

并为通用验证码创建一个基本控制器

public class BaseController : Controller
{
    public void Validate(BaseModel model)
    {
        bool ITipunique = repository.ISITIPUnique(vmj.NetworkInfo2.FirstOrDefault().IPADDRESS);
        ....
        if ((model.IsIPUnique == true) && (!ITipunique || !Tipunique))
        {
            ModelState.AddModelError("NetworkInfo2[0].IPAddress", "Error occurred. The Same IP is already assigned.");
        }
        if ((model.IsMACUnique == true) && (!ITmacunique || !Tmacunique))
        {
            ModelState.AddModelError("NetworkInfo2[0].MACAddress", "Error occurred. The Same MAC Address is already assigned.");
        }
        .... // other common validation  
    }
}

然后在特定的控制器上

public class ServerController : BaseController
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ServerJoin sj)
    {
        Validate(sj); // adds the ModelStateErrors
        if (!ModelState.IsValid)
        {
            return View(sj);
        }
        ....
    }
}
public class VMController : BaseController
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(VMJoin vmj)
    {
        Validate(vmj); // adds the ModelStateErrors
        if (!ModelState.IsValid)
        {
            return View(vmj);
        }
        ....
    }
}

如果VMJoinServerJoin有相同的接口,您可以创建将ModelState作为第二个参数的扩展方法。

更新以下是扩展方法的示例

    public static void TestMethod<T>(this T context, ModelStateDictionary modelsState) where T : YourDbBaseClass
    {
        //check context
        //add errors if exist
        modelsState.AddModelError("Error", "Big Error");
    }
    //Usage
    TestMethod<YourDbContext>(ModelState);

在我看来,最干净的方法是使用自定义验证数据注释属性。

您只需创建自定义属性

public class IPUniqueValidator : ValidationAttribute
{
    public string ShouldCheck { get; set; }
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value != null)
        {
            var IP = value.ToString();
            var isValid = false;
            // Using reflection to get the other property value 
            var shouldCheckPropert = validationContext.ObjectInstance.GetType().GetProperty(this.ShouldCheck);
            var shouldCheckPropertValue = (bool)shouldCheckPropert.GetValue(validationContext.ObjectInstance, null);
            if (shouldCheckPropertValue)
            {
                // do validation code...
            }
            if (isValid)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult("Error occurred. The Same IP is already assigned.");
            }
        }
        else
        {
            return new ValidationResult("" + validationContext.DisplayName + " is required");
        }
    }
}

用新属性标记您的模型:

public class VMJoin
{
    [IPUniqueValidator(ShouldCheck = "ShouldCheck")]
    public string IpAddress { get; set; }
    public bool ShouldCheck { get; set; }
}
public class ServerJoin
{
    [IPUniqueValidator(ShouldCheck = "ShouldCheck")]
    public string IpAddress { get; set; }
    public bool ShouldCheck { get; set; }
}

它只留给您添加验证状态的检查。该框架将为您完成所有工作。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ServerJoin sj)
{
    if (ModelState.IsValid)
    {
        // do staff 
    }
}

我会使用自定义ActionFilter。点击此处查看如何从操作过滤器访问ModelState

如何从ActionFilter访问ModelState?

您还可以在自定义操作过滤器中设置依赖项注入,以接收所需的存储库,从而进一步提高可测试性/可维护性。

相关内容

最新更新