Templates for implementing resource extension in the ASP.NET web interface

I would like to implement RESTful services that support resource extension so that services are autonomous, but if clients want to limit the number of service calls they make, they can do this using the query string parameter. Some examples of resource expansion:

Let's say I have 2 services, each in its own CSPROJ:

  • ACME.AuthorsService
  • ACME.BooksService

The ACME.AuthorsService controller has a way to get the author by ID:

// GET /api/author/1
public async Task<HttpResponseMessage> Get(int id)
{
    Author author = await _authorRepo.GetById(id);
    if (author == null)
    {
        return Request.CreateResponse(HttpStatusCode.NotFound);
    }

    return Request.CreateResponse(HttpStatusCode.OK, author);
}

Which will return information about the author:

{
    "Name": "Alan Moore",
    "Href": "https://acme.org/api/author/1",
    "Books": [
        {
            "Id": 9999,
            "Title": "V for Vendetta",
            "Href": "https://acme.org/api/book/9999"
        },
        {
            "Id": 8888,
            "Title": "Neonomicon",
            "Href": "https://acme.org/api/book/8888"            
        }
    ]
}

Similarly, the ACME.BooksService controller has a way to get the book by ID:

// GET /api/book/1
public async Task<HttpResponseMessage> Get(int id)
{
    Book book = await _bookRepo.GetById(id);
    if (book == null)
    {
        return Request.CreateResponse(HttpStatusCode.NotFound);
    }

    return Request.CreateResponse(HttpStatusCode.OK, book);
}

Which will return detailed information about the book:

{
    "Id": 9999,
    "Title": "V for Vendetta",
    "ISBN-10": "140120841X"
    "ISBN-13": "978-1401208417",
    "Pages": 296,
    "Href": "https://acme.org/api/book/9999"
}

, "Href" . , https://acme.org/api/author/1?expand=books :

{
    "Id": 1,
    "Name": "Alan Moore",
    "Href": "https://acme.org/api/author/1",
    "Books": [
        {
            "Id": 9999,
            "Title": "V for Vendetta",
            "ISBN-10": "140120841X"
            "ISBN-13": "978-1401208417",
            "Pages": 296,
            "Href": "https://acme.org/api/book/9999"
        },
        {
            "Id": 8888,
            "Title": "Neonomicon",
            "ISBN-10": "1592911315",
            "ISBN-13": "978-1592911318",
            "Pages": 176,
            "Href": "https://acme.org/api/book/8888"            
        }
    ]
}

/ ASP.NET Web API , (ACME.AuthorsService, ACME.BooksService ..) "Href" .

, , , (: ) : :

  • HREF GetS (ControlService) - HttpClient, Book .
  • - BookService , ?
  • OData $expand ( Googling )?
  • - ?

, . , "Href" .

+4

Source: https://habr.com/ru/post/1536577/


All Articles