twitter - Making IAuthorizor or TwitterContext Global / Accessible -


one more follow extract bearertoken linqtotwitter iauthorizer

i using auth / context / bearertoken in application wanted use l2t library check on rates. need twittercontext so, out of twitterctx scope.

is possible set public class can access anywhere without need reauthorize?

thank in advance.

there different ways this. here couple can go singleton route or factory method.

note: there's debate on whether singleton appropriate pattern use or whether global reference object appropriate @ all, that's isn't you're asking.

public class twittercontextservice {     static twittercontext twitterctx;     public static twittercontext instance     {                 {             if (twitterctx == null)                 twitterctx = createtwittercontext();              return twitterctx;         }     }      public static twittercontext createtwittercontext()     {         var auth = new applicationonlyauthorizer()         {             credentialstore = new inmemorycredentialstore             {                 consumerkey = environment.getenvironmentvariable(oauthkeys.twitterconsumerkey),                 consumersecret = environment.getenvironmentvariable(oauthkeys.twitterconsumersecret)             },         };          return new twittercontext(auth);     } } 

then there 2 ways can use -

as singleton:

twittercontext twitterctx = twittercontextservice.instance; 

or factory method:

twittercontext twitterctx = twittercontextservice.createtwittercontext(); 

alternatively, can use ioc container (tons of information , available libraries on web) , pass dependencies code uses twittercontext. guess there several different ways this.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -