architecture - When a Service delegates data retrieval to a Data Mapper, which one should return a Model? -


i have set basic architecture, in service delegates retrieval of data data mapper (pseudocode below).

productservice->fetchbyid(123);  class productservice {     public function fetchbyid(id)     {         product = productmapper->fetch('id', id);         // or product = new product(productmapper->fetch('id', id)) ?         return product;     } }  class productmapper {     public function fetch(bysomething, value)     {         // fetch stuff db.         // return new product or raw data?      } } 

what best practice here if want end with, let's say, productcollection, or product?

  • should data mapper create instances of these models , return them service, passes them along actor?
  • or should data mapper give raw data service, instantiates models data before passing them caller?

i think responsibility of data access code convert raw data query i.e. sql object. datamapper mapping if didn't return object?

i'm not clear why need service @ though here - for? not

var theobject = thedataaccessthing.get(someid)

[update]

if service has logic i'd structure code (example in c#) like:

public class orderservice {     private readonly iorderrepository _orderrepository;     public orderservice(iorderrepository repository){         _orderrepository= repository     }      public void applydiscount(orderid){         var order = _orderrepository.get(orderid);         order.applydiscount();         _orderrepository.save(order);     } } 

or statically:

public static class orderservice {     public static void applydiscount(iorderrepository orderrepository, int someorderid)      {         var order = orderrepository.get(orderid);         order.applydiscount();         orderrepository.save(order);     } } 

this makes easy test, allowing change underlying implementation of iorderrepository see fit.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -