Determining App or Session Context
I'd like to be able to use some of the same methods whether or not I'm using an app auth token or a user auth token, and need a test to determine which context I'm in. Something like the following:
publi开发者_Python百科c FacebookClient GetFacebookClient() {
return IsUserRequest() ? GetFacebookWebClient() : GetFacebookAppClient();
}
public dynamic GetUser(long fbId) {
var fbClient = GetFacebookClient();
return fbClient.Query(String.Format(@"SELECT uid,name FROM user WHERE uid = ({0})", fbId);
}
However, I can't seem to find a good test to implement IsUserRequest(). Any ideas?
I was using a singleton for my Facebook utilities class, and so I just allowed for two: a web context singleton, and an app context singleton.
public class FbUtil {
private static readonly FbUtil webInstance = new FbUtil(false);
private static readonly FbUtil appInstance = new FbUtil(true);
static FbUtil() {}
private FbUtil(bool isAppInstance) {
IsAppInstance = isAppInstance;
}
private readonly bool IsAppInstance;
public static FbUtil WebInstance {
get { return webInstance; }
}
public static FbUtil AppInstance {
get { return appInstance; }
}
public FacebookClient GetFacebookAppClient() {
var oauth = new FacebookOAuthClient { AppId=Config.FacebookConfig.AppId, AppSecret=Config.FacebookConfig.AppSecret };
dynamic tokenRes = oauth.GetApplicationAccessToken();
return new FacebookClient(tokenRes.access_token);
}
public FacebookClient GetFacebookWebClient() {
return new FacebookWebClient();
}
public FacebookClient GetFacebookClient() {
return IsAppInstance ? GetFacebookAppClient() : GetFacebookWebClient();
}
public FbUser GetUser(long facebookId) {
var client = GetFacebookClient();
dynamic result = client.Get(facebookId);
return new FbUser(result);
}
... etc. other FB utility methods
}
and I simply call it like so from my business objects:
var fbUser = FbUtil.WebInstance.GetUser("12345");
or
var fbUser = FbUtil.AppInstance.GetUser("12345");
This keeps the interface clean, and I don't have any redundant methods. If a method is not availlable in the app context, I throw an exception, which isn't ideal, but it suits my purposes for now. prabir, how do you feel about this approach?
精彩评论