I'm quite stuck on one part of using WCF for a client/server messaging system, and would very much appreciate some help.
On the server, I've got a Message DataContract object where one of the properties points to a typed collection of MessageBody DataContracts. A stripped down version of the class looks like this:
[DataContract]
class Message {
[DataMember]
string From;
[DataMember]
string To;
[DataMember]
string Subject{get;set;}
[DataMember]
MessageBodies {get;}
}
[DataContract]
class MessageBodies : CollectionBase<MessageBody>{}
[DataContract]
class MessageBody {
[DataMember]
BodyType Type get {get;set;}
[DataMember]
string Body {get;set;}
}
From the App.Client.dll, I create a WCF Proxy of the a ServiceContract and DataContracts (btw: no referencing to a common 'App.Contracts.dll' where I could have put the above defined DataContracts), For transporting data from client to server, I'm now all set...
But from user functionality side on the client side, there's still a ways to go.
I want to ensure that users can work with the above properties, but with some type checking happening as they instantiate the client objects. For example, I wish the actual class that users work with to look more like:
class MessageBody {
//ReadOnly only:
public BodyType Type get {get {return _Type;}}
private BodyType _Type;
//Validated property:
public string Body {
get{ return _Body;}
set{
if (value == null){throw new ArgumentNullException();}
_Body = value;
}
}
private string _Body;
//Set via Constructor only:
public MessageBody (BodyType type, string body){
//validate type and body first...
_Type = type;
_Body = body;
}
}
One direction I tried to use to solve this was as follows:
If I renamed the DataContract from Message to CommMessage, I could then wrap the POCO/WCF object with a smarter object... But although this would work for most of the properties, except for the collection properties:
public Message {
CommMessage _InnerMessage;
public string Subject {get {_InnerMessage.Subject;}}
//Option 1: give direct access to inner object...but they are just poco's...
public CommMessageBodies MessageBodies { get {_InnerMessage.Addresses;}}
//Option 2...don't have one yet...what I would like is something like
//this (returning MessageBody rather than CommMessageBody):
//public MessageBodies MessageBodies {get {_InnerMessage.Bodies;}}
}
Thanks very much for any and all suggestions!