Saturday, 7 September 2013

What is Best practice managing/creating classes for client side (any) and server side of web-service (WCF)

What is Best practice managing/creating classes for client side (any) and
server side of web-service (WCF)

I need some advice:
For exposing web-service I'm using wcf basicHttpBinding.
1. Service has single instance mode and keeps List<ExtendedClientAction>
in memory.
2. Service should recieve <ClientAction> and save it to that
List<ExtendedClientAction>
3. Service should return List<ClientAction>, which is truncated list of
ExtendedClientAction
So I've faced some problems: ExtendedClientAction class on the server side
has constructor and fields which has getters and setters. I'm not sure
that it is a good practice to share types, because it could have some
businesses logic, which should be closed, and it could have much more
fields which should not be exposed to the client.
That is why, I've devided it two new classes ClientAction for client
purposes and Inherited class ExtendedClientAction. ClientAction class
doesn't have any constructors and getters and setter for fields, because
they couldn't travel across the wire. And ExtendedClientAction has all the
staff.
So client sends to wcf service ClientAction, which is decorated and saved
on server side as ExtendedClientAction. It works ok.
But when I'm trying to send back List of ClientAction I face problem,
because my List is actually of Type of ExtendedClientAction because I
simply cast List of ExtendedClientAction to Listof ClientAction. And as
don't want to expose ExtendedClientAction I don't decorate it with
DataContract attributes, so I have error as Service aborts this request.
Question is what is best practice to keep interoperability between wcf and
any different clients when creating and sharing classes?
//class for client purposes
[DataContract(Namespace = "http://www")]
public class ClientAction
{
[DataMember]
public DateTime StartTime;
[DataMember]
public DateTime EndTime;
[DataMember]
public string Name;
[DataMember]
public Guid Guid;
[DataMember]
public int Id;
[DataMember]
public Statuses Status;
}
below class for serverside
// class inherites ClienAction and is for server side purposes
public class ExtendedClientAction:ClientAction
{
private Guid _guid;
private Statuses _status;
private DateTime _endTime;
private DateTime _startTime;
public ExtendedClientAction()
{
_guid = Guid.NewGuid();
}
public Guid Guid
{
get
{
return _guid;
}
private set { var me= value; }
}
public DateTime EndTime
{
get { return _endTime; }
set
{
_endTime = value;
_status = Statuses.Finished;
}
}
public DateTime StartTime
{
get { return _startTime; }
set
{
_startTime = value;
_status = Statuses.Started;
}
}
public void Copy(ClientAction clientAction)
{
this.Name = clientAction.Name;
this.StartTime = clientAction.StartTime;
this.EndTime = clientAction.EndTime;
}
public string Comment;
}
here is method of interface and implentation of this interface:
[OperationContract(Name = "ReportForAllDates")]
List<ClientAction> ReportAll();
implementation:
public class TimeManagementService : ITimeManagement
{
private List<ExtendedClientAction> _myActionList;
public List<ClientAction> ReportAll()
{
return _myActionList.Cast<ClientAction>().ToList();
}
}

No comments:

Post a Comment