Proxy Pattern To Separate Dependencies

Introduction

Adding dependencies to your project is one of the biggest causes of project complexity. As the dependencies increase, so does the complexity. When developing frameworks (e.g. Exrin) this becomes an even bigger issue as you must continuously update your framework at the whim of your dependencies. When developing projects for your application, I prefer to also view them in this context. The application developer is then responsible for all the dependencies used in the application and not burdened referenced projects dependencies.

The Interface

Interfaces allow you to pass around objects without knowing about their implementation. You define your interface in the project that needs to use it. As an example, in Exrin there is an interface called INavigationContainer. Exrin needs to use a NavigationContainer, called a NavigationPage in Xamarin Forms but the framework it isn’t responsible for the Xamarin Forms reference, the application is. Hence the application needs to pass the instance to the framework.

// Limited example of the NavigationContainer
public interface INavigationContainer
{ 
    Task PushAsync(object view);

    Task PopAsync();
}

The Proxy

The interface shows what the Framework needs, now we must implement it in the application, where we have a reference to Xamarin Forms.

Proxy

Below is a very basic example.

public class NavigationContainer : INavigationContainer
{
    private NavigationPage _page = null;

    public NavigationContainer(NavigationPage page)
    {
        _page = page;
    }

    public async Task PopAsync()
    {
        await _page.PopAsync(); 
    }

    public async Task PushAsync(object page)
    {
        await _page.PushAsync(page as Page);
    }
}

Summary

Now we can pass the NavigationContainer into the framework as it implements INavigationContainer. This allows the framework to know nothing of Xamarin Forms, allowing you to freely upgrade or downgrade Xamarin Forms. You can now update the other project without having its project dependencies impact all the applications that reference it.

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer |
Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

Leave A Comment?