Lets say I would like to build an application to connect to a server and upload, delete, … files.
At first I need to connect somehow and I need a session. Can I build a function that is returning a session and after that I can do whatever I want with this session object? Something like:
mySession connect(url, user, password)
{
//connecting
return session;
}
void uploadFile(File f) {/*...*/};
var currentSession = connect(/*...*/);
currentSession.uploadFile(...);
currentSession.deletFile(...);
currentSession.close(...);
Could this be good? If I have a session object I can pass it everywhere and say .upload, .delete, .whatever.
And what do you think about the functions like void uploadFile() – maybe I should change void? Because after an upload how do I know that it was successful? Maybe a boolean is better? (if I get true, I know it was successful and if false It wasnt). Any ideas? 😉 Thx
My previous experience had the database session wrapped as IDisposable, so consumers would always request the database session via a using:
then in the wrapped session’s
Disposemethod, it would flush and close the connection and dispose anything necessary. This way I was ensured (usually) that the connection was closed and disposed of; at least if developers/myself used it properly.As for your second question, your
uploadFilecan return true/false on success or throw an exception. If you expect it to fail for legitimate reasons often, perhaps true/false is better. You could also have it return aUploadResultsobject which has a boolean pass/fail property along with the reason/exception as to why it failed.Then your code might look like this:
But that’s just one sample; you can play around with the design as it works for you.