简体   繁体   English

使用外部Web服务时的ASP.NET MVC模型

[英]ASP.NET MVC models when using external web services

I'm getting started on a new MVC project where there are some peculiar rules and a little bit of strangeness, and it has me puzzled. 我从一个新的MVC项目开始,那里有一些特殊的规则和一些陌生的地方,这让我感到困惑。 Specifically, I have access to a database containing all of my data, but it has to be handled entirely through an external web service. 具体来说,我可以访问包含我所有数据的数据库,但是必须完全通过外部Web服务进行处理。 Don't ask me why, I don't understand the reasons. 不要问我为什么,我不明白原因。 That's just how it is. 就是这样。

So the CRUD will be handled via this API. 因此,将通过此API处理CRUD。 I'm planning on creating a service layer that will wrap up all the calls, but I'm having trouble wrapping my head around the model... To create my model-based domain objects (customers, orders, so on..) should I: 我正计划创建一个服务层来包装所有调用,但是我无法将自己的头绕在模型上。要创建基于模型的域对象(客户,订单等)。我是不是该:

  1. Create them all manually 手动创建它们
  2. Create a dummy database and point an ORM at it 创建一个虚拟数据库并指向一个ORM
  3. Point an ORM at the existing database but ignore the ORM's persistence in lieu of the API. 将ORM指向现有数据库,但忽略ORM代替API的持久性。

I feel like I've got all the information I need to build this out, but I'm getting caught up with the API. 我觉得我已经掌握了构建该应用程序所需的所有信息,但是我对API有所了解。 Any pointers or advice would be greatly appreciated. 任何指示或建议,将不胜感激。

Depending on the scale of what you're doing option 3 is dangerous as you're assuming the database model is the same as that exposed by the external service. 根据您正在执行的规模,选项3很危险,因为您假设数据库模型与外部服务公开的模型相同。 Options 1 and 2 aren't IMHO much different from each other - in either case you'll have to decide what your objects, properties and behaviours are going to be - it just boils down to whether you're more comfortable doing it in classes or database tables. 选项1和2彼此之间没有太多不同-在两种情况下,您都必须决定对象,属性和行为的含义-归结为您是否更愿意在课堂上这样做或数据库表。

The key thing is to make sure that the external service calls are hidden behind some sort of wrapper. 关键是要确保外部服务调用隐藏在某种包装器后面。 Personally I'd then put a repository on top of that to handle querying the external service wrapper and return domain objects. 然后我个人将一个存储库放在其上,以处理查询外部服务包装程序并返回域对象。

In general, ORMs are not known for their ability to generate clean domain model classes. 通常,ORM因其生成干净域模型类的能力而闻名。 ORMs are known for creating data layers, which you don't appear to need in this case. ORM以创建数据层而闻名,在这种情况下,您似乎不需要。

You could probably use a code generation tool like T4 to code generate a first pass at your domain model classes, based on either the web service or the database, if that would save you time. 您可能会使用T4之类的代码生成工具来基于Web服务或数据库在域模型类上生成代码,以节省时间。 Otherwise, you probably would just manually create the domain objects. 否则,您可能只是手动创建域对象。 Even if you code generate a first pass at your domain objects, it's unlikely there is a clean 1-1 mapping to your domain objects from either the database or web service, so you will likely need to spend significant time manually editing your code generated domain classes anyway. 即使您的代码在域对象上生成了第一遍,也不太可能存在从数据库或Web服务到您的域对象的清晰1-1映射,因此您可能需要花费大量时间手动编辑代码生成的域无论如何。

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

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