简体   繁体   中英

Do I have to really create multiple models?

MS stack developer historically.

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. (yes, I have heard of entity framework. even tried it once).

As I embrace Angular and web API 2

I find that Angular wants me to write a model on the front end. 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. Kind of ugly,but it does marry structure and logic.

I find that Web API2 wants me to write a 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.

The question is this:

Is there any way around having 3+ copies of each class spread across the stack? Codesmith is a very capable code generator... it can gen multiple files... but...

If its just a couple data members, and 3 places, I can copy paste edit and get it done.

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.

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.

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. 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. 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.

  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. (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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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