I'm making a Filesharing system for a school project using a n-tier architecture. I want to validate user input in my business logic and be able to notify the user what input has errors and which error it is.
I don't really know how to approach this. My business logic has a method to insert a new upload like this:
public bool NewFile(File entity)
{
return repo.Insert(entity);
}
This is my model of the File object:
public class File : Upload
{
public int UploadId { get; set; }
public string FileType { get; set; }
public string Category { get; set; }
public int Upvote { get; set; }
public int Downvote { get; set; }
}
The upload model contains properties like title, description etc.
How will I be able to notify the user about input errors with a method that returns a Boolean? Do I make a separate validation class and make the method return an instance of the validation class? Or do I throw custom exceptions with the right error message and catch it in my presentation layer?
Would appreciate it if anyone could point me in the right direction
I don't know what framework your are using but the good method to validate user input would be to perform validations before trying to insert in database.
There is this solution that is quite common in asp.net mvc, you might be able to use it in your case.
If that is note convenient I would suggest to use a try/catch around your insert but you would have to do the logic yourself to notify the user what input threw the error and how it can be fixed (maybe there would be a size limit for example?).
EDIT: try/catch is enough for a school project but in production you would prefer anticipating any possible errors before the insert. Like so :
public bool NewFile(File entity)
{
if( /* check a validation rule */)return false;
else if( /* check another rule */ )return false;
return repo.Insert(entity);
}
of course if you want to send back information to the user maybe you would prefer returning a string message explaining what validation rule did not pass.
regards