扩展的LINQ to SQL生成的类 [英] Extending LINQ to SQL generated classes

查看:111
本文介绍了扩展的LINQ to SQL生成的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我选择的LINQ to SQL的ORM框架的ASP .NET MVC3项目。一切都很好之前,我面临着需要投入更多的领域确认密码登记表。至于有人提到在一个问题上的SO(不幸的是我无法找到它的时刻)时,最好使用界面生成的LINQ扩展,而无需其他类,用于存储验证特性to SQL类使用验证属性。所以在这里我们去:

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 类由LINQ生成的类到SQL基于数据库实体。

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; }
}

查看是强类型的视图 RegTryViewModel 模式。

View is strongly typed view with RegTryViewModel model.

我只问在这里,以确保我做的一切权利。这让我感到不舒服的事情是,I S $ P $垫验证 IRegTry 界面和 RegTryViewModel 类之间的逻辑。但我不能添加 ConfirmPassword 属性 IRegTry 接口,因为基本SQL到LINQ类不具有它在所有。 在此先感谢你们!

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 partial classes. As long as you set up the partial class in the same namespace with the same name, everything will get set up automatically. 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
}

}

您可以设置部分类为 IValidatableObject 它实现了自己的验证方法。你可以对确认支票==在你的验证方法,密码

You can set up the Partial Class as an IValidatableObject which implements its own Validate method. You can have a check for Confirm==Password in your Validate method.

您可以得到一些这方面的更多信息<一个href="http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=scott-allen&name=mvc3-building-data-ii&mode=live&clip=0&course=aspdotnet-mvc3-intro"相对=nofollow> Pluralsight视频

You can get some more information in this Pluralsight Video

这篇关于扩展的LINQ to SQL生成的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆