Skip to main content

Xamarin Forms Core components Part 1 Dependency Service

Intro

Xamarin Forms is a collection of controls that can be used and rendered on multiple platforms, and in order of them to function as they are suppose to, they do need a set of core components that defines the way these controls, in how they are created, how they are rendered to how they are used, of course every platform is different and sometimes a platform specific extra configuration is required, specially that there are so many differences between the different platforms in matter of design, user experience and operating system behavior.


So one of the core components of  Xamarin  Forms is the Dependency Service, and by the name suggest it does act as the dependency resolver for all forms controls, if you are not familiar with IOC "Inversion Of Control" and Dependency Injection please refer to the link for a quick intro i wrote a while ago on IOC IOC Part 1 - Inversion of control Principle


And as of such the Dependency Service is the concrete inboxed DI Contatiner for Xamarin Froms, it is not only available under the hood, it still is part of the Xamarin Froms and Xamarin Platform Libraries, and for that you can at will use that dependency service to server whatever you may be able to. 


Just a quick note:- you can wrap the usage of the Xamarin Dependency service inside your own DI container if you are using another DI Container, which is the case most of time.

Which cases you can use Xamarin Dependency Service ?

Coming from the idea that the xamarin forms controls are all using the dependency service, but how ? 
The idea is really simple, Xamarin Forms is a set of Interfaces that are implemeneted differently on each platform it supports, Xamarin forms currently supports Andriod, iOS, MacOS (Beta), Windows Universal Apps, the dependency service resolves the interface to its platform implementation depending on which platform is calling that interface.

So if you need to have any functionality implemented differently on different platform for instance a storage manager that stores photos differently in all platforms, how does this work and what will happen using it?


The Problem?

So as we have said we want to create a storage manager that stores photos on each and every platform but most of our app logic is in the Xamarin.Forms PCL "Portable Class Library", so we will need to wire the Xamarin forms Interface for this to the implementation of the corresponding platform, and here is how :

The Solution

We add the interface that we want to interact with in our app logic "The PCL project", for instance the MVVM ViewModel, and so we start wit the interface

1- The Interface

using System;
public interface IStorageManager
{
   async Task SaveFile(string fileName, byte[] data);
}

As abstract as it is, Just a simple interface that saves a file
now to the implementation part we need to have an implementation for each platform

2-The implementation 

UWP

using System;

public interface StorageManager:IStorageManager
{
    public async Task SaveFile(string fileName, byte[] data)
    {
        try
        {
            FolderPicker folderPicker = new FolderPicker();
            folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            folderPicker.FileTypeFilter.Add("*");
            StorageFolder folder = await StorageFolder.GetFolderFromPathAsync();
            file = await folder.CreateFileAsync(fileName);
            File.WriteAllBytes(data);
        }
        catch (System.Exception)
        {
            return false;
        }
        return true;
    }
}



So is it enough for the runtime to know the implementation just because the class inherits from the IStorageManager Interface ??
the answer of course is no, we did not even configure the Dependency Service to resolve this, so here is how we use the dependency service to map the interface to the implementation to be resolved at runtime

Using the the Dependency Attribute



using System;
[assembly: Xamarin.Forms.Dependency (typeof (StorageManager))]
namespace StorageManager.UWP
{
    public interface StorageManager:IStorageManager
    {
        public async Task SaveFile(string fileName, byte[] data)
        {
            try
            {
                FolderPicker folderPicker = new FolderPicker();
                folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
                folderPicker.FileTypeFilter.Add("*");
                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync();
                file = await folder.CreateFileAsync(fileName);
                File.WriteAllBytes(data);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }
    }
}

iOS

And for the iOS we can use this implementation :
using System;
[assembly: Xamarin.Forms.Dependency (typeof (StorageManager))]
namespace StorageManager.iOS
{
    public interface StorageManager:IStorageManager
    {
        public async Task SaveFile(string fileName, byte[] data)
        {
            try
            {
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFile sampleFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                await FileIO.WriteAllBytesAsync(sampleFile, text);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }
    }
}

Android

And for the Android we can use this implementation "Same as iOS":
using System;
[assembly: Xamarin.Forms.Dependency (typeof (StorageManager))]
namespace StorageManager.Android
{
    public interface StorageManager:IStorageManager
    {
        public async Task SaveFile(string fileName, byte[] data)
        {
            try
            {
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFile sampleFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                await FileIO.WriteAllBytesAsync(sampleFile, text);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }
    }
}

Conclusion: 

The idea to have the dependency service available is to give the power to modify use, and assume the real power behind Xamarin forms, which is writing reusable components that can be used on as much platforms as possible.

Comments

  1. BlueHost is the best hosting company with plans for any hosting requirements.

    ReplyDelete

Post a Comment

Popular posts from this blog

(AsyncWebClient) Async Webclient for windows phone 8

Using windows phone web client is pretty common but the thing is you can not use it using Async -> Await mechanism so i used threading to create an async functionality for The Download string and upload string methods here is the code below // Comment public class AsyncWebClient { public Task DownloadString(Uri uri) { var task = new TaskCompletionSource(); try { var client = new WebClient(); client.DownloadStringCompleted += (s, e) => { task.SetResult(e.Result); }; client.DownloadStringAsync(uri); } catch (Exception ex) { task.SetException(ex); } return task.Task; } public Task UploadString(Uri uri, string content) { var task = new TaskCompletionSource(); try { var client…

RuntimeBinderException cannot resolver property in Unit Test in case of dynamic return.. Solved

Xamarin UI Tests – Deep dive Part 1

IntroAs the title may describe this is the first of the series of articles that will cover the UI Test of Xamarin in a deep dive, we will start simple and then dig deeper as we go. Since this is very well the first of the series, it will mostly cover up the architecture and the testing technique that Xamarin UI Tests uses.

The Technology
Xamarin UI Test is an automation testing framework similar to selenium, Watir, Watin (.net), Robot and Sikuli, if you have used BDD (Behavioral Driven Development) or a more advanced TDD (Test Driven Development) approach at least you must have come across one of these frameworks, and in that perspective comes Xamarin UI Testing as an automation framework designed specifically for Xamarin Automation testing, For starter Xamarin UI Tests was not build from scratch, instead it was built on top of another UI Automation testing framework that is targeting android and iOS sepecifically which is Calabash, I have to say that this choice was made wisely as of n…