• Dependency Injection

For ease of configurability, it's recommended to use a dependency Injection/Inversion-Of-Control framework such as Unity to handle the automatic initialization of the main SDK objects (repositories, clients, serializer) in charge of making the communications back and forth to the Yammer API endpoints seamless. You can copy this code in your App.xaml.cs file and call the _configureIoC()  in your initialization routine.

 

  • Initialize SDK Objects

In the code samples we use the Unity IoC Framework to handle the lifespan of the core objects. Below is an example of how this can be done (and this is how the sample code does too):

 

        private IUnityContainer _container;

        private void _configureIoC()
        {
            _configureIocOAuth();
            _configureIocClients();
        }

        private void _configureIocOAuth()
        {
            _container = new UnityContainer();

            _container
                .RegisterSingletonType<IQueryStringSerializer, QueryStringSerializer>()
                .RegisterInstance<ISerializer>(new JsonDotNetSerializer())
                .RegisterInstance<IDeserializer>(new JsonDotNetDeserializer())
                .RegisterInstance<IWebProxy>(WebRequest.DefaultWebProxy)
                .RegisterSingletonType<ICookieStore, CookieStore>();

            _container
                .RegisterInstance<IClientConfiguration>(new ClientConfiguration(
                    MyOAuthClientInfo,
                    ProductInfoHeaderValue.Parse("Yammer_Activities_ModenApp/1.0.0.0"),
                    MyYammerUris));

            _container.RegisterSingletonType<IAuthClient, AuthClient>(
                new InjectionConstructor(typeof(IClientConfiguration),
                    typeof(IQueryStringSerializer),
                    typeof(ISerializer),
                    typeof(IDeserializer),
                    _container.Resolve<OAuthResponseErrorHandler>(),
                    typeof(ICookieStore),
                    typeof(IWebProxy)));
        }
        private void _configureIocClients()
        {
                _container.RegisterSingletonType<IAutoCompleteClient, AutoCompleteClient>(
                    new InjectionConstructor(typeof(IClientConfiguration),
                                             typeof(IQueryStringSerializer),
                                             typeof(ISerializer),
                                             typeof(IDeserializer),
                                             _container.Resolve<OAuthResponseErrorHandler>(),
                                             typeof(ICookieStore),
                                             typeof(IWebProxy)))
                .RegisterSingletonType<IOpenGraphClient, OpenGraphClient>(
                    new InjectionConstructor(typeof(IClientConfiguration),
                                             typeof(IQueryStringSerializer),
                                             typeof(ISerializer),
                                             typeof(IDeserializer),
                                             _container.Resolve<OAuthResponseErrorHandler>(),
                                             typeof(ICookieStore),
                                             typeof(IWebProxy)))
                .RegisterSingletonType<IUserClient, UserClient>(
                    new InjectionConstructor(typeof(IClientConfiguration),
                                             typeof(IQueryStringSerializer),
                                             typeof(ISerializer),
                                             typeof(IDeserializer),
                                             _container.Resolve<OAuthResponseErrorHandler>(),
                                             typeof(ICookieStore),
                                             typeof(IWebProxy)))
                .RegisterSingletonType<IMessageClient, MessageClient>(
                    new InjectionConstructor(typeof(IClientConfiguration),
                                             typeof(IQueryStringSerializer),
                                             typeof(ISerializer),
                                             typeof(IDeserializer),
                                             _container.Resolve<OAuthResponseErrorHandler>(),
                                             typeof(ICookieStore),
                                             typeof(IWebProxy)));


            _container
                .RegisterSingletonType<IStateManager, StateManager>()
                .RegisterSingletonType<IAuthRepository, AuthRepository>()
                .RegisterSingletonType<IUsersRepository, UsersRepository>()
                .RegisterSingletonType<IOpenGraphRepository, OpenGraphRepository>()
                .RegisterSingletonType<IUserRepository, UserRepository>()
                .RegisterSingletonType<IMessageRepository, MessageRepository>();
        }

The advantage of using a container such as Unity (like in the code above) is that it will take care of the lifecycle of your objects, and also handle the singleton objects as needed. (Note: the RegisterSingletonType method use here, it’s an extension method we have created on top of the Unity container for ease of use).

Once all these objects are initialized, you’re pretty much ready to start using the SDK and integrating Yammer functionality into your app.

 

OAuth Process

Last edited Apr 26, 2014 at 10:58 PM by jmjc95, version 3