1

I'm about to start a new project and I'd like to use Single Page Application technique. Since I'll be using ASP.NET I think the easiest way will be using Angular, which I'm new with.
Anyway, what scares me the most about Angular (or any other JS/TS tech), is since I don't have much time, I can't afford to rewrite all the models/entities to another language. The code and maintenance cost of this is too high for me.

tl;dr

So my question is, is there a way to have Angular use the original model/entity names so I can use them in the page without the need to rewrite any unnecessary code?
Will the .NET attributes take action?

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • 2
    Not sure if I understand correctly, but perhaps something from here would help (TypeLite)? (Assuming you use TypeScript for your Angular2 dev) http://stackoverflow.com/questions/12957820/how-to-reuse-existing-c-sharp-class-definitions-in-typescript-projects – Ivan Sivak Jan 04 '17 at 10:21
  • Consider using Swagger to generate a Swagger schema file (swagger.json) which you can use Swagger CodeGen or AutoREST to create rest clients including models – Tseng Jan 04 '17 at 15:10

2 Answers2

1

I guess your concern is, that your business object world (entity model) needs to be reflected in your client/angular app as models (javascript objects) ?? The need for them comes also from typing errors you get in angular 2.

Creating and maintaining a transparent model world spanning server and client part is way too much effort for real world applications, although it would be nice.

I decided to use and receive the model as a result from a remote call via AJAX/WebAPI and work with these "models" in my client applications. The result then reflects your business model (entities) you have probably defined already.

this.dataService.getRecords('MT_MyEntity')
        .subscribe((data: any[]) => {
            var response: any = data;                 // Do this to avoid typing errors
            var resprecords: any = response.items;
            // Here you get entities; 
            // Deal with your business objects fetched from remote system; Use it to show in forms, ....
        },
        error => {
            // your error handling
        });

In your application you can use the entity and attributes names you have defined in your server side model (take care of upper/lower case modifications)

For me this is a pragmatic way to deal with that and it works very well.

Karl
  • 3,099
  • 3
  • 22
  • 24
  • 1
    You've defined my issue pretty well. In fact I wish there would be a way to create web apps with C# and XAML only. I despise JS. But your solution sounds fair. – Shimmy Weitzhandler Jan 04 '17 at 13:38
1

For any decent size application the benefits of creating client side model far outweigh the effort required to create and maintain them.

This effect is more pronounce with TypeScript as it allows compile time checking of the contracts. As we move more and more code to client side and use frameworks like Angular, having a clearly defined model helps us understand what is happening. We derive the same benefits that we derive when type checking is available on server.

Having a separate client side model also allows us to adapt such model to client side UI needs (albeit sometime we create viewmodel to satisfy such requirements)

The approach of generating these client side contracts, as highlighted by @Ivan can help us to reduce the overall effort.

Chandermani
  • 42,589
  • 12
  • 85
  • 88