开发者

Do I need to translate enum values across a WCF service?

The scenario is as follows: I implemented a WCF service (lets call it X) which has its own data objects. The service X is using another WCF service (Y) which has its own set of data objects. Service X needs to pass some data it receive from service Y to its clients (service X clients). As far as i know, 开发者_如何学Pythonit is considered a "best practice" to translatethe objects received from Y service to data objects of X service.

What is the best practice when it comes to Enum values? Do i need to map each enum value or is there another way?


Generally the idea is to isolate users of your service from changes in your implementation. Therefore, you do not expose your implementation types on the wire. Image the situation where you decide to rename an enum value. If the service consumer does not update their implementation you will have introduced a breaking change as the service user will be sending the old enum value to you which will not deserialize correctly.

In addition you may find that not all of the enum values are applicable to users of your service (maybe they are used internally)

So, yes, you should translate enum values just like other types

If you give your enums explicit numeric values you could translate between them using casts

class Program
{
    static void Main(string[] args)
    {
        Internal i = Internal.Too;

        External e = (External) i;

        Console.WriteLine(e);
    }

}

enum Internal
{
    One = 1,
    Too = 2
}

[DataContract]
enum External
{
    [EnumMember]
    One = 1,
    [EnumMember]
    Two = 2
}

However, you would have to be careful that they did not become out of sync

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜