Quantcast
Channel: Software Development and Infrastructure in the Cloud » Diego
Viewing all articles
Browse latest Browse all 3

Monotouch – Repository Pattern – Proxy Pattern – IoC

$
0
0

I’m a .NET technician. I have been developing .NET solutions (web apps) since more than 10 years. I’m a mobile developer too… now… IOS developer. I develop IOS solution using .NET (Mono) framework, thanks to Monotouch and many thanks to Xamarin Studio.

What I was looking for was a way to join my past experiences with my new needs.

One of the most important needs that my customers ask to me is to allow application running without an internet connection even if master data are stored on a remote server (SAP? Microsoft SQL SERVER? MySQL SERVER? AS400? it’s not relevant). Network connection (throughout http channels) is the foundation for a web app, but not for a mobile app. This is the constraint I want to analyze in this post.

Obviously it is a nonsense to check, before every call, if network connection is available. Code repetition is only one of the negative aspect of this approach. What about if a potential user goes to one of his customer where network connectivity isn’t abstent, but it isn’t stable? If you check at application startup you could find connection, that will be lost in a couple of seconds or minutes. These are only some of possible situations and relative issues.

Inversion of Control, Repository Pattern and a Proxy Pattern can solve all of your problem. In this post I won’t tell you about these patterns, I’ll use them, I’ll talk to you about them. Spend your time looking at them and you’ll find interesting information for your source code.

First of all: IoC Container. We have to find the right one. Looking around the net I tested many libraries… and TinyIoc is the easiest for me. (TinyIoC).

IoC will be the key to reduce lines of code and to make maintenance easier.

Repository Pattern: define an internet (ex. IRepository) where you’ll define all your methods. All the methods that allow you to access data or services (the access to a remote server will be accomplished through REST serves, of course… NOT DIRECTLY, right? :)  ).

Then implement your interface twice: RestRepository (i.e. for remote access, via RESTFull services) and SqliteRepository (i.e. if locally you’ll use Sqlite to store data).

A third implementation of IRepository interface will be ProxyRepository. This is the access point for you app. Your app will not contact RestRepository or SqliteRepository directly, but it will contact only ProxyRepo.

The Proxy will contain routing logic.

Let’s code!

With IoC you will instantiate one variable for the proxy and inside the proxy a variable for both Rest and Sqlite.

Repository registration at app startup.


TinyIoCContainer current = TinyIoCContainer.Current;
current.Register<IResponse, IosRestResponse>(new IosRestResponse(), "RestResp");
current.Register<IRequests, IosRestRequests>(new IosRestRequests(), "RestReq");
current.Register<IRepository, ProxyRepository>(new ProxyRepository(), "ProxyRepo");
current.Register&ly;IRepository, RestRepository>( new RestRepository(), "RestRepo");
current.Register<IRepository, SQLiteRepository>( new SQLiteRepository(), "SqliteRepo");

In your Business classes use the PROXY repo:

IRepository _repo = (IRepository) TinyIoCContainer.Current.Resolve<IRepository>("ProxyRepo");
...
_repo.CallSomeMethod(params);

What about Proxy Implementation?

 

public void UpdateCurrentRepository(ConnectionType? connType)
{
ConnectionType = connType.HasValue ? connType.Value : (!IsConnected ? ConnectionType.Offline : ConnectionType.Online);
if (!IsConnected || CurrentConnectionType == ConnectionType.Offline)
_currRepo = (IRepository) TinyIoCContainer.Current.Resolve<IRepository>("SqliteRepo");
else
_currRepo = (IRepository) TinyIoCContainer.Current.Resolve<IRepository>("RestRepo");}

With this method you can switch between offline mode (SqliteRepo) or online mode (RestRepo).

IRepository methods will be easy like this:

 


public ListGetCustomers(CustomersRequest request)
{
return _currRepo.GetCustomers(request);
}

 

That’s all. Easy. Simple. Userfull. Powerfull.

Just another info. If you download TinyIoC from the link I posted a few line above and you’ll try to deploy it to a mobile device or compile with ad-hoc configuration, you’ll find that the package failed or crashes because he cannot find system.web reference. In a Mono framework there is not this library. And so? No problem. Remove from IoC project the file “TinyIoCAspNetExtensions.cs” that uses this reference. Compile the project and use it. Now your app will work perfectly.

 

Bye!

Diego

 


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images