Reputation: 2572
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
Reputation: 26505
Generally the way I architect this kind of things:
ViewDidLoad
and ViewDidUnload
appropriately so that most memory can be freed in low memory conditiionsIsViewLoaded
or visibility in the event, etc. to determine if you need to handle the eventYou could unsubscribe in Dispose
for good measure, but it is not needed.
Upvotes: 1