[ColdBox-4.1.0]: Scope for Active Entity in cbmongodb

35 views
Skip to first unread message

Ancient Programmer

unread,
Nov 26, 2015, 11:55:33 AM11/26/15
to ColdBox Platform
Hi folks,

I was wondering if you could help me clear my confusion. I am using "cbmongodb" on my project. So far, it is working as intended. However, after reading my references including ColdBox ORM, Wirebox, etc., I was not sure if I did correctly.

Here is what I have:

/models/User.cfc
component name="User" extends="cbmongodb.models.ActiveEntity" accessors=true scope="session" { ... }

/handlers/User.cfc
function create(event, rc, prc) {
var user = getModel("User");

if (!user.loaded()) {
// Populate entity
user.populate({
'email' = rc.email,
'password' = rc.password,
...
}


Is it appropriate to set Active Entity in session scope? I would image that each user that comes to the site has its own user object.


Thanks!

Jon Clausen

unread,
Nov 26, 2015, 1:09:59 PM11/26/15
to col...@googlegroups.com
You wouldn't want to store the Active Entity object in the session scope, in general, as it is not immutable when loaded via Wirebox.  

Scoping that in the session via the component attributes could also cause issues with using that entity in relation to other users, if another user is loaded (e.g. viewing a profile) during the active session.

I would recommend , for the current user, storing a serializable copy of the entity document (e.g. Session.currentUser = User.load(id).get_document() ).

Then, if you need the object, itself, you can load it from the session.currentUser["_id"] value, while still having access to the current user data throughout the session.

HTH,
Jon
--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coldbox/cb18fe6c-603e-486b-ac81-2104908d4859%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ancient Programmer

unread,
Nov 26, 2015, 6:16:47 PM11/26/15
to ColdBox Platform
Thanks for the reply.
Furthermore, I was thinking how this Active Entity fits in Bean and Service concepts. Or, am I making it more complicated?

Thanks for your time answering my questions during this Thanksgiving holiday. :)

Jon Clausen

unread,
Nov 26, 2015, 7:00:15 PM11/26/15
to col...@googlegroups.com
Not at all.  I think incorporating the entity in to either of those concepts depends on your usage.  For my purposes, the User Entity,  for example, is my User Bean.  Even if it doesn’t conform to the Java Bean standard 100%, it serves the same purpose, has a zero-argument constructor, and has the getters and setters of a Bean.  Since I can extend the functionality of the core Active Entity in my model, I rarely find a need to create another Object through which to interface the core properties of an entity. 

That being said, a User Entity is one which might be “Bean-worthy”, if you will, as there will be common task for a user which will fall outside the scope of the Active Entity.  Then again, you may be able to accomplish those with a service object.  See below...

The service layer is another matter, especially given the limitations of NoSQL in joining collections.  If I need to create, update or marshall entity relationships during CRUD operations, I’m almost always incorporating an entity service. Primary entities (those that don’t exist as an extension of or in service to to another entity) almost always require a service layer of their own.  I find that helps me to keep my components lean and the responses consistent throughout the service layer.
--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.

Maikel Verstegen

unread,
Aug 18, 2016, 12:10:49 PM8/18/16
to ColdBox Platform
Can someone explain how to implement the CBmongodb entities with a proper service layer? so an example of a model/entity.cfc, entityService and handler.
I'm using CBorm and want to be consistent. Many thanks

Op vrijdag 27 november 2015 01:00:15 UTC+1 schreef JClausen:

Ancient Programmer

unread,
Aug 19, 2016, 12:21:46 AM8/19/16
to ColdBox Platform
In cbMongoDB, there is CBORMCompatActiveEntity. Perhaps, you could create an ORM-like entity by extending CBORMCompatActiveEntity. (I haven't tried it yet if that works.)

This is an example of service layer I am using:

// models/Customer.cfc
component name="Customer" collection="customer" extends="cbmongodb.models.ActiveEntity" accessors=true {

property name="email"    validate="string" schema=true  unique=true;
property name="password" validate="string" schema=true;
....
}

// models/CustomerService.cfc
component {

property name="wirebox" inject="WireBox";

function init() {
return this;
}

function create(data={}) {
var customer = wirebox.getInstance("Customer");
customer.populate(arguments.data);
if (customer.isValid()) {
customer.create();
}
....
}
....
}

// handlers/Customer.cfc
component {

property name="customerService" inject="CustomerService";

function doRegister(event, rc, prc) {
if (event.valueExists("customer") && isStruct(rc.customer)) {
response = customerService.create(rc.customer);
....
}
....
}
....
}


Reply all
Reply to author
Forward
0 new messages