Much like the Factory pattern I am looking for a good way to call the desired Data Access methods through a central class. Unfortunately, my DAL classes are static (as I need them to cache records) so I cannot use the Factory pattern. I don't want to convert them into Singletons either unless I see solid benefits in doing so.
In the following code Part
is related to Car
and Boat
via a junction object, and Part
has related child objects that need to be returned. This is why some of the methods return Maps:
public class CarDataAccess
{
public static Car__c[] queryCars(Id[] carIds) { ... }
public static Car__c[] queryCars(String[] uniqueIds) { ... }
public static Map<Car__c, Part__c[]> queryAllCars(Boolean activeOnly) { ... }
}
public class BoatDataAccess
{
public static Boat__c[] queryBoats(Id[] boatIds) { ... }
public static Boat__c[] queryBoats(String[] uniqueIds) { ... }
public static Map<Boat__c, Part__c[]> queryAllBoats(Boolean activeOnly) { ... }
}
/* The Part object has no Unique Id or Active field */
public class PartDataAccess
{
public static Part__c[] queryParts(Id[] partIds) { ... }
public static Part__c[] queryAllParts() { ... }
}
What I currently have is something like this:
public class ObjectDataAccess
{
public static SObject[] queryObjects(ObjectTypeEnum typ, Id[] ids)
{
if (typ == ObjectTypeEnum.CAR) ...
else if (typ == ObjectTypeEnum.BOAT) ...
else if (typ == ObjectTypeEnum.PART) ...
else throw error;
}
public static SObject[] queryObjects(ObjectTypeEnum typ, String[] uniqeIds)
{
if (typ == ObjectTypeEnum.CAR) ...
else if (typ == ObjectTypeEnum.BOAT) ...
else throw error;
}
public static SObject[] queryAllObjects(...)
{
// NO CLUE!
}
}
To me this doesn't look right at all. First, this is more of a Util class than anything else! Second, as you can see I have no idea how to handle the different input/output types for the selectAllObjects method.
All ideas for a proper implementation of this problem are welcome.
[apex]
? – Adrian Larson♦ Aug 29 '16 at 19:38...
into/*quick description of what is going on*/
that might help at least. – Adrian Larson♦ Aug 30 '16 at 4:19...
is a placeholder for the appropriate DAL method call. For example if type = BOAT theBoatDataAccess.queryBoats(...)
method is called. – Mossi Aug 30 '16 at 4:50queryAllObjects
, is the desired result where you get the object and it's related objects? Like for example if you queried Account, you'd get Accounts with Contacts, etc. ? – Programmable Medley Sep 9 '16 at 4:04