I have a lightning component controller class:
public class ComponentVisibilityController {
@AuraEnabled
public static Boolean checkToDisplayComponent(Id recordId) {
return ComponentVisibilityFacade.run(new LeadComponentVisibility(recordId));
}
}
In the ComponentVisibilityFacade
class:
public class ComponentVisibilityFacade {
public static Boolean run(ComponentVisibility componentVisibility) { //LeadComponentVisibility is a Child of ComponentVisibility.
componentVisibility.setCurrentUser();
.
.
.
.
.
return componentVisibility.displayUnshared();
}
}
In the ComponentVisibility
class I have the below method and more:
public virtual class ComponentVisibility {
protected User currentUser = null;
public void setCurrentUser() {
currentUser = UserDetailsUtil.queryUserDetails(userInfo.getUserId());
System.debug('currentUser: ' + currentUser.Name);
}
public Boolean displayUnshared() {
.
.
.
}
}
As you can see UserDetailsUtil
is static class that calls queryUserDetails
method and returns the current user.
I have other Util classes (most of them just query various objects and return results) that are all static and are called in LeadComponentVisibility class.
So in summary:
- A static class
ComponentVisibilityController
(I know the class can instantiated but since it only has a static method, I am not going to instantiate it, hence a static class) is calling another static classComponentVisibilityFacade
, where a new instance ofLeadComponentVisibility
(dynamic binding) is created which calls another static classUserDetailsUtil
(via inheritance) to get current user.
Now:
- Should I make
UserDetailsUtil
an instantiable class? Or is it ok I leave it static? I mean is there a problem with multiple instances ofLeadComponentVisibility
class calling the staticUserDetailsUtil
? - Is my design correct? That is, I am following good software design principles(SOLID)? Or am I missing something or can the class structures be improved? For example:
UserDetailsUtil
just queries user object based on the user or lead owner id I am passing and does nothing else(the S in SOLID). LeadComponentVisibility
decides whether to make a component visible or not but also calls other classes and their methods because I need to decide whether a component is visible based on current users and owners roles, hence setCurrentUser method and others. Is calling these classes and their methods inLeadComponentVisibility
OK or am I violating the S?- Since this is Util class if today itself I end up using
UserDetailsUtil.queryUserDetails(Id user)
method in another class that requires another field. What should I do? Since this field is used only in this another class and not inComponentVisibility
class, I am not unnecessarily querying a field I don't need? - In
UserDetailsUtil.queryUserDetails(Id user)
, I am querying only required fields of User. But tomorrow (say a year later) if I need another field of the user for the sameComponentVisibility
class, how would I do that? Yes I know I can simply modify the query. But this violates the O in SOLID. Should I query all the fields so that I don't violate the O? Again, I am not unnecessarily querying fields I don't need?
getUserById(Id userId)
, getUserByFederationId(String fedId),
getUserByXXX(..)` and return eitherUser
orList<User>
and let caller pluck field they need. Underlying utility needs to query once and cache; or accept collections as input