Hi,
I'm looking for advice on mapping between different XML Schamata.
The C# (2.0) project I'm currently working on needs to send custom business
entities to multiple webservices, each accepting the same entity but in a
different schema.
As an extra complication the local entities are not serializable, so I have
implemented ISerializationS urrogate to fill the StreamingContex t with object
data.
I'm looking for a way to map the name/value pairs in StreamingContex t to the
target schema.
interface Local.IEntity { DateTime startDate; DateTime endDate;}
interface Remote.IEntity { DateTime[] days; }
What I have written so-far (see below) feels like I'm overcomplicatin g
things, please suggest a better way.
sealed class Local2RemoteSer ializationBinde r : SerializationBi nder
{
public override Type BindToType(stri ng assemblyName, string
typeName)
{
switch (typeName)
{
case "Local.Enti ty":
return typeof(Remote.E ntity);
}
}
}
class Converter : ISerializationS urrogate {
Remote.IEntity Convert(Local.I Entity input)
{
SurrogateSelect or ss = new SurrogateSelect or();
ss.AddSurrogate (this);
StreamingContex t sc = new
StreamingContex t(StreamingCont extStates.Remot ing);
ISerializationS urrogate surrogate = new Converter();
IFormatter formatter = new BinaryFormatter (ss); // could be an
XML formatter
MemoryStream ms = new MemoryStream();
formatter.Seria lize(ms, input);
// reset the memorystream
ms.Position = 0;
formatter.Binde r = new Local2RemoteSer ializationBinde r();
return (Remote.IEntity )formatter.Dese rialize(ms);
}
#region ISerializationS urrogate Members
//... removed for brevity
#endregion
}
--
Joris van Lier
I'm looking for advice on mapping between different XML Schamata.
The C# (2.0) project I'm currently working on needs to send custom business
entities to multiple webservices, each accepting the same entity but in a
different schema.
As an extra complication the local entities are not serializable, so I have
implemented ISerializationS urrogate to fill the StreamingContex t with object
data.
I'm looking for a way to map the name/value pairs in StreamingContex t to the
target schema.
interface Local.IEntity { DateTime startDate; DateTime endDate;}
interface Remote.IEntity { DateTime[] days; }
What I have written so-far (see below) feels like I'm overcomplicatin g
things, please suggest a better way.
sealed class Local2RemoteSer ializationBinde r : SerializationBi nder
{
public override Type BindToType(stri ng assemblyName, string
typeName)
{
switch (typeName)
{
case "Local.Enti ty":
return typeof(Remote.E ntity);
}
}
}
class Converter : ISerializationS urrogate {
Remote.IEntity Convert(Local.I Entity input)
{
SurrogateSelect or ss = new SurrogateSelect or();
ss.AddSurrogate (this);
StreamingContex t sc = new
StreamingContex t(StreamingCont extStates.Remot ing);
ISerializationS urrogate surrogate = new Converter();
IFormatter formatter = new BinaryFormatter (ss); // could be an
XML formatter
MemoryStream ms = new MemoryStream();
formatter.Seria lize(ms, input);
// reset the memorystream
ms.Position = 0;
formatter.Binde r = new Local2RemoteSer ializationBinde r();
return (Remote.IEntity )formatter.Dese rialize(ms);
}
#region ISerializationS urrogate Members
//... removed for brevity
#endregion
}
--
Joris van Lier