Maxim Korobov
Maxim Korobov

Reputation: 2572

Better client side of web-service

I'm writing client app for web-service using C# and MonoTouch. It has some commands for user session (register, login, logout), messaging (list, write, delete, like), image routines (upload, download) and so on.

I wrote implementation for user session request types, which could be executed asynchronously, like that:

{
    public delegate void LoginEventHandler(Session session);
    public delegate void ErrorEventHandler(Error error);
    public delegate void StatusEventHandler(string status);

        public interface IDataProvider
        {
            #region Session
            Session GetSession();

            event LoginEventHandler OnRegistered;
            event ErrorEventHandler OnRegisteredError;
            void Register(Person user);

            event LoginEventHandler OnLoggedIn;
            event ErrorEventHandler OnLoggedInError;
            void Login(Person user);

            event StatusEventHandler OnLoggedOut;
            event ErrorEventHandler OnLoggedOutError;
            void Logout(Session session);
            #endregion
        }
    }
}

It works, but even now it's a bit boring and weak to implement 2 event handlers with unsubscribing from them later for any requests:

dataProvider = new DataProviderService ();
...
appDelegate.dataProvider.OnRegistered += HandleOnRegistered;
appDelegate.dataProvider.OnRegisteredError += HandleOnRegisteredError;
appDelegate.dataProvider.Register(GetPerson());
...
void HandleOnRegistered (Session session)
{
    InvokeOnMainThread(delegate {
        appDelegate.settings.Session = session;

        appDelegate.dataProvider.OnRegistered -= HandleOnRegistered;
        appDelegate.dataProvider.OnRegisteredError -= HandleOnRegisteredError;

        Waiter.HideWaiter();
        Console.WriteLine(session.userId);
    }
    );
}

void HandleOnRegisteredError (Error error)
{
    InvokeOnMainThread(delegate {
        appDelegate.dataProvider.OnRegistered -= HandleOnRegistered;
        appDelegate.dataProvider.OnRegisteredError -= HandleOnRegisteredError;

        Console.WriteLine(error);
    });
}    

Any suggestion to improve architecture for further development? Or any material to see how guru's usually do?

Upvotes: 0

Views: 239

Answers (1)

jonathanpeppers
jonathanpeppers

Reputation: 26505

Generally the way I architect this kind of things:

  • Lazy-load my controllers
  • Each controller is a singleton within the app and stays around the life of the application (I use IoC containers, but you don't have to)
  • Make your WCF service (or channel) as singleton as well
  • Subscribe to events like these in the constructor of each controller
  • Handle ViewDidLoad and ViewDidUnload appropriately so that most memory can be freed in low memory conditiions
  • Check IsViewLoaded or visibility in the event, etc. to determine if you need to handle the event

You could unsubscribe in Dispose for good measure, but it is not needed.

Upvotes: 1

Related Questions