简体   繁体   English

域数据模型中的非持久属性

[英]non-persistent properties in domain data model

I hear that for a small project DTO's are not recommended for example here and here .我听说对于一个小项目 DTO 是不推荐的,例如这里这里 I wonder if it is OK for a considerably small project (team-wise) to merge non-persistent properties in the domain models?我想知道对于一个相当小的项目(团队明智的)合并域模型中的非持久属性是否可以? eg:例如:

namespace Domain.Entities
{
    public class Candidate : BaseEntity
    {
        public Candidate()
        {
            // some construction codes
        }

        // region persistent properties

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool? IsMale { get; set; }
        public DateTime BirthDate { get; set; }
        // other properties ...


        // region non-persistent properties
        public string FullName => $"{FirstName} {LastName}";
    }
}

Is this just keeping simple or am loosing anything valuable this way?这只是保持简单还是以这种方式失去任何有价值的东西?

I'm not advocating a particular approach, just sharing information...我不是在提倡一种特定的方法,只是分享信息......

I wouldn't put your computation of FullName in a DTO.我不会将您对 FullName 的计算放在 DTO 中。 A DTO is just a simple object, really more of a struct, and shouldn't have any logic in it. DTO 只是一个简单的对象,实际上更像是一个结构体,其中不应包含任何逻辑。 The purpose of a DTO is to move data from one layer/tier to another and create a layer of indirection that allows your domain model to evolve independent of your clients. DTO 的目的是将数据从一层/层移动到另一层,并创建一个间接层,允许您的域模型独立于您的客户而发展。 FullName on your Entity as a non-persistent property makes more sense here than in the DTO.实体上的 FullName 作为非持久属性在这里比在 DTO 中更有意义。 If you want to go full enterprise, it would be in a transformer/adapter.如果你想成为一个完整的企业,它会在变压器/适配器中。

If your project is really small, and is likely never going to grow, then abandoning the DTO can be acceptable.如果您的项目真的很小,并且可能永远不会增长,那么放弃 DTO 是可以接受的。 Just keep in mind, that if your project grows you may have to do some refactoring, and there are some other things to consider...请记住,如果您的项目增长,您可能需要进行一些重构,还有一些其他的事情需要考虑......

Another benefit of the DTO is keeping some data where it needs to stay. DTO 的另一个好处是将一些数据保留在需要保留的地方。 For example, if you have sensitive data in your entity object and you don't put something in place to prevent it from being returned in a web request, you just leaked some information off your app server layer (think the password field in your user entity).例如,如果您的实体对象中有敏感数据并且您没有放置一些东西来防止它在 Web 请求中返回,那么您只是从应用程序服务器层泄露了一些信息(想想您用户中的密码字段实体)。 A DTO requires you to think about what is being sent to/from the client and makes including data an explicitly intentional act vs an unintentional act. DTO 要求您考虑向/从客户端发送的内容,并使包含数据成为明确的有意行为与无意行为。 DTOs also make it easier to document what is really required for a client request. DTO 还可以更轻松地记录客户端请求真正需要的内容。

That being said, each DTO is now code you have to write and maintain, which is the main reason to avoid them, and a model change can have a noticeable ripple effect through the system.话虽如此,现在每个 DTO 都是您必须编写和维护的代码,这是避免它们的主要原因,并且模型更改可能会对系统产生明显的连锁反应。

It comes down to deciding how you want to handle potential data leakage, how you want to manage your clients (if you can), and how complex your model may get.这归结为决定您希望如何处理潜在的数据泄漏、您希望如何管理您的客户(如果可以的话)以及您的模型可能变得多么复杂。

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

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