If I have a core class which is used internally by all the modules and then I would like to publish a wcf service which would make use of this class... then what is the best way to do that? Is it better to mark it as data contract directly, or better create a new class as data contract which either inherits or maps to the core structure or ... what do you suggest guys?
Asked
Active
Viewed 274 times
2
-
I also found this question related to this one which helps other people coming to this page: http://stackoverflow.com/questions/83604/wcf-datacontracts-and-underlying-data-structures – Learner May 09 '11 at 13:08
1 Answers
0
Either will work, since in most cases the client will be getting a WSDL/MEX representation - not your type directly. In either case, remember to use explicit [DataContract]
/[DataMember]
attributes so that it truly is contract-based, rather than looking at the fields.
(note: don't inherit a DTO from your domain type; a DTO should be separate, if you go that route)
Personally, I prefer a separate DTO, but I know people have success exposing the domain model directly (that is how LINQ-to-Data-Services works, for example).

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
-
Thank you very much for your feedback. So, if both would work, which one would be better? What if I would come up with a second service which would make use of the same class, but slightly different? – Learner May 09 '11 at 09:43
-
2@Cristi - in that case, separate DTOs. Without the additional context of second service", either is honestly workable. I prefer separate DTOs, but it is a but subjective until you have a specific requirement to push it one way or the other. – Marc Gravell May 09 '11 at 09:44
-
Thanks a lot Marc. You've been very helpful. Before I close this question could you please check this question that I have: http://stackoverflow.com/questions/5913177/wcf-multiple-service-contracts-using-pretty-same-data-contracts . Here everybody says that I should use inheritance and I was pretty sure that I should not do that. It's pretty close to this question, so please, if you have time and want to help. I just want to be sure that I go in the right direction. – Learner May 09 '11 at 09:51
-
Actually that is really kind of a concrete case with 2 different services using a core class. – Learner May 09 '11 at 09:52
-
@Cristi - wow, I hate that; very little isolation etc. Up to you of course. – Marc Gravell May 09 '11 at 10:01
-
Yeah, personally i really think creating new data contracts which maps from/into the core classes would give higher flexibility and decoupling... so maybe I should stick to this. – Learner May 09 '11 at 10:08