简体   繁体   English

我真的必须创建多个模型吗?

[英]Do I have to really create multiple models?

MS stack developer historically.历史上的 MS 堆栈开发人员。

I have committed to retooling to the following stack我已承诺对以下堆栈进行重组

 angular -> ms web.api2 -> C# business objects -> sql server

Being old, I develop the database from requirements and use Codesmith to generate the business logic layer.年纪大了,我从需求开发数据库并使用 Codesmith 生成业务逻辑层。 (yes, I have heard of entity framework. even tried it once). (是的,我听说过实体框架。甚至尝试过一次)。

As I embrace Angular and web API 2当我拥抱 Angular 和 web API 2

I find that Angular wants me to write a model on the front end.我发现Angular要我在前端写一个model。 This seems to be just a data structure, I cant even add helper methods to it这似乎只是一个数据结构,我什至无法为其添加辅助方法

So I also often write a class with helper methods that takes an instance of the model.因此,我也经常使用辅助方法编写 class,该方法采用 model 的实例。 Kind of ugly,but it does marry structure and logic.有点丑,但它确实结合了结构和逻辑。

I find that Web API2 wants me to write a model.我发现 Web API2 要我写一个 model。 This again seems to be just a data structure.这似乎又只是一个数据结构。 I am exploring the dynamic data type, but really this doesn't buy me much.我正在探索动态数据类型,但实际上这并没有给我太多帮助。 Instead of writing a class, I'm writing a mapping function.我没有写 class,而是写了一个映射 function。

The question is this:问题是这样的:

Is there any way around having 3+ copies of each class spread across the stack?有没有办法让每个 class 的 3 个以上副本分布在堆栈中? Codesmith is a very capable code generator... it can gen multiple files... but... Codesmith 是一个非常强大的代码生成器......它可以生成多个文件......但是......

If its just a couple data members, and 3 places, I can copy paste edit and get it done.如果它只有几个数据成员和 3 个位置,我可以复制粘贴编辑并完成它。

Just seems to me that now committing to keeping a data structure in synch in 3 different environments is setting oneself up for a lot of work.在我看来,现在致力于在 3 个不同的环境中保持数据结构同步正在为大量工作做好准备。

I have spent the last 15 years trying to shove as much code as I can into a framework of inheritable classes so I can keep things DRY.在过去的 15 年中,我一直在尝试将尽可能多的代码放入可继承类的框架中,这样我就可以保持 DRY。

Am I missing something?我错过了什么吗? Are there any patterns that can be suggested?有什么可以推荐的模式吗?

[I know this isn't a question tailored for SO, but it is where all the smart people shop. [我知道这不是为 SO 量身定做的问题,但它是所有聪明人购物的地方。 Downvote me if you feel honor bound to do so.]如果您觉得有必要这样做,请投票给我。]

Not entirely familiar with how CodeSmith generates it's classes, but if they are just plain-old-CLR-objects that serialize nicely, you can have WebApi return them directly to your Angular application.不完全熟悉 CodeSmith 如何生成它的类,但如果它们只是可以很好地序列化的普通旧 CLR 对象,您可以让 WebApi 将它们直接返回到您的 Angular 应用程序。 There are purists that will frown upon this, but depending on the application, there may be a justification.有些纯粹主义者会对此不以为然,但根据应用程序,可能会有理由。

Then, in the world of Angular, you have a few options, again, depending on your requirements/justification, and your application - again, purists will definitely frown upon some of the options.然后,在 Angular 的世界中,您有几个选项,再次取决于您的要求/理由和您的应用程序 - 再一次,纯粹主义者肯定会不赞成某些选项。

  1. create classes that match what's coming down from the server (more correct method)创建与来自服务器的内容相匹配的类(更正确的方法)
  2. Treat everything as "any", lose type safety, and just access properties as you need them ie don't create the model.将所有内容视为“任何”,失去类型安全性,并根据需要访问属性,即不要创建 model。 (obviously less correct method) (显然不太正确的方法)
  3. find a code generation tool that will explore API end points to determine what they return, and generate your typescript classes for you.找到一个代码生成工具,它将探索 API 端点以确定它们返回的内容,并为您生成 typescript 类。

Personally, using Entity Framework, I (manually) create my POCO's for database interraction, have a "view"/DTO class that WebAPI would then send back to the client, and a definition of the object in Typescript, but I am a control freak, and don't like generated code.就个人而言,使用实体框架,我(手动)创建用于数据库交互的 POCO,有一个“视图”/DTO class,然后 WebAPI 将发送回客户端,以及 Z9327DAFFB33041504F28Z 中 object 的定义,3 但我是 amcc99 一个控制怪胎,并且不喜欢生成的代码。

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

相关问题 EF,我真的必须先在数据库中创建一个实体,然后才能关联它吗? - EF, Do i really have to create an entity in the db before being able to relate it? ASP.NET MVC:是否真的必须为每个动作创建一个视图? - ASP.NET MVC: Do I really have to create one view per action? 在一个视图中使用多个模型时,是否必须实现ViewModel? - Do I have to implement a ViewModel when using multiple Models in one View? 在XElement中设置值-我真的必须从根开始吗? - Set value in XElement - do I really have to start from the root? 我是否真的必须在自定义控件的OnMouseDown中调用Focus? - Do I really have to call Focus in OnMouseDown of my custom Control? REST服务-我是否真的必须阅读OperationContext才能获得消息? - REST Services - Do I really have to read the OperationContext to get the message? 我真的需要在这里创建新交易吗? - Do I really need to create a new transaction here? 我真的需要为此方法创建并返回集合吗? - Do I really need to create and return the collection for this method? 我真的必须以不同的方式编写我的代码,以便可以测试它吗? - Do I really have to do write my code differently so just so I can test it? 为什么我必须在视图中引用我的模型? - Why do I have to reference my models in a view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM