I have picked LINQ to SQL as ORM framework for ASP .NET MVC3 project. Everything was good before I was faced with need to put additional field ‘Confirm Password’ to registration form. As it was mentioned in one question on SO (unfortunately I can’t find it at the moment), it’s better to use interface to extend generated LINQ to SQL classes with validation attributes, instead of having another class for storing validation attributes. So here we go:
public interface IRegTry
{
[Required]
[Email]
string EMail { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Should not exceed 100 symbols")]
string FirstName { get; set; }
[Required]
string Password { get; set; }
}
[MetadataType(typeof(IRegTry))]
public partial class RegTry : IRegTry { }
RegTry class is generated class by LINQ to SQL based on database entity.
On the View we have confirm password field, which should make sure that two typed password equals to each other.
So here we adding it:
public class RegTryViewModel : RegTry
{
[Required]
[EqualTo("Password", ErrorMessage = "You should type two identical passwords to continue")]
public string ConfirmPassword { get; set; }
}
View is strongly typed view with RegTryViewModel model.
I just ask here to make sure I’m doing everything right. The thing that makes me feel uncomfortable is that I spread validation logic between IRegTry interface and the RegTryViewModel class. But I can’t add ConfirmPassword property to IRegTry interface because base SQL to LINQ class doesn’t has it at all.
Thanks in advance guys!
I know that you already accepted an answer for this but I think it may be better to set this up using
partialclasses. As long as you set up thepartialclass in the samenamespacewith the same name, everything will get set up automatically. Here is an example of how I set this up in one of my projects:}
You can set up the Partial Class as an
IValidatableObjectwhich implements its ownValidatemethod. You can have a check forConfirm==Passwordin your Validate method.You can get some more information in this Pluralsight Video