Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

Cimbalino Toolkit: Services

The Cimbalino toolkit is fast becoming a big favourite of mine and is starting to be used in more and more apps that I’m building. In it, you’ll find a huge amount of useful tools for building Windows Phone apps, from converters, to behaviours, to controls, to services. It’s a monster! I’m going to try and do a number of posts detailing certain parts of the toolkit and thought I’d start with Services.

Services

Pedro Lamas (Cimbalino author) has done a wonderful job in providing a huge array of services that have been cleverly split up into different nuget packages. The bulk of the services can be found in the main Toolkit package, but he also has a number of other packages all with just one or two services within them. The reason for doing this is each different package requires you to add a different capability to the app; putting all of these in one package would mean you having capabilities against your app that you just don’t need. The separated services are for: Camera, Phone Dialler, Device info, User info, Media Library and Location.

So how might you go about using these services? Well there are two ways: the normal way, the MVVM way.

The Normal Way

I’ve said this is the normal way, because all it involves doing is creating an instance of the class and using, as you would any other class. So basically:

        private string GetUserAnid()
        {
            return new UserExtendedPropertiesService().AnonymousUserID;
        }

And that’s a very simple usage of just one of the services. All the services can be used in this way.

The MVVM Way

My favourite approach to using MVVM is with the MVVM Light toolkit from Laurent Bugnion, and those who have used it will most likely know all about IoC and registering types and interfaces (if you’re not, this is a good starting place). For the purposes of this, I will assume familiarity with this approach. Easily the best advantage of using this approach is how much it allows you to have design time data available to you, and this is where what Pedro has done with Cimbalino becomes really apparent. All of the services he’s created are implementations of an Interface. So what does that mean? Well, in your ViewModelLocator, you’d be able to do something like this:

        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
                //SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
                SimpleIoc.Default.Register<IUserExtendedPropertiesService, UserExtendedPropertiesDesignService>();
            }
            else
            {
                //SimpleIoc.Default.Register<IDataService, DataService>();
                SimpleIoc.Default.Register<IUserExtendedPropertiesService, UserExtendedPropertiesService>();
            }

            SimpleIoc.Default.Register<MainViewModel>();
        }

Now, notice in the DesignMode we register the UserExtendedPropertiesDesignService, which is my own implementation of that interface. Mine looks like this:

    public class UserExtendedPropertiesDesignService : IUserExtendedPropertiesService
    {
        public T GetPropertyValue(string propertyName)
        {
            return default(T);
        }

        public string AnonymousUserID { get { return "MYAWESOMEANID"; } }
    }

Which you can see, if the AnonymousUserID property will return MYAWESOMEANID if it were to ever be called. This means, in one of your ViewModels, you could have the following:

    public class MainViewModel : ViewModelBase
    {
        private readonly IUserExtendedPropertiesService _userExtendedPropertiesService;

        public const string WelcomeTitlePropertyName = "WelcomeTitle";
        private string _welcomeTitle = string.Empty;
        public string WelcomeTitle
        {
            get
            {
                return _welcomeTitle;
            }
            set
            {
                if (_welcomeTitle == value)
                {
                    return;
                }
                _welcomeTitle = value;
                RaisePropertyChanged(WelcomeTitlePropertyName);
            }
        }

        public MainViewModel(IUserExtendedPropertiesService userExtendedPropertiesService)
        {
            _userExtendedPropertiesService = userExtendedPropertiesService;

            WelcomeTitle = _userExtendedPropertiesService.AnonymousUserID;
        }
    }

And this is where the cool stuff happens. Through the use of Dependency Injection, at the design time, my own Design implementation of IUserExtendedPropertiesService gets injected into the ViewModel, so when I call _userExtendedPropertiesService.AnonymousUserID, if I’m in design time, it returns MYAWESOMEANID, which is what you’d see in the Visual Studio/Blend designer, and if it’s called at run time, it will return what Pedro has in his implementation (which is the actual ANID in this case).

All of the services in Cimbalino can be used in this way, which makes it a really powerful toolkit to have for when you’re wanting information as well as design time data to help you build your amazing looking apps. And want to know just how many services are available?

This many
CimbalinoServices
And those are just the ones in the main toolkit package!

SL

Advertisements

One response to “Cimbalino Toolkit: Services

  1. Pingback: Windows Store Developer Links – 2013-05-02 | Dan Rigby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: