gpt4 book ai didi

c# - 向 POCO 添加方法或创建单独的类来更新 POCO 的值是一种好的做法吗?

转载 作者:可可西里 更新时间:2023-11-01 02:59:16 28 4
gpt4 key购买 nike

向 POCO 添加方法或创建单独的类以在我们需要时更新 POCO 的值是一种好的做法吗?

例如,

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

我只是用它来保存数据以更新 model entity 或将数据返回到 View 。有时我需要在将 object 发送到 View 之前更新一些属性,例如上面示例中名为 Roles 的列表,所以我想知道是否应该添加POCO 类的方法还是创建一个类来更新属性更好?

最佳答案

Here是这个问题的答案:

A POCO is not a DTO. POCO stands for Plain Old CLR Object, or PlainOld C# Object. It’s basically the .Net version of a POJO, Plain OldJava Object. A POCO is your Business Object. It has data,validation, and any other business logic that you want to put inthere. But there’s one thing a POCO does not have, and that’s whatmakes it a POCO. POCOs do not have persistence methods. If you havea 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 thisconcept is Persistence Ignorance (PI). POCOs are PersistenceIgnorant.

我更喜欢通读整篇文章,不仅是为了得到您问题的答案,也是为了了解POCODTO 之间的区别。

关于c# - 向 POCO 添加方法或创建单独的类来更新 POCO 的值是一种好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32036204/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com