简体   繁体   English

Web API,AutoMapper和DTO /模型

[英]Web API, AutoMapper and DTO/Model

I keep my model in a Model assembly and the DTO classes in a DTO assembly. 我将模型保留在模型装配中,将DTO类保留在DTO装配中。 Both are referenced in the WebAPI project and mappings are created there. 两者均在WebAPI项目中引用,并且在此处创建映射。 The WPF client just references the DTO assembly. WPF客户端仅引用DTO程序集。

My entities have an 'Id' identity property and I set the access modifier to internal so that the id is only set in the model assembly (backed by EF6). 我的实体具有“ Id”身份属性,我将访问修饰符设置为internal,以便仅在模型装配体中设置ID(由EF6支持)。 Even the Id in the DTO is set to internal. 甚至DTO中的ID都设置为内部。

The problem is that when the DTO reaches the client, the Id is 0. Do I need to set the accessibility to public? 问题在于,当DTO到达客户端时,Id为0。我需要设置对公众的可访问性吗? What if the client changes the id and saves the entity? 如果客户端更改ID并保存实体该怎么办? This will cause problems. 这会引起问题。

Is there another alternative? 还有其他选择吗?

The Id field shouldn't be read only, both in the server and in the client. 在服务器和客户端中, Id字段都不应该是只读的。

They have to be set both in the server by EF, and in the client by the serializer, so you do not want to make it inaccessible. 必须在EF的服务器中以及在序列化器的客户端中都设置它们,因此您不想使它不可访问。 If you don't want the user to mess with it, I'd say just mark it with the [Obsolete] attribute or some other means of warning user, but you should keep it visible and editable. 如果您不希望用户对此感到困惑,我想说的只是使用[过时]属性或其他警告用户的方式标记它,但是您应该保持可见和可编辑的状态。

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

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