Proxy Pattern To Separate Dependencies


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.


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);


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.


Subscribe to a hand-picked round up of the best Xamarin development links every week. Published every Friday. Free.

Signup to Weekly Xamarin

* I do not maintain or run this newsletter, but I do recommend it.