3

I am developing a Web API as services layer for a ASP.NET web forms application.

There are two controllers ContractorController (gives details about the contractor) and PaymentsController (gives details about payments done to a contractor).

Now in our UI application we have this User-Control which will be used across many pages:

enter image description here

Is the responsibility of the UI to call the ContractorController and PaymentsController separately to get the required data? Or should I provide a Consolidated Method (may be on PaymentsController) to give back all the required data, which seems to be going against RESTful approach or dealing with resources?

Thank you.

  • 1
    I'd say this depends fully on the consequences of this decision. Do you have any benefit in building this "consolidated method" (maybe db overhead of doing two calls joining on the contractor, for example) that justifies the time and maintenance (that is, cost) of having an aditional method specifically for this? You might build first having the UI calling separate methods and refactor to a consolidated one later if needed. I would prefer this later strategy, but it all depends on the factors your are facing. – h7r Jan 25 '15 at 17:06
  • The only reason I thought of doing a Consolidated Method is that is for performance reasons, which would enable app developer to call Service layer once instead of calling it two times. On second thought we are having some pretty aggressive caching strategy on Contractor Controller, so I guess performance will not be as bad as I thought. We are having lot of scenarios like this, so I wanted to put some serious thought into it before making any decision. – Sri Harsha Velicheti Jan 25 '15 at 17:13
  • I would advice against building this new method until the need for it is shown. "Premature optimization is the root of all evil". This might have the added benefit of making possible to bring the functionality to a productive state in shorter time. – h7r Jan 25 '15 at 17:15
  • 2
    @h7r: That's not what "Premature Optimization" means. Perhaps you're thinking of YAGNI? – Robert Harvey Jan 25 '15 at 17:57
  • @RobertHarvey YAGNI would fit too. But why do you say it "doesn't mean" that? It means whay it means; you might argue that it does not fit the problem, however I think it does. – h7r Jan 25 '15 at 18:08
1

I would make it this way:

  • Encapsulate this UI piece into a component, that is responsible only for UI part of the job. Upon construction this component needs data about both contractor and payments to be provided to it (via constructor, some setter methods - whatever. Depends on your design)

  • Encapsulate database-related logic into two services (ContractorService and PaymentService). Those services can fetch data from database using conditions you provide and prepare it (convert types, fill related object instances, localize via calls to LocalizationService etc).

  • Now in any controller action (e.g. in any section /page of your website regardless of how controller is called) you first call respected services to fetch the data and then feed this data into your UI component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.