2

I am novice to MVC framework. I am using entityframework DBcontext to perform Database related operation. I am doing this way Entityframework-->dbcontext-->Model-->Controller--> View. I am directly binding Model to view. e.g. there is Table name UserProfile and i have created Model with same name and colum as property of Model. I am puling collection of record from Userprofile and bind this collection directly to 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

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?

ekad
  • 14,436
  • 26
  • 44
  • 46
  • 1
    Generally best to use a view model for the view - [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jun 23 '15 at 07:42

2 Answers2

3

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. Imagine you have a User record with 30+ fields, but you only want to display the Username, you could simply create a 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.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • @matytommo, thanks for you answer but this is not my question. I have update question for better clarification. I just want to know about entire EF Model binding to view is good idea or not? – Tarsem Singh Jun 23 '15 at 08:01
  • 1
    @TarsemSingh That **is** the answer to that question :). You shouldn't use EF classes in the view, it is advised against. – Mathew Thompson Jun 23 '15 at 08:02
  • it mean I should use DTO class for binding to view. right ? if this is correct then can you please tell me what is cons for direct bind of model to view. – Tarsem Singh Jun 23 '15 at 08:07
  • 1
    @TarsemSingh No you shouldn't use a DTO, that's what I'm saying. This separates your database code from your UI layer, which you should be doing. It also means that there isn't any database calls in the view via traversing the object graph (think `Parent.Child.GrandChild`, that will generate database queries). It's more about separation of concerns. – Mathew Thompson Jun 23 '15 at 08:18
0

As @mat said, it's not recommended to use POCO classes directly in the views. 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? (I hope your answer is not "Modify the POCO to meet the users request").

Also, if your application is n-tier, it's strongly advised to use some DTOs to transfer the data between the layers.

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.

Read more on stackoverflow about your question here

Community
  • 1
  • 1
Zippy
  • 1,804
  • 5
  • 27
  • 36