Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to add methods to the POCOs or to create separate class to update the values of the POCOs?

Is it a good practice to add methods to the POCOs or to create separate class to update the values of the POCOs in case we need that?

For example,

public class ForUser
{
    [Required]
    public int Depratment { get; set; }

    public List<SelectListItem> DepartmentsList { get; set; }

    [Required]
    public int Role { get; set; }

    [Required]
    [StringLength(200, MinimumLength = 3, ErrorMessage = "Length Of The First Name Should  Be More Than Three Letters")]
    public string FirstName { get; set; }

    [StringLength(200, MinimumLength = 3, ErrorMessage = "Length Of The Mid Name Should  Be More Than Three Letters")]
    public string MidName { get; set; }

    [Required]
    [StringLength(200, MinimumLength = 3, ErrorMessage = "Length Of The Last Name Should  Be More Than Three Letters")]
    public string LastName { get; set; }

    [Required]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }

    [StringLength(14, MinimumLength = 10 , ErrorMessage = "Length Of The Mid Name Should  Be More Than Nine Letters and Less than fourteen Letters")]
    [RegularExpression(@"^[+]?[0-9]*", ErrorMessage="Phone Number is not correct")]
    public string PhoneNumber { get; set; }

    [Required]
    public string Password { get; set; }


    public int UserId { get; set; }
    public int Company { get; set; }
    public int Country { get; set; }
    public List<SelectListItem> Roles { get; set; }
}

I use it just to hold the data to update the model entity or to return data to the view. Sometimes I need to update some properties before I send the object to the view, like the list called Roles in the above example, so I am wondering about if I should add the methods to the POCO class or is it better to create a class to update the properties?

like image 255
Mohamad Haidar Avatar asked Aug 16 '15 14:08

Mohamad Haidar


People also ask

Can POCOs have methods?

POCOs are simple and we should be able to instantiate them anywhere without difficulties. POCOs can have methods to implement logic and behaviors like validation. A POCO has no naming conventions for properties or methods.

In which approach database is required before creating POCO classes?

In Code First approach, you avoid working with visual model designer (EDMX) completely. You write your POCO classes first and then create database from these POCO classes.

What is the use of POCO class in C#?

A POCO entity is a class that doesn't depend on any framework-specific base class. It is like any other normal . NET CLR class, which is why it is called "Plain Old CLR Objects". POCO entities are supported in both EF 6 and EF Core.

What is POCO and DTO?

Here's the difference: POCO describes an approach to programming (good old fashioned object oriented programming), where DTO is a pattern that is used to "transfer data" using objects. While you can treat POCOs like DTOs, you run the risk of creating an anemic domain model if you do so.


1 Answers

Here is an answer to this question:

A POCO is not a DTO. POCO stands for Plain Old CLR Object, or Plain Old C# Object. It’s basically the .Net version of a POJO, Plain Old Java Object. A POCO is your Business Object. It has data, validation, and any other business logic that you want to put in there. But there’s one thing a POCO does not have, and that’s what makes it a POCO. POCOs do not have persistence methods. If you have a POCO of type Person, you can’t have a Person.GetPersonById() method, or a Person.Save() method. POCOs contain only data and domain logic, no persistence logic of any kind. The term you’ll hear for this concept is Persistence Ignorance (PI). POCOs are Persistence Ignorant.

I prefer to read the whole article, not just to get the answer to your question, but also understand the difference between the POCO and DTO.

like image 105
Mohamad Haidar Avatar answered Sep 24 '22 04:09

Mohamad Haidar