Breeze.js混合DTO和实体 [英] Breeze.js mixing DTOs and entities

查看:78
本文介绍了Breeze.js混合DTO和实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ward的文章 Breeze Server:随心所欲


典型的业务应用程序至少具有200个域模型
类型。 90%以上的时间我通过
导线发送的数据的形状与我的业务模型中实体的形状相同。

...

当客户实体的形状与
a服务器端业务实体的形状不太吻合时,对于这种
的特殊情况,我可以切换到DTO。


对于我们的应用程序,这真是妙不可言,但是为DTO切换 some 实体的最佳方法是什么? / p>

例如,我们的User实体包含不应向客户端公开的敏感属性。它还具有从其他系统提取并返回给客户端的相关数据,理想情况下,这些数据应该只是客户端User对象上的额外属性。用户似乎是切换到DTO的理想人选。



如果用户是一个孤立的实体,这可能会更容易,但是问题在于,基本上每个地方都引用了该用户。 。例如,几乎每个实体都具有 CreatedBy 属性。



是否可以在模型中的任何地方切换用户DTO的用户实体?对于模型中引用用户的所有其他实体,我们仍然需要能够以扩展的用户属性加载它们,以查询这些用户属性,并通过更改这些用户属性来保存它们。



除了建立一个大型DTO模型(与实体模型具有95%的相同性,并在它们之间使用一些映射代码/框架)外,我不知道该怎么做。但是,正如沃德在这篇文章,我不喜欢每种类型的DTO;那是会破坏生产力的过大杀伤力。

解决方案

您的陪伴很好。这样的问题越来越多。我希望很快提供更好的指导。


在短期内(假设您是.NET开发人员),您可能会在DocCode示例中找到一些线索。搜索 ProductDto。 DocCode并未显示您如何保存对其进行更改,因此我将不得不等到下一次。


您的情况实际上可能很容易解决。 / strong>


步骤1:使用自定义的 DbContext


首先编写一个业务模型的 DbContext 的子类。向此子类添加对您的 OnModelCreating 的替代,并教它忽略不应作为一部分的 User 属性。

 受保护的覆盖无效OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< User>()。忽略(u => u.whatever);
...
base.OnModelCreating(modelBuilder);
}

现在引用此派生的 DbContext 与客户沟通。


请注意,这涉及非常少的代码,并且易于维护。它不会干扰您使用基本 DbContext 的权限,该基本权限保留对 User 所有属​​性的完全访问权限。


步骤#2:配置JSON.NET以从序列化中排除这些属性


按照詹姆斯·牛顿·金的指南。如果您不想用<$ c $装饰/污染 User 类,请特别查看 IContractResolver c> [JsonIgnore] 属性。 James是JSON.NET的作者。


In Ward's article "The Breeze Server: Have It Your Way":

The typical business application has a minimum of 200 domain model types. 90+% of the time the shape of the data I'm sending over the wire is the same as the shape of the entity in my business model.
...
When the shape of a client entity doesn't align well with the shape of a server-side business entity, I may switch to a DTO for that particular case.

This hits the nail right on the head for our application, but what's the best way to switch just some entities for DTOs?

For example, our User entity contains sensitive properties that should not be exposed to the client. It also has related data that gets pulled from other systems and returned to the client, which ideally should just be extra properties on the client-side User objects. User seems an ideal candidate for switching to a DTO.

If User were an isolated entity this might be easier, but the problem is that User is referenced basically everywhere in the model. Almost every entity has a CreatedBy property, for example.

Is there a way to switch the User entity for a User DTO everywhere in the model? For all the other entities in the model that reference users, we still need to be able to load them with their user properties expanded, to query them on those user properties, and to save them with changes to those user properties.

I'm not sure how to do this other than building a big DTO model that is 95% identical to the entity model, and have some mapping code/framework to go between them. But, as Ward says in this post, "I don't like DTOs for every type; that's overkill that can ruin productivity."

解决方案

You are in good company. Questions like this are stacking up. I hope to provide better guidance "soon".

In the short run (assuming you're a .NET developer), you may find some clues in the DocCode sample. Search for "ProductDto". DocCode doesn't show how you'd save changes to it so I'll have to hold that off until another time.

Your scenario may actually be easy to address.

Step #1: Use a custom DbContext

Start by writing a sub-class of your business model's DbContext. Add to this sub-class an override to your OnModelCreating and teach it to ignore the User properties that should not be part of the model.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<User>().Ignore(u => u.whatever);
   ...
   base.OnModelCreating(modelBuilder);
}

Now refer to THIS derived DbContext when communicating with clients.

Notice that this involves a very small amount of code and is easy to maintain. It doesn't interfere with your use of the base DbContext which retains full access to all properties of User.

Step #2: Configure JSON.NET to exclude these properties from serialization

Follow James Newton King's guidance. Look in particular at IContractResolver if you don't want to decorate/pollute your User class with the [JsonIgnore] attribute. James is the author of JSON.NET.

这篇关于Breeze.js混合DTO和实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆