2

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.

7
  • 1
    It's not super clear to me what you're actually asking. Also, why did you untag [apex]?
    – Adrian Larson
    Aug 29, 2016 at 19:38
  • @AdrianLarson I have a set of static classes that I would like to call through a single medium. My apologies for the confusion. The Apex tag was initially there because I was thinking of Apex Enterprise Patterns. But I figured it may be redundant because most questions in SFSE are Apex related.
    – Mossi
    Aug 29, 2016 at 19:41
  • I still think you leave too much to the imagination for this question to be effectively possible to answer. Maybe if you expanded ... into /*quick description of what is going on*/ that might help at least.
    – Adrian Larson
    Aug 30, 2016 at 4:19
  • The ... is a placeholder for the appropriate DAL method call. For example if type = BOAT the BoatDataAccess.queryBoats(...) method is called.
    – Mossi
    Aug 30, 2016 at 4:50
  • In the queryAllObjects, 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. ? Sep 9, 2016 at 4:04

1 Answer 1

1

You might look at how the Selector library does it. Or just use that library; why reinvent the wheel? It allows you to do things like:

// In Service Layer
public static Select.Filter isAccountTask()
{
    return Select.Field.startsWith(Task.WhatId, SObjectType.Account.getKeyPrefix());
}

// In Handler Layer
List<Task> accountTasks = TaskService.isAccountTask().filter(newRecords);

In this case, it seems like you might wish to filter a generic List<SObject> to get the records of a specific type? You might want something like:

public Select.Filter isSObjectType(SObjectType schemaType)
{
    return Select.Field.startsWith('Id', schemaType.getDescribe().getKeyPrefix());
}
1
  • You know, this post helped me realize I should have probably said Data Access Layer instead of Selector because I am not needing all the features of the Selector library.
    – Mossi
    Aug 29, 2016 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .