Hi I have been reading some lecture notes and I cant work out why this method:
[OperationContract]
Student PostStudent (Student student);
Is good.
And this method is bad:
[OperationContract]
void PostStudent (string firstname, string lastname etc..);
Yet my implemented version is this:
[OperationContract]
void PostStudent(Student student);
So Im not sure if my implemented version is bad, Im also unsure how my lecturer got
Student PostStudent (Student student); // ?
Web services are built upon the use of messages. A message in WCF is defined by writing a class, which your
Studentclass is, and (optionally) marking it with theDataContractattribute. This enables versioning and setting various properties on the properties of that class (although the latter effect can also be achieved using the MessageParameter attribute).So yes,
PostStudent (string firstname, string lastname etc..)is bad.Whether or not to return something from that method is up to you. A
voidcan be perfectly fine, because using for example SOAP you can return a Fault indicating why the user could not be created: no error means the creation went well.When you want to inpect the created Student, you might as well define a
PostStudentResult(or aPostResult<T>) class and return that, containing the propertiesStudent(orT Result) andStatus, where the first contains the student as it’s created and the latter indicates whether or not the creation was successful.