i am working on an asp.net mvc-5 web application with entity framework-6.and i have mapped my DB tables using entity framework which generates a .edmx file containing a DBContext class. and currently i have two controller classes one for managing servers and the other for managing vms. When add/edit Server or VMs I want to check if their ip & mac addresses already exists. currently i am going these checks on the action method itself as follow:-
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. ");
}
this approach is working well, but i am facing a problem is that i have to repeat these validations on all the related action methods mainly (add & edit) and inside other controller classes (server, vms, storage device, etc...) so is there a way to manage the shared validation in a better way , which facilitate re-use and maintainability ?
EDIT
ServerJoin is as follow:-
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; }
}
}