Skip to content

Getting data from request

padzikm edited this page Dec 11, 2015 · 8 revisions

Almost every request carries some data with it, that we can divide into two categories:
First category - publicly available data, that is known to all components, no matter where it comes from, such as common orderId created by main application when user want to check out his shopping cart, or collection of productIds delivered as part of a form. These data is well defined on a page and all components know how to extract this information from request.
Second category is private component data, that belongs to only one component and other parts of a system don't know about existing these informations. That can be for example products names in shopping cart when checking out shopping cart - only ProductsService knows this data, because it's the only one that is responsible for products names in a system (however productIds can be publicly known). To avoid name conflicts on a page - because other services can put the their private data with the same names - we can simple prefix each service's private data uniquely - for example with service name - which can be easily done using template views. In this way we can conclude that each service knows what data it put into request's data and therefore it also knows how to get it from the request.
Finally where to get data from - all sended data resides in static HttpContext, so when main application publishes request to services, each service has access to all needed data and it can get it out in a way it wants.
Example of form widget for adding cutomer, where some data is public and some data is private, using templates:

@model CustomerModel
@Html.EditPublic(p => p.CustomerId) //public data without service specific prefix - <input name="CustomerId" value="someId">
@Html.Edit(p => p.Name) //private data that will be rendered as <input name="CustomersService.Name" value="SomeName">
@Html.Edit(p => p.Surname) //private data that will be rendered as <input name="CustomersService.Surname" value="SomeSurname">

Example of action handling adding customer post request:

[HttPost]
[InternalAction]
public ActionResult AddCustomer([Bind(Prefix = CustomersConst.ServiceName)] CustomerModel model, Guid customerId)
{
    //handling logic
    return RequestHandled();
}

Of course data which is public on page is available to all components, so main application can also use it while handling request:

[HttPost]
public ActionResult AddCustomer(Guid customerId)
{
    //Handle request
    return RedirectToAction("CustomerSummary", new { id = customerId });
}
Clone this wiki locally