REST WCF - best approach to expose entities or convert custom classes to Entity classes vice versa

StackOverflow https://stackoverflow.com/questions/17323156

  •  01-06-2022
  •  | 
  •  

質問

I have taken a step back on two of the approaches in my project (WCF REST Service).

  1. Started with WCFDataServices since it support full OData service stack, but due to more validation requirements on CRUD operations, switched to 'WCF Service' with EF.
  2. And now thinking to step back to use Self-tracking entities to exposing entities to client, as many articles says STE is no more supported by Microsoft and preferred to use OData.(but again WCFDataService not suitable for me).

Please suggest what is the best design here to expose my entities over client. Alternatively, I may have to write custom classes (Data Contracts) of Entity Model. But, this increases code (for conversion of objects between Custom and Entity) and decreases maintainability.

Please suggest is there any best approach to expose my entities. Your suggestions are valuable and most appreciated.

役に立ちましたか?

解決

Fowlers first law of distributed object design states, "don't distribute your objects". This just means give them a copy and not the actual entity itself. If you were to create mirror copies of your entities in your data contract namespace, you retain much more flexibility, should your database schema need changing. If your data contract is initially identical to your entity, a tool such as AutoMapper will eliminate all the conversion code you need to write. Once configured, to convert your entity to your data contract becomes a 1 liner:

Mapper.Map<CustomerDto>(customer);

This takes your customer entity and gives you back a new customer dto. It's all convention based and works by matching up property names. Even if the data contract isn't entirely identical to the entity, you only have to prompt AutoMapper for those properties it can't figure out for itself.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top