简体   繁体   中英

Abstracting NS* objects using Xamarin

My background is C#. I've started to work with Xamarin Studio on OS X to write an iOS app. It's a small app and I want to release for Android once finished with the iOS version.

I'm attempting to use NSUserDefaults to store settings used in the app. My first pass was to use NSDictionary and save the dictionary. Xamarin documentation shows NSDictionary inherits from IDictionary and IDictionary<NSObject, NSObject> .

Is it worth the effort to abstract out NSObject in my repository class? Then I would use native C# types in my business logic but give my repository/model the ability to convert to/from NSObject when loading and saving data.

Back to my first paragraph about IDictionary , should I make the repository convert to/from IDictionary<NSObject, NSObject> but use IDictionary<string, string> throughout the rest of the codebase? I have a feeling I will be fighting an uphill battle if I attempt this strategy.

I'm slightly scatterbrain at this point, so new to the platform I'm not sure what question(s) to ask yet. Perhaps a way to sum up my confusion is "when should I be using native C# types and when should I be using NS* types?"

Are you going to try to reuse as much of the code as possible to port your application to other Xamarin supported platforms?

If so, then take the time to abstract the NS* types out of your code as much as possible.

If not, don't worry about abstracting them away and make things simpler for yourself. Abstraction for no reason adds needless complexity.

Maybe you could think in abstract the NSUserDefaults inside a System.IO.IsolatedStorage.IsolatedStorageSettings , it is not implemented in iOS or Android but by doing it you can port this code to all the Windows platforms too.

You can see the Moonlight implementation here to use it as a template:

https://github.com/mono/moon/blob/master/class/System.Windows/System.IO.IsolatedStorage/IsolatedStorageSettings.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM