Issue
Below is my Interface -
public interface IDBClient {
public String read(ClientInput input);
}
This is my Implementation of the Interface -
public class DatabaseClient implements IDBClient {
@Override
public String read(ClientInput input) {
}
}
Now I have a factory which gets the instance of DatabaseClient like this -
IDBClient client = DatabaseClientFactory.getInstance();
....
Now I need to make a call to read method of my DatabaseClient which accepts the ClientInput parameter and below is the class for the same. This class was not written by me so that is the reason I am having a question on this and I am pretty much sure this is the wrong way of doing it.
public final class ClientInput {
private Long userid;
private Long clientid;
private Long timeout_ms = 20L;
private boolean debug;
private Map<String, String> parameterMap;
public ClientInput(Long userid, Long clientid, Map<String, String> parameterMap, Long timeout_ms, boolean debug) {
this.userid = userid;
this.clientid = clientid;
this.parameterMap = parameterMap;
this.timeout_ms = timeout_ms;
this.debug = debug;
}
}
So when customer make a call to read method of DatabaseClient, they will create the ClientInput parameter like this and then use the factory to get the Instance of DatabaseClient and then call the read method accordingly.
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("attribute", "segmentation");
ClientInput input = new ClientInput(109739281L, 20L, paramMap, 1000L, true);
IDBClient client = DatabaseClientFactory.getInstance();
client.read(input);
Problem Statement:-
- So my first question is does the
userid,clientid,timeout_msshould beLongobject or just simplylonginClientInputclass? - Second question I have is, it might be possible that customer can pass wrong information such as
negative user ids,negative client id,negative timeoutvalue etc etc.. Then where I should do this validation? Should I do this validation check in the constructor ofClientInputclass or at some other place? What's the better way of doing this and how should I do the validation?
Solution
I don't think there's a single correct answer. A few suggestions:
The biggest difference I see between
longandLongin this context is thatLongmay benull. If there's a possibility you might have missing values, theLongobject will be helpful asnullcan indicate missing values. If you're using primitives, you'll have to use some special value to indicate missing, which is probably going to be a mess. Speed or size is not likely to be an issue unless you're planning on making an array of a million of these things and then serializing.My preference for validation logic is to throw some sort of custom
ValidationExceptionat the point at which the thing could fail. If you're just creating these things with a constructor, the simplest thing would be just to validate there, e.g.public ClientInput(Long userid, Long clientid, Map<String, String> parameterMap, Long timeout_ms, boolean debug) throws ValidationException { if (userid == null) throw new ValidationException("UserId is required"); ...etc, etc... }
Ultimately, the ValidationException is only useful if you can catch it at a point where you can do something useful with it - echo it back to a user or whatever.
Answered By - Steve B.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.