duckhamqng / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
Apache License 2.0
0 stars 0 forks source link

Improve FileDataStore implementation #462

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
One of our users suggested the following:

https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.
DotNet4/Apis/Util/Store/FileDataStore.cs?r=eb702f917c0e18fc960d077af132d0d83bcd6
a88

This class is not sealed, but its behaviour is like a sealed class, because has 
a private member called folderPath that is the atom of this class and is 
visible outside only by a getter propertyFolderPath
The constructor receives a folder name and create it under %APPDATA%
But my application cannot depend from the user… I wish to store my data in my 
own directory in my location. How to do this? Deriving is impossible because 
folderPath member is privateand not visible from a derived class and is used by 
all the methods. Using the “new” keyword hiding this member was not a good 
idea. I wrote my own class TokenDataStore (also redefining the 
GenerateStoredKey static method to define the name of the file) with plenty of 
copy-and-paste programming… blah….

SUGGESTION 1
folderPath should become “protected” so that can be visible in a derived 
class.
GenerateStoredKey should become “virtual” instead of “static” so that 
can be overridden in a derived class.
A programmer can define its own IDataStore implementation so that just defining 
the constructor without calling base constructor can handle its own folderPath 
building, then can call its own GenerateStoredKey instead of official one (my 
stored keys don’t need to have the full name of the type into the name of the 
file). For all other operations, FileDataStore is great and a derived class 
would use the same logic. IMHO this is the best solution.

SUGGESTION 2
Without defining a derived class: just add a new constructor
        public FileDataStore(string folder, bool isCompleteFolder)
        {
            if (isCompleteFolder)
            {
               folderPath = folder;
            }
            else
            {
               folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), folder);
            }

            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
        }

… so that a programmer can fully use the logic of FileDataStore, but just 
deciding where to keep files.

All combinations of these suggestions can be evaluated too. Mine is only a 
message to improve a class that has actually a good implementation!

Original issue reported on code.google.com by pele...@google.com on 5 May 2014 at 3:44

GoogleCodeExporter commented 9 years ago
More details are available also here:
http://stackoverflow.com/questions/23405767/system-unauthorizedaccessexception-a
ccess-to-the-path-google-apis-auth-is-deni/23430287?noredirect=1#comment36108622
_23430287

Original comment by pele...@google.com on 8 May 2014 at 2:10

GoogleCodeExporter commented 9 years ago
https://codereview.appspot.com/97380043

Original comment by pele...@google.com on 12 May 2014 at 8:37

GoogleCodeExporter commented 9 years ago

Original comment by pele...@google.com on 14 May 2014 at 8:06

GoogleCodeExporter commented 9 years ago
FIXING the "fix": https://codereview.appspot.com/100490046/

Original comment by pele...@google.com on 19 May 2014 at 5:01