简体   繁体   English

将LINQ扩展为SQL生成的类

[英]Extending LINQ to SQL generated classes

I have picked LINQ to SQL as ORM framework for ASP .NET MVC3 project. 我已经选择LINQ to SQL作为ASP .NET MVC3项目的ORM框架。 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上的一个问题中提到的那样(遗憾的是我目前无法找到它),最好使用接口将生成的LINQ扩展为具有验证属性的SQL类,而不是使用另一个类来存储验证属性。 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. RegTry类是基于数据库实体由LINQ to SQL生成的类。

On the View we have confirm password field, which should make sure that two typed password equals to each other. 在View上我们确认了密码字段,这应该确保两个键入的密码彼此相等。

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. View是使用RegTryViewModel模型的强类型视图。

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. 让我感到不舒服的是我在IRegTry接口和RegTryViewModel类之间传播验证逻辑。 But I can't add ConfirmPassword property to IRegTry interface because base SQL to LINQ class doesn't has it at all. 但我不能将ConfirmPassword属性添加到IRegTry接口,因为基本SQL到LINQ类根本没有它。 Thanks in advance guys! 先谢谢你们!

如果您使用的是View Model类,则不需要连接到DAL Model类的验证逻辑,因此您不需要将该验证接口链接到DAL Model类。

I know that you already accepted an answer for this but I think it may be better to set this up using partial classes. 我知道你已经接受了这个答案,但我认为最好使用partial类来设置它。 As long as you set up the partial class in the same namespace with the same name, everything will get set up automatically. 只要在同一namespace使用相同名称设置partial类,就会自动设置所有内容。 Here is an example of how I set this up in one of my projects: 以下是我在我的一个项目中如何设置它的示例:

namespace OperationsMetrics
{
[MetadataType(typeof(ClientStatMD))]
public partial class client_wkly_stat : IValidatableObject
{
    public class ClientStatMD
    {
        [Required(ErrorMessage = "Client selection is required")]
        public virtual int client_id { get; set; }
        [Required(ErrorMessage = "SLAs met is required")]
        public virtual int wkly_sla_met { get; set; }
        [Required(ErrorMessage = "Total SLAs possible is required")]
        public virtual int wkly_sla_req { get; set; }
        [Required(ErrorMessage = "Number of input files is received")]
        public virtual int num_inp_files_rec { get; set; }
        [Required]
        public string client_name { get; set; } 

    }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (wkly_sla_met > wkly_sla_req)
        {
            yield return new ValidationResult("SLAs met cannot be greater that SLAs possible");
        }


    }
    public string client_name { get; set; } //this isn't a part of the actual db object but can still be accessed in the Validate method
}

} }

You can set up the Partial Class as an IValidatableObject which implements its own Validate method. 您可以将Partial Class设置为IValidatableObject ,它实现自己的Validate方法。 You can have a check for Confirm==Password in your Validate method. 您可以在验证方法中检查Confirm==Password

You can get some more information in this Pluralsight Video 您可以在此Pluralsight视频中获得更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM