Repository Layer (Day 8)

The repository layer is where your connection to data stores or information occurs, bring back the data and convert it to a business entity. On mobile apps it is most common to see a direct connection to an API or a connection to a local SQLite DB that I will talk about further in the series. While there are quite a few approaches to repository patterns this is the simple pattern that I have found works well.


As you will see in the ApiRepository project there is a BaseRepository and each repository has its own implementation (units of work) for each task it must perform. I have created partial classes to separate out each unit of work but it could all be in one class.

The Repository is rather simple. Call an API, get a Dto, pass the Dto back up the chain to the service layer or model. Because the Repository is a bottom layer, the Dto’s are a common definition across projects. I have put the Dto’s in a separate Definition Project.

API Connectivity

Connecting to the API is as easy as an HttpClient with JSON.Net. As you will see in the BaseRepository there are a number of helper functions. In the constructor, I disable caching, I actually prefer this as a default because if I am bothering to do an expensive call an API I don’t want any cached results I will cache myself.

The standard REST conventions apply of GET, PUT, POST and DELETE. The response is captured, deserialized and put into a Result object. On sending information the object is serialized and sent. Its really simple, not much more to say on that subject.

Speed Up Your HTTP

*For Android and iOS only* Download Modern Http Client and the here is the usage. (Thank Paul Betts for this awesome package). You don’t even need to change your code except for the initialization of your HttpClient. I haven’t done performance tests on this but I can say just install it on an existing app and notice the speed improvements. *I will be adding this to the project soon*


I’m going to go into authentication a bit later and I will be using the very simple JWT Auth as an example. You will notice in the BaseRepository there is an InjectAuthorizationHeader. In this function I inject the token to the HttpClient headers and authorization is complete.

Local Storage Repository

In this example I have used an API as it is the most common. But there may be some times when you want to use local storage or even both in a Sync relationship.

If you are using a DB as local storage, make sure your app only knows about the local DB and keep the sync service separate and self managing. The DB connects/syncs with the API and the app connects with the DB.


I haven’t created a public example of this (only done it in a live project). It works well to keep the 2 processes very separate otherwise complexity sky rockets. They are complications such as notifying other repositories of a sync in process and its best to provide a queuing process on read and writes but that can provide serious bottleneck issues. That’s an entire blog post by itself.

If you need something simple Azure Mobile Services may be an option for you, which is Xamarin compatible. I haven’t tried. version 2.0 but I started using 1.3 over a year ago and dropped it because it was far too restrictive.

5 Common Pitfalls In Enterprise Mobile Development

Based on my experience, of over 9,000hrs of Xamarin.Forms development, check out some of my hard learnt lessons, in enterprise Xamarin.Forms development.

<< Messenger Service (Day 7) ||  Security And Authentication (Day 9) >>

14 Days To Building An Enterprise Quality Xamarin Forms App

One Comment