Custom Validation in MVC2 clarification - Following an example of scott gu's?

Here's something I'm using to enforce unique emails in user registration.

Here's something I'm using to enforce unique emails in user registration: AttributeUsage(AttributeTargets. Field | AttributeTargets. Property, AllowMultiple = false, Inherited = true) public class UniqueEmailAttribute : ValidationAttribute { public UniqueEmailAttribute() : base("The value for '{0}' is already taken") { } public override bool IsValid( object value ) { string toCheck = value as string; if( String.

IsNullOrEmpty(toCheck) ) return false; return ( DataRepository. GetMembersByEmail(toCheck).Count() == 0 ); } Inject public IDataRepository DataRepository { get; set; } } The only thing that may not be reasonably self-explanatory here is the Inject attribute decorating the DataRepository property. It's provided by the nInject library as a means of doing dependency injection (inversion of control) so that I can separate database access from the application.

But prior to incorporating DI I just had the attribute create an instance of the concrete DataRepository object and used that (that's not good practice, of course, which is why I went the nInject route).

Following the SoC (Separation of Concern), you shouldn't do this kind of validation on the model but rather on the controller.

This is of absolutely no help, I also find it strange that Scott Guth would be writing tutorials advocating against best practices... And if it should be done in the controller, you still do not answer my question or provide any information on how that would be implemented. – baron Mar 10 at 0:15 What I'm saying is that your model, nor your attributes should not be aware of the underlying persistence. Do not forget that examples on blog articles does not follow best practices for the sake of simplicity.

What I was trying to say is that you should ensure that there are no duplicate on the controller, by querying your persistence layer. – Fabian Vilers Apr 9 at 8:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions