Pergunta

I have the following request objects:

[Route("/Model/Blah/BySlug/{Slug}/", "GET")]
[Route("/Model/Blah/ByBlahSeriesId/{SomeOtherId}/", "GET")]
[Route("/Model/Blah/ById/{Id}/", "GET")]
public class GetBlahRequest : Blah, IReturn<BlahResponse>
{
    public int Start { get; set; }
    public int Limit { get; set; }

}

[Route("/Model/Blah/", "POST")]
public class PostBlahRequest : Blah, IReturn<BlahResponse>
{

}

[Route("/Model/Blah/", "PUT")]
public class PutBlahRequest : Blah, IReturn<BlahResponse>
{

}

And I'm being told there are conflicting routes on the GET?

How can that be? they are very different. The code to call the get is:

 var blahResult = _client.Get(new GetBlahRequest
            {
                Id = model.BlahId
            });

Am I missing something here?

Foi útil?

Solução

You do have a conflict. Because your GetBlahRequest DTO when extended with Blah would be the equivalent to:

public class GetBlahRequest
{
    public int Id { get; set; }
    public int BlahSeriesId { get; set; }
    public string Slug { get; set; }
    public int Start { get; set; }
    public int Limit { get; set; }
}

When the client tries to make a request say with { Id = 123 } it can't know which of the 3 routes to choose from. If BlahSeriesId is not nullable (int?) then it defaults to 0, Slug will default to null but will be treated as empty. It effectively sees these three routes:

/Model/Blah/BySlug//
/Model/Blah/ByBlahSeriesId/0/
/Model/Blah/ById/123/

While in this scenario, it's obvious it should choose the last route, the client is really warning of scenarios where you may specify a Slug and an Id, which according to your DTO is perfectly valid, and thinks aargh!

GetBlahRequest
{
    Id = 123,
    Slug = "Something"
}

Would result in three possible routes of:

/Model/Blah/BySlug/Something/
/Model/Blah/ByBlahSeriesId/0/
/Model/Blah/ById/123/

While it's a little more work, the routes should be separated to DTOs with clearer intent.

public class GetBlahRequest : Blah
{
    public int Start { get; set; }
    public int Limit { get; set; }
}

[Route("/Model/Blah/ById/{Id}/", "GET")]
public class GetBlahById : GetBlahRequest, IReturn<BlahResponse>
{
}

[Route("/Model/Blah/BySlug/{Slug}/", "GET")]
public class GetBlahBySlug : GetBlahRequest, IReturn<BlahResponse>
{
}

[Route("/Model/Blah/BySeriesId/{BlahSeriesId}/", "GET")]
public class GetBlahBySeriesId : GetBlahRequest, IReturn<BlahResponse>
{
}

Then in your action methods:

public BlahResponse Get(GetBlahById request)
{
    return GetBlah(request);
}

public BlahResponse Get(GetBlahBySlug request)
{
    return GetBlah(request);
}

public BlahResponse Get(GetBlahBySeriesId request)
{
    return GetBlah(request);
}

BlahResponse GetBlah(GetBlahRequest request)
{
    // Code to return BlahResponse
    return ...
}

Then your client needs to call:

var blahResult = _client.Get(new GetBlahById { Id = model.BlahId });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top