I have an application which have two fields IP & Mac, with two checkboxes, to indicate if the user wants to check if the IP AND/OR MAC addresses he entered are unique or not . I use to do the checking inside my post action methods using service method.
But this have required to repeat the same check on my Post Create and POST Edit action emthods.so I found another way to move my validation logic from my action methods and put them inside my model Ivalidatable object as follow:-
public class ConsoleServerJoin : IValidatableObject
{
Repository repository = new Repository();
public ITSYSConsoleServer ConsoleServer { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (ConsoleServer != null)
{
bool IT360ipunique = repository.ISIT360IPUnique(NetworkInfo.IPAddress);
bool IT360macunique = repository.ISIT360MACUnique(NetworkInfo.MACAddress);
bool ITSYSipunique = repository.ISITSYSIPUnique(NetworkInfo.IPAddress);
bool ITSYSmacunique = repository.ISITSYSMACUnique(NetworkInfo.MACAddress);
if ((IsIPUnique == true) && (!IT360ipunique || !ITSYSipunique))
{
yield return new ValidationResult("Error occurred. The Same IP is already assigned.", new[] { "NetworkInfo.IPAddress" });
}
if ((IsMACUnique == true) && (!IT360macunique || !ITSYSmacunique))
{
yield return new ValidationResult("Error occurred. The Same MAC Address is already assigned.", new[] { "NetworkInfo.MACAddress" });
}
}
}
}
}
So my question is whether calling my model repository from my Ivalidatble object a right approach to follow, or i should only be calling my repository class from inside the controller classes ?
Thanks