简体   繁体   中英

WCF - how to expose strongly typed objects to clients from a base type (without client casting?)

I have a WCF service that needs to return different types of Quotes (ie Quote1, Quote2, Quote3) -- all inherit from Quote

My plan is to expose a single method

Quote GetQuote(Message message);

However then I am forcing the client to do something like this

if (quote is Quote1) elseif (quote is Quote2) etc

Without having a different method for each quote type is there a good way to distinguish Quotes without having the client cast to each type?

Anyone have any suggestions on a better way to do this?

Thanks

If you are using DataContracts then you can decorate them with the KnownTypeAttribute .

Either expose different method for each Quote type or do the cast on the client - that is the whole point of exposing the base type. There is no way to avoid casting if you expose the base type and you need to work with derived types on the client.

You can also define single logic of your Quote type which will serve all your needs (including methods working with Quote data) - derived types will override some functionality. Then you can share assembly with Quote definitions between the server and the client application and use polymorphism.

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