简体   繁体   English

REST Web服务-实体框架是否过大?

[英]REST web service - is entity framework an overkill?

I'm familiar with the EF and it looks pretty cool. 我熟悉EF,它看起来很酷。 As far as I can see, it is basically a LINQ to SQL with extra functions (like caching, automatic connection handling and so on). 据我所知,它基本上是具有额外功能(如缓存,自动连接处理等)的LINQ to SQL。 However, in my opinion EF is useful for those applications that directly comminicate with the model data (~persistence). 但是,我认为EF对于直接与模型数据(〜持久性)最小化的应用程序很有用。

In case of writing a RESTful web service, we are reading and writing objects (for example) in JSON format. 如果编写RESTful Web服务,我们将以JSON格式读写对象(例如)。 The application calls the web service with some data and it returns data back. 该应用程序使用一些数据调用Web服务,并返回数据。

That's why I'm actually thinking on not using EF because it looks like an overkill for me. 这就是为什么我实际上在考虑使用EF的原因,因为这对我来说似乎太过强大了。 Since I'm not planning to expose the actual model, I would use DTOs instead (both as input and output of a web service call). 由于我不打算公开实际模型,因此我将改用DTO(都作为Web服务调用的输入和输出)。 This means that I have to do the mapping to the underlying model anyway so the EF would be used as a LINQ to SQL wrapper. 这意味着无论如何我都必须映射到基础模型,以便将EF用作LINQ to SQL包装器。

Is there anything I'm missing? 有什么我想念的吗? Is there any feature that would be useful while writing a RESTful web service? 编写RESTful Web服务时,是否有任何有用的功能? is there any benefit from using EF instead of LINQ to SQL? 使用EF而不是LINQ to SQL有什么好处?

So the logic here is that you aren't exposing your entities past the data layer, so EF is pointless. 因此,这里的逻辑是您不会将实体暴露在数据层之外,因此EF是毫无意义的。

I never expose my EF Entities pass the business layer, just one layer down from the data layer. 我从未公开过我的EF实体通过业务层,而仅是数据层的下一层。 I always project them to ViewModels and Models which are just POCOs. 我总是将它们投影到只是POCO的ViewModels和Models上。 I've seen this in lots of projects. 我已经在很多项目中看到了这一点。

Rarely do I actually use the entity change tracking features. 实际上,我很少使用实体更改跟踪功能。 By the time a GET/POST has occurred it doesn't make sense to requery the entities on the POST just so you can update them via change tracking. 到GET / POST发生时,重新查询POST上的实体就没有意义了,只是您可以通过更改跟踪来更新它们。 Instead a direct update makes more sense and avoids an unnecessary roundtrip to the database. 相反,直接更新更有意义,并且可以避免不必要的数据库往返。

My point being is in what I've seen it most commonly used, the EF models are not exposed past more than one layer in most cases. 我的观点是,在我所看到的最常用的东西上,EF模型在大多数情况下不会暴露超过一层。 This ensures View/UI layers don't accidentally modify EF state or cause lazy loading(which is usually disabled). 这样可以确保View / UI层不会意外修改EF状态或导致延迟加载(通常是禁用的)。

However I still get to leverage the great EF/DB mapping layer and EF LINQ queries, which is by far the greatest features of EF. 但是,我仍然可以利用出色的EF / DB映射层和EF LINQ查询,这是迄今为止EF的最大功能。

Most alternatives such as Dapper are just that, a framework for executing queries. 诸如Dapper之类的大多数替代品就是这样一个执行查询的框架。

So I wouldn't fallback to just doing ADO.NET or an older query technology just because you aren't using all the features of EF. 因此,我不会仅仅因为您没有使用EF的所有功能而退回到仅仅使用ADO.NET或更旧的查询技术。 You should still use a modern query framework such as EF or Dapper, simply because you are still executing queries. 您仍应使用现代查询框架,例如EF或Dapper,仅因为您仍在执行查询。 Just because you aren't exposing the entities doesn't change that. 仅仅因为您不公开实体就不会改变这一点。

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

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