Search code examples
javascriptdesign-patternsdtobreezesingle-page-application

Does Breeze elminate the need for DTOs in single-page-applications?


I'm building my first SPA and I've gone as far as building DTOs for each of my entities, but I just found breeze and it seems like it takes care of serializing your changes into bare-minimum packages to optimize updates/additions/etc.

The reason I built DTOs is to "flatten" my data and limit how much data I'm putting on the wire, but I'm wondering if I even need this overhead if Breeze takes care of it.


Solution

  • There are reasons for DTOs. "Flattening the data" is not one of them. Neither is "limiting how much data I'm putting on the wire".

    Breeze does a nice job with object graphs. Imagine sending 100 orders for a customer. You wouldn't want to repeat the customer name on each order DTO. With Breeze you query for the Customer orders (use an "expand") and you get one copy of the Customer and the orders to go with it.

    var query = new breeze.EntityQuery.from('Customers')
               .where('Id', 'eq', 42)
               .expand('orders');
    

    On the other hand, if you only want a list of customer names, use a "projection":

    var query = new breeze.EntityQuery.from('Customers') // all customers
               .select('id, companyName'); // project into an anonymous 2-property object
    

    Use the occasional server-side DTO to build something that you can't easily create from the client (e.g., Customer-and-total-current-year-order-dollars).

    The point is that you can mix DTOs, projections, and entity queries to suit your needs. You do not have to go all one way or the other (in my opinion).