Coffeehouse Thread

9 posts

Relational models from database > server > client AND back again

Back to Forum: Coffeehouse
  • User profile image
    itsnotabug

    Let's say I have some db tables and some POCO classes that map properties 1 to 1 with table columns. I'd like to select out a complex relationship, map those correctly to instances of those POCO classes and shuttle that complex object down to the browser as JSON... THEN go the other way... post that JSON, deserialize it back to my complex object for processing on the server.

    Now obviously FK fields in the db schema should translate into List<POCO> in the "parent" object... so imagine an object with properties, including Lists of an object with properties, including Lists of an object with properties, etc...

    Is there an end-to-end solution for this scenario? I guess I'm interesting in hearing strategies on how to best maintain that relationship database to browser and back again.

  • User profile image
    kettch

    @itsnotabug: I don't think I've ever gone more than a couple of layers deep. You generally need to take your complexly related sets of objects and flatten them down into a view model (or several) of some kind that will easily translate into JSON. You might have to rely on something like knockout.js to help with keeping everything related.

    It seems like I always have the best luck with vastly flattened and simplified view models, but I can't think of any one go-to technique that I've ever seen.

  • User profile image
    ScanIAm

    Plus, it's wasteful of bandwidth to bring along all that data if it isn't absolutely necessary for the UI.  You want to display a subset of what's in the POCO, so only transmit that.

  • User profile image
    Jim Young

    Have you looked into ORM solutions like NHibernate or Entity Framework? The important thing in my mind is to have a distinct and isolated layer where database access occurs and marshals the data up to the POCO classes.

  • User profile image
    DCMonkey
  • User profile image
    itsnotabug

    , Jim Young wrote

    The important thing in my mind is to have a distinct and isolated layer where database access occurs and marshals the data up to the POCO classes.

    I think this is where I'm leaning... something like a distinct ViewModel layer containing my "client-safe" classes (almost the same as my DAL POCOs but with actual List<ViewModelPoco>, sans id's or other private sensitive info) and manually build up the state of my complex ViewModel step-by-step in each ViewModelPoco class constructor, by selecting out the "children" FK List<ViewModelPoco>. I know there are libraries like valueinjector to assist in merging the POCO values onto the ViewModelPoco values.

    , ScanIAm wrote

    Plus, it's wasteful of bandwidth to bring along all that data if it isn't absolutely necessary for the UI.  You want to display a subset of what's in the POCO, so only transmit that.

    I've been reading up on angular.js and that framework promises 2-way databinding on complex JSON. I think the amount of data transmitted would be offset by the fact I'll be dealing with a relatively "fat" client. A lot of multi-step stuff would be happening only in the browser, then submitting everything at the end in one post. (I know this is not REST).

  • User profile image
    JoshRoss

    If you have a simple schema, you might want to store xml in your database. It might be easier to reason about. As a fallback you can just use xml client side and middle tier.

    I guess it depends where you will be willing to have your solution fail. All solutions fail, but I like to have mine fail at points where I can reason about the outcome.

    -Josh

  • User profile image
    ScanIAm

    @JoshRoss:

    Agree+++.  Would agree again!

    But, I get that the question wasn't "what are the merits of doing this", so I'll leave it alone.

  • User profile image
    ZippyV

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.