Passing BO between layers?


#1

In my application design I use a multilayered approach (see this for additional info). To pass data between layers I user Business Objects/Entities. Basically they are “expensive” property bags (if you understand the expression “cheap property bags”), but serve to carry data from the database or data layer into the UI and back again. In the second article, if you change tiers to layers, that is how my applications are structured.

The BLL (business logic layer) acts as Ember’s router to decide how data can be created, modified and destroyed. I want to create an object on one page and pass it to the appropriate route when done. I’m hoping to use a query string parameter so the page that creates the object knows which route to pass the object to when done.

The route would load the data for that controller/view (the model) and add this newly created object to the model (also save) and display the page/view as normal. But I see no way to pass a created object from one controller to another route.

OK, as I was typing this I had this pop into my head. ControllerA could call it’s route (RouteA) to access the route’s method controllerFor to get ControllerB and pass the new object to ControllerB (causing B to save the data) and then ControllerA could transition to RouteB which would load the new data and display ViewB (what I want to happen). The only problem is I don’t see a property on the controller that gives me access to it’s route (this.route?).

How do you solve/handle this kind of problem?


#2

I’m not sure if I’m fully grasping your problem, but you can do something like this in your routeA:

actions: {
    create: function (){
       var object = MyObject.create({fizz: buzz});
       this.transitionTo('routeB', object')
    }
}

(You can also put this action on your controller and use this.transitionToRoute())

This will cause your app to transition to route B, with the newly created Object being set to the ‘model’ property on controllerB. (Skipping the usual model hook).

Have a look here for details on transitionTo and how it interacts with the route lifecyle: http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo

Sorry if i missed your problem :frowning:


#3

No, you’re at the heart of the problem. Let me see if I can say it clearer. Each MVC group represents one page of an Ember application, and I might go a step farther and say that each MVCTR (T = template, R = route) represents a page. I have a Model, a Controller, a View, a Route, and a Template for each page.

In my case I have an Index page, from which you can go to the Collection page (a page that shows all the animals in your collection) or you can go to a Plan page (a page that shows a breeding plan over years). Users only have one collection but can have lots of plans, even plans with hypothetical animals (animals not in their collection, they may plan on getting in the future, or animals they just want to see the results of if they crossed them).

From either the Collection page or a Plan page you can access the CreateSnake page. On that page I create a “Snake” object. Depending on which page you came from determines which page you return to and which Model that snake object gets added to.

In the case of the Collection page the snake object gets added to an array. The Plan page is more complex as the snake gets added to the Plan model but for a specific year, and if you plan goes into the future, each of the years after the year the snake was added.

That complexity I want in the Route, who manages the CRUD of objects, and all I want to pass to the route is the snake object, clearly not to replace the Model which is an object that contains, among other things, the collection array, but to add that newly created snake object to the model about to be displayed by the route (who would also persist it before display).

The way I figured out to do it is have the CreateSnakeController load the CollectionModel add the snake object and persist it, then transition to the CollectionRoute who reloads the Model (with the new snake) and renders the Collection page.

But this feels wrong, it feels like I’m messing with the internals of another object. What I want to do is only load the model once…Ah HAH!!! As I was typing this I got that I could pass the loaded Model from the CreateSnakeController to the CollectionRoute via the optional additional model parameter you show in the code above. Ok, minor victory, the part that still feels wrong is that the CreateSnakeController loads data (that belongs to the CollectionRoute) and persists data, then shells off to the route. Who should be doing the loading and persisting BUT the Route knows nothing about the query strings driving the process and I know of no way to get a reference to the route from the controller to pass the Snake object along so the route (acting as a BLL) can process the new object accordingly.