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

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!

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.

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. :slight_smile:

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.

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

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);

}

}

}

`