开发者

Transforming objects

Is 开发者_运维问答there simple way transform one object in another, first defined in application object model and another defined in web service refference. Definitions of two objects are the same.


If you are using WCF, you can actually share types defined in a WCF service with clients of that service.

See the section Using the Employee Class on the Client of this article:

http://10rem.net/blog/2009/07/13/sharing-entities-between-wcf-and-silverlight

Types defined in your web service become available to (.NET) clients consuming them by electing to reuse types defined on the WCF side.

UPDATE:

As promised in one of my comments, here's code to create a copy of an object using serialization. I'm including both a variant that expects a specific class, and a variant that uses generics to support any serializable type. You can replace DataContractSerializer with BinarySerializer in many cases.

static public Organization Copy(Organization org)
{
    MemoryStream stream1 = new MemoryStream();

    //Serialize the Record object to a memory stream using DataContractSerializer.
    DataContractSerializer serializer = new DataContractSerializer(typeof(Organization));
    serializer.WriteObject(stream1, org);

    stream1.Position = 0;
    //Deserialize the Record object back into a new record object.
    Organization orgCopy = (Organization)serializer.ReadObject(stream1);

    return orgCopy;
}

static public T Copy<T>(T obj)
{
    MemoryStream stream1 = new MemoryStream();

    //Serialize the Record object to a memory stream using DataContractSerializer.
    DataContractSerializer serializer = new DataContractSerializer(typeof(T));
    serializer.WriteObject(stream1, obj);

    stream1.Position = 0;
    //Deserialize the Record object back into a new record object.
    T objCopy = (T)serializer.ReadObject(stream1);

    return objCopy;
}


you could probably map the objects one to the other having an helper class which copies/assigns member to member.

In general the best approach is anyway to avoid this kind of transformation especially if objects are the same as in your case.

This can be avoided with a good design, for example in WCF specifying data contracts and having such classes implementing a common interface shared in both service and application, then of course you should program in all the places against the interface and not the class which implements it.

for old XML web services, when data contracts were not available yet, something can still be done using interfaces but then it also depends on the way you expose your types, if the service returns a class of its own type or of a type defined in a common class/interface library.


You could use AutoMapper to do this:

AutoMapper is an object-object mapper ... AutoMapper works best as long as the names of the members match up to the source type's members. If you have a source member called "FirstName", this will automatically be mapped to a destination member with the name "FirstName".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜