LimeBean icon indicating copy to clipboard operation
LimeBean copied to clipboard

Deal with joins in an OO way without Class Creation

Open Nick-Lucas opened this issue 7 years ago • 0 comments

Right now we can make multiple requests to the database and get related objects and code this into Bean subclasses, but one of Limebean's best features is you don't need these subclasses to do work. It might be nice to have an interface which can handled this for us, and allow CRUD operations on joins, without the need for table classes.

Conversely it might be totally overkill to do this as there are ways to achieve it already. But this would provide a common pattern to work with

A simple interface might look like:

Bean bean = api.Load("books", 7);
bean.Join("authors", foreignKeyField, joinKeyField);
List<Bean> authors = bean.Joins["authors"];
authors.foreach(a => api.Trash(author));

Or maybe with new objects to create and manage the join tree:

BeanPod pod = api.DispensePod("books")

// Single join on Book
pod.Join("authors", "author_id", "id")

// Chained join tree from Book
pod
  .Join("publishers", "publisher_id", "id")
  .Join("publisher_contacts", "id", "publisher_id")
  .Join("contacts", "contact_id", "id");

// Load an update
Bean book = pod.Load(7);
Bean contact = book
  .Joins["publishers"]  
  .Joins["publisher_contacts"]
  .Joins["contacts"]
  .FirstOrDefault(c => c.name == "Bob James");
if (contact != null) api.Trash(contact);

(.Joins[...] could lazy load as we need things, instead of eager loading entire trees)

Nick-Lucas avatar Jul 02 '16 13:07 Nick-Lucas