我如何知道是否应该使用自我跟踪实体或 DTO/POCO?
关于我们的设计,我可以问自己哪些问题,以确定我们是否应该在应用程序中使用 DTO 或自我跟踪实体?
以下是我知道需要考虑的一些事项:
- 我们有一个标准的 n 层应用程序,带有 WPF/MVVM 客户端、WCF 服务器和 MS SQL 数据库。
- 用户可以定义自己的接口,因此 WCF 服务所需的数据会根据用户为自己定义的接口而变化。
- 模型在客户端和服务器端都用于验证。我们不会直接绑定到 DTO 或 STE
- 某些模型包含在需要时从 WCF 服务延迟加载的属性
- 数据库层垃圾邮件多个服务器/数据库
- 服务器端的权限检查会影响数据的返回方式。例如,根据用户的角色,某些数据被部分或完全屏蔽。
- 我们的资源是有限的(时间、人力等)
那么,我如何确定什么适合我们呢?我以前从未使用过 EF,所以我真的不知道 STE 是否适合我们。
我见过有人建议从 STE 开始,并且仅在出现问题时才实施 DTO,但是我们目前已经部署了 DTO,并正在尝试确定使用 STE 是否会让生活变得更轻松。我们在这个过程中处于足够早的阶段,切换不会花费太长时间,但我不想切换到 STE 只是为了发现它对我们不起作用,并且必须将所有内容切换回来。
What are some questions I can ask myself about our design to identify if we should use DTOs or Self-Tracking Entities in our application?
Here's some things I know of to take into consideration:
- We have a standard n-tier application with a WPF/MVVM client, WCF server, and MS SQL Database.
- Users can define their own interface, so the data needed from the WCF service changes based on what interface the user has defined for themselves
- Models are used on both the client-side and server-side for validation. We would not be binding directly to the DTO or STE
- Some Models contain properties that get lazy-loaded from the WCF service if needed
- The Database layer spams multiple servers/databases
- There are permission checks on the server-side which affect how the data is returned. For example, some data is either partially or fully masked based on the user's role
- Our resources are limited (time, manpower, etc)
So, how can I determine what is right for us? I have never used EF before so I really don't know if STEs are right for us or not.
I've seen people suggest starting with STEs and only implement DTOs if they it becomes a problem, however we currently have DTOs in place and are trying to decide if using STEs would make life easier. We're early enough in the process that switching would not take too long, but I don't want to switch to STEs only to find out it doesn't work for us and have to switch everything back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解你的架构,我认为这对 STE 因为:
主要优点(也是唯一的优点)是它们的跟踪能力,但只有当双方都使用 STE 时,跟踪能力才起作用:
简而言之:客户端或服务器端没有额外的模型。要充分利用 STE,它们必须是:
任何其他情况仅意味着您没有利用自我跟踪功能,并且您不需要它们。
那你的其他要求呢?
这应该是可能的,但要确保每个“延迟加载”部分都是单独的结构 - 不要在客户端构建复杂的模型。我已经看到过这样的问题:人们必须将整个实体图发回以进行更新,这不是您总是想要的。因此,我认为您不应该将加载的部件连接到单个实体图中。
我不确定您实际上希望如何实现这一点。 STE 不使用投影,因此您必须直接将实体中的字段设为空。请注意,当实体不处于跟踪状态时,您必须执行此操作,否则您的屏蔽将保存到数据库中。
这不是 STE 的问题。服务器必须使用正确的 EF 上下文来加载和保存数据。
STE 是变更集模式的实现。如果您想使用它们,您应该遵循它们的规则以充分利用该模式。如果使用得当,它们可以节省一些时间,但这种加速会牺牲一些架构决策。与任何其他技术一样,它们并不完美,有时您会发现它们很难使用(只需遵循 self-tracking-entities< /a> 标签查看问题)。它们也有一些严重的缺点,但在 .NET WPF 客户端中你不会见到他们。
If I understand your architecture, I think it is not good for STEs because:
The main advantage (and the only advantage) or STEs is their tracking ability but the tracking ability works only if STE is used on both sides:
In short: There are no additional models on client or server side. To fully use STEs they must be:
Any other scenario simply means that you don't take advantage of self tracking ability and you don't need them.
What about your other requirements?
This should be probably possible but make sure that each "lazy loaded" part is separate structure - do not build complex model on the client side. I've already seen questions where people had to send whole entity graph back for updates which is not what you always want. Because of that I think you should not connect loaded parts into single entity graph.
I'm not sure how do you want actually achieve this. STEs don't use projections so you must null fields directly in entities. Be aware that you must do this when entity is not in tracking state or your masking will be saved to the database.
It is something that is not problem of STEs. The server must use a correct EF context to load and save data.
STEs are implementation of change set pattern. If you want to use them you should follow their rules to take full advantage of the pattern. They can save some time if used correctly but this speed up comes with sacrifice of some architectural decisions. As any other technology they are not perfect and sometimes you can find them hard to use (just follow self-tracking-entities tag to see questions). They also have some serious disadvantages but in .NET WPF client you will not meet them.
您可以为给定场景选择 STE,
STE 的优点 -
STE 的缺点 -
POCO 的优点 -
POCO 的缺点 -
You can opt STE for given scenario,
Pros for STE -
Cons for STE -
Pros for POCO -
Cons for POCO -
POCO 是动态代理,在线上运行效果不佳 请参阅此 MSDN 文章了解不过解决方法。所以它们可以被制作,但在我看来,你最好选择 STE,因为我相信它们与 WPF/MVVM 开发非常吻合。
POCO are dynamic proxied and don't play nice on the wire see this MSDN article for the workaround though. So they can be made to but IMO you're better off going STE as I believe they align nicely with WPF/MVVM development.