generic requests on agatha
does anyone know why I can't do this ?
public class CreateScenario开发者_如何学PythonHandler :
GL.RRSL.RequestHandler<CommandRequest<ScenarioProfileData>,
CommandResponse<ScenarioProfileData>>
why is it imposible for Agatha to figure out the type of the generic Request. It is defined there. ?
Type 'GL.RequestResponse.CommandRequest`1[T]' cannot be exported as a schema type because it is an open generic type. You can only export a generic type if all its generic parameter types are actual types.
any ideas of how to do this. It feels so restrictive to have to create a request object for each type of operation.
I'm actually using generic requests/responses successfully.
The trick was to register closed generic requests/responses as known-types. In order to achieve this, I'm using the following conventions:
- generic requests/responses can have only one generic parameter
- that generic parameter should has a generic constraint that specifies that it should implement a given interface
I'm using this convention to construct every possible closed generic type that I'm going to be using as request or response. For example, I can have something like this:
interface IDtoWithId
{
int Id { get; }
}
public class GetEntityRequest<TDto> : Request where TDto : IDtoWithId
{
....
}
public class UserDto : IDtoWithId
{
public int Id { get; set; }
public string Name { get; set; }
}
Then, when configuring Agatha, I'm using something like this https://gist.github.com/916352 and doing:
....
configuration.Initialize();
KnownTypeProvider.ClearAllKnownTypes();
KnownTypeHelper.RegisterRequestsAndResponses(typeof(UserDto).Assembly);
The KnownTypeHelper registers the GetEntityRequest type as a known-type and that allow me to handle that request using a handler hierarchy like this:
public abstract class GetEntityHandler<TEntity, TDto> :
RequestHandler<GetEntityRequest<TDto>, GetEntityResponse<TDto>>
{
...
}
public class GetUserHandler : GetEntityHandler<User, UserDto>
{
}
I'm using this approach for the CRUD part of an application and it is working very well.
The problem here has to do with how CommandRequest and CommandResponse are defined. Agatha looks at the classes which extends Request and Response and add's them to the known types in the WCF.
When the server starts the service, WCF complains that the type CommandRequest is generic and can't be used. WCF if saying that it can't claim to know about a generic type.
When I define CommandRequest and CommandResponse as abstract, and then create classes like ScenarioIORequest/Response which extend CommandRequest and CommandResponse respectively with the apropiate type to be wrapped, WCF does not complain.
It feels like a waste that I have to define specific types when I would like to have generic requests and responses for different DTO. Maybe this will change at some point, but it seams to be WCF issue rather then the Agatha project issue.
精彩评论