Home > CRM, Javista, Microsoft, Microsoft Dynamics, Tips and Tricks > Why a DeleteRequest returns a (empty) DeleteResponse

Why a DeleteRequest returns a (empty) DeleteResponse

You may have noticed that when working with the Execute method of the CRM web service you always get a response, even if it doesn’t contain any data. For instance, when passing a DeleteRequest you receive a DeleteResponse. There are no properties in the DeleteResponse though.

The reason is quite simple and I’m using the same approach in many of my own web services. Consider the following classes in a web service project:

[XmlInclude(AddRequest)]
[XmlInclude(SubtractRequest)]
public abstract class Request {
public abstract Response Execute();
}

[XmlInclude(AddResponse)]
[XmlInclude(SubtractResponse)]
public abstract class Response {}

public class AddRequest : Request {
public int x;
public int y;
public override Response Execute() {
return new AddResponse { result = x+y }
}
}
public class AddResponse : Response {
public int result;
}

public class SubtractRequest : Request {
public int x;
public int y;
public override Response Execute() {
return new SubtractResponse { result = x-y }
}
}
public class SubtractResponse : Response {
public int result;
}

public class MyWebService : WebService {
public Response Execute(Request request) {
return request.Execute();
}
}

There’s only one method named “Execute” in MyWebService and you can either pass an AddRequest or a SubtractRequest. As a response you get an AddResponse or a SubtractResponse. The great thing is that you never have to touch the Execute method again and therefore the interface of your service never changes. If you need new functionality then you create new Request and Response classes.

As the Execute method returns an abstract Response object even an operation not providing results, like the CRM DeleteRequest, has to return an instance of a derived Response class. That’s the reason why you receive an empty DeleteResponse n CRM.

The concept is very easy and powerful and as said before, I have used it successfully in many implementations. If you are creating web services then consider trying it. Even if you don’t then it helps understanding CRM a bit more.

Cheers,

Michael Höhne

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: