A day in the life...

public class GeekEarth : Earth { }
posts - 14, comments - 22, trackbacks - 0

Tightly coupling generic types

My server application works purely on a request/response pattern, like so

   1:  var query = new GetCustomerQuery(customerUniqueID);
   2:  var response = 
   3:    AppServer.Execute<GetCustomerQuery, GetCustomerQueryResponse>(query);

 

What I wanted to avoid though was the possibility that the user (me writing the client app) would do something silly like the following code and use the wrong pair combination

   1:  var query = new GetCustomerQuery(customerUniqueID);
   2:  var response = 
   3:    AppServer.Execute<GetCustomerQuery, GetEmployeeQueryResponse>(query);

 

Up until now I had the server interface defined like this, so that I can at least ensure the generic parameters are a Query and Response…

   1:  TResponse Execute<TRequest, TResponse>(TRequest request)
   2:    where TRequest : Request
   3:    where TResponse : Response;

 

But a very simple addition ensured that the response type specified is the right type for the request.

   1:  TResponse Execute<TRequest, TResponse>(TResponse response)
   2:    where TRequest : Request
   3:    where TResponse : Response, IResponseFor<TRequest>;

 

Then when I create my GetCustomerQueryResponse class I merely need to declare it like so

   1:  public class GetCustomerQueryResponse : Response, 
   2:    IResponseFor<GetCustomerQuery>
   3:  {
   4:    //etc
   5:  }

 

Now the client code above won’t compile because GetEmployeeQueryResponse does not implement IResponseFor<GetCustomerQuery>.

Print | posted on Tuesday, August 18, 2009 11:44 AM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 6 and type the answer here:

Powered by: