X
X

Login

Login if you already have an account

LOGIN

Register

Create a new account. join us!

REGISTER

Support

Need some help? Check out our forum.

FORUM
May 7, 2012 vprakash@miegoapps.com

GrabBags, References and Back references

References

A Reference is a member data type which allows objects to refer to other objects. For example, a Person object could have members named mother and father which are references to other Person objects.

When you define an objecttype in your application.ffdl file, the datatype for a Reference is REFERENCE. Thus, to create our Person example, we could write:

CREATE OBJECTTYPE Person name STRING, gender STRING, mother REFERENCE, father REFERENCE

On the client side, objects pointed to by references are only retrieved if they have not already been. The FatFractal client SDKs ensure that only one copy of a particular object resides in client memory.

GrabBags

A Grab bag is a named set of  references to other objects. Extending our Person example, we might wish to have a grab bag called siblings which would hold the set of references to a Person’s brothers and sisters.

When you define an objecttype in your application.ffdl file, the datatype for a Grab bag is GRABBAG. Our Person example would then be defined:

CREATE OBJECTTYPE Person name STRING, gender STRING, mother REFERENCE, father REFERENCE, siblings GRABBAG

Object references are added and removed from GrabBags using the grabBagAdd or grabBagRemove methods in the various APIs.

The following client code snippets illustrate use of the Person object as we have defined it.

[tabs_framed] [tab title="iOS"] [toggle title="Person.h"] [fancy_pre_box]
@interface Person : NSObject
@property (strong, nonatomic) NSString  *name;
@property (strong, nonatomic) NSString  *gender;
@property (strong, nonatomic) Person    *mother;
@property (strong, nonatomic) Person    *father;
@end
[/fancy_pre_box] [/toggle] [toggle title="Person.m"] [fancy_pre_box]
@implementation Person
@synthesize name, gender, mother, father;
@end
[/fancy_pre_box] [/toggle] [fancy_pre_box]
FatFractal *ff = [[FatFractal alloc] initWithBaseUrl:@"http://acme.fatfractal.com/myapp"];
Person *fred = [ff getObjFromUrl:@"/Persons/abcdzyxw"];
NSError *err;

NSArray * fredsSiblings = [ff grabBagGetAllForObj:fred
grabBagName:@"siblings"
error:&err];
NSArray *fredsDadsBrothers = [ff grabBagGetAllForObj:fred.father
grabBagName:@"siblings"
withQuery:@"gender eq 'MALE'"
error:&err];

[/fancy_pre_box] [/tab] [tab title="Java (Android)"] [toggle title="Person.java"] [fancy_pre_box]
public class Person {
private String name, gender;
private Person mother, father;
public Person(String name, String gender, Person mother, Person father) {
this.name = name; this.gender = gender;
this.mother = mother; this.father = father;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public Person getMother() { return mother; }
public void setMother(Person mother) { this.mother = mother; }
public Person getFather() { return father; }
public void setFather(Person father) { this.father = father; }
}
[/fancy_pre_box] [/toggle] [fancy_pre_box]
FatFractal ff = FatFractal.getInstance(new URI("http://acme.fatfractal.com/myapp"),
new URI("https://acme.fatfractal.com/myapp"));
Person fred = ff.getObjFromUri("/Persons/abcdzyxw");

List fredsSiblings = ff.grabBagGetAll(fred, "siblings");
List fredsDadsBrothers = ff.grabBagGetAllForQuery(fred.getFather(), "siblings", "gender eq 'MALE'");

[/fancy_pre_box] [/tab] [tab title="JS (Server)"] [toggle title="Person"] [fancy_pre_box]
function Person() {
this.name = null;
this.gender = null;
this.mother = null;
this.father = null;
this.clazz = "Person"; // connect to iOS/Android class
return this;
}
[/fancy_pre_box] [/toggle] [fancy_pre_box]
var ff = require('ffef/FatFractal');
var fred = ff.getObjFromUri("/Persons/abcdzyxw");

fredsSiblings = ff.grabBagGetAll(fred.ffUrl, "siblings")
fredsDadsBrothers = ff.grabBagGetAllForQuery(fred.father.ffUrl, "siblings", "gender eq 'MALE'");

[/fancy_pre_box] [/tab] [/tabs_framed]

References and GrabBags in Queries

GrabBags and references are treated as sub-resources during a query. This means that the names of the reference or grab bag members may be included in the request URI. For example, let’s say we have an application whose base URL is

http://acme.fatfractal.com/myapp

Now let’s say we have a collection, /Persons, which contains all of our Person objects, and that Fred from above has a guid of “abcdzyxw”. Then we can find Fred’s mother at:

http://acme.fatfractal.com/myapp/ff/resource/Persons/abcdzyxw/mother

and find his brothers and sisters at:

http://acme.fatfractal.com/myapp/ff/resource/Persons/abcdzyxw/siblings

References vs. GrabBags

References are intended to support easy modeling of and access to your application’s data graph by your application code. GrabBags, on the other hand, allow you to dynamically create relationships between objects without necessarily having to reflect that in your application’s object model.

References and GrabBags also differ in their load behavior. References are retrieved dynamically and on-demand by the client-side SDKs. GrabBags, on the other hand, must be specifically requested. For example, if you load a Person from the above examples, that Person’s mother and father will also be loaded, while siblings will only be loaded on request.

Back references

For every object, the FatFractal Platform will automatically keep track of what other objects refer to it. Such automatic references are called Back references. Every object automatically has a BackReferences grab bag, and it is maintained automatically by the FatFractal Platform.

Back references allow you to easily find what objects have a reference to a given object through a URI. If we have the URI of a given object, it’s back references are accessed like this:

<object URI>/BackReferences.<collection>.<member>

For example, notice that our Person object has no member for children. Nevertheless, back references allow us to very easily find them. Let’s say we want to find Fred’s children. Recall that Fred may be retrieved from the URI

http://acme.fatfractal.com/myapp/ff/resource/Persons/abcdzyxw

Fred’s children can then be found at

http://acme.fatfractal.com/myapp/ff/resource/Persons/abcdzyxw/BackReferences.Persons.father

In words, this says, “Give me all objects in the Persons collection whose father member is the Person with guid abcdzyxw.”

Contact