简体   繁体   English

.NET C#中MVC的最佳实践

[英]Best practice for MVC in .net C#

I am novice to MVC framework. 我是MVC框架的新手。 I am using entityframework DBcontext to perform Database related operation. 我正在使用entityframework DBcontext执行数据库相关的操作。 I am doing this way Entityframework-->dbcontext-->Model-->Controller--> View . 我这样做是Entityframework-> dbcontext-> Model-> Controller-> View I am directly binding Model to view. 我直接绑定模型来查看。 eg there is Table name UserProfile and i have created Model with same name and colum as property of Model. 例如,有表名UserProfile ,我创建了具有与Model属性相同名称和列的Model。 I am puling collection of record from Userprofile and bind this collection directly to View. 我正在从Userprofile中收集记录集合,并将该集合直接绑定到View。 Here is Model 这是模特

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [DisplayName("ID")]

    public long UserId { get; set; }
    private string _UserName;
    public string UserName
    {
        get { return _UserName; }
        set { _UserName = value; }

    }
    public string Thalassemic
    {
        get;
        set;
    }

    [Display(Name = "Your First Name")]
    [Required(ErrorMessage = "First Name is Required.")]
    [StringLength(30, ErrorMessage = "First Name must be {2}-{1} to long", MinimumLength = 2)]
    [RegularExpression("^[a-zA-z ]+$", ErrorMessage = "First name must contain only characters")]
    public string FirstName { get; set; }

    [Display(Name = "Your Last Name")]
    [Required(ErrorMessage = "Last Name is Required.")]
    [RegularExpression("^[a-zA-z ]+$", ErrorMessage = "Only characters are allowed.")]
    [StringLength(20, ErrorMessage = "First Name must be {2}-{1} to long", MinimumLength = 2)]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Email Address is Required.")]
    [StringLength(250, ErrorMessage = "{0} must be {2}-{1} to long", MinimumLength = 4)]
    [Display(Name = "Email Address")]
    [DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid email-address.")]
    [RegularExpression("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", ErrorMessage = "Please enter a valid email-address.")]
    //todo: re-think about updating email address as we are user email as login id and this can't change
    public string Email { get; set; }}

Should I use this DTO object to bind data with view 我是否应该使用此DTO对象将数据与视图绑定

public class UserProfileDTO
{
    public long UserId { get; set; }
    private string _UserName;
    public string UserName
    {
        get { return _UserName; }
        set { _UserName = value; }
    }
    public string Thalassemic
    {   get;
        set;
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

based on above I want to know that is it right method? 基于以上我想知道那是正确的方法吗? if not then What is best practice? 如果不是,那么最佳实践是什么?

Using database POCO classes as a model is generally discouraged in MVC, in place of view models that contain only the data that the view needs. 在MVC中,通常不建议将数据库POCO类用作模型,以代替仅包含视图所需数据的视图模型。 Imagine you have a User record with 30+ fields, but you only want to display the Username , you could simply create a UsernameViewModel : 假设您有一个包含30多个字段的User记录,但是您只想显示Username ,只需创建一个UsernameViewModel

public class UsernameViewModel
{
    public string Username { get; set; }
}

It's much cleaner because you're only sending to the client the fields that you need. 因为您只将所需的字段发送给客户端,所以它更清洁。

As @mat said, it's not recommended to use POCO classes directly in the views. 正如@mat所说,不建议在视图中直接使用POCO类。 It is advised against it because the user should never be able to modify the POCO values, also, what happens when you want to show more than one POCO to the user? 建议不要这样做,因为用户永远不能修改POCO值,而且,当您想向用户显示多个POCO时会发生什么? (I hope your answer is not "Modify the POCO to meet the users request"). (希望您的答案不是“修改POCO以满足用户要求”)。

Also, if your application is n-tier, it's strongly advised to use some DTOs to transfer the data between the layers. 另外,如果您的应用程序是n层,则强烈建议使用一些DTO在各层之间传输数据。

As a suggestion and benefits of using a DTO class you can check the msdn link here or, as a demo part, there is a good tutorial on what to use depending on your requirements on codeproject here . 作为一个建议,并使用DTO类,你可以查看MSDN链接的好处在这里 ,或作为演示的一部分,对因在CodeProject你的要求用什么一个很好的教程在这里

Read more on stackoverflow about your question here 这里阅读更多关于stackoverflow的问题

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

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