0

I have been endeavoring to wrap my head around building larger apps using the recommended MVC approach that most of the apple docs and various tutorials seem to embrace. That is, nested controllers each holding their own view, and while I 'get it', I don't necessarily like it -- in my particular app, I have a view (and controller) hierarchy that's now several classes deep. For example, a main controller/view -> sidebar -> upper half of sidebar (NSSplitView in an NSView) -> NSTableView.

I've been playing with passing the data/objects I need up and down this hierarchy, but it's getting kind of messy and seems to be leading me down a tight-coupling road I don't like very much, passing lots of things in the init methods for each controller -- for instance a data model.

I've also played with an idea a friend suggested, a singleton service. Essentially, it's a wrapper around whatever data model or 'thing' my controllers might need, in my case an EKEventStore that surfaces an NSArray property and emits notifications (on the main thread). So far this approach seems to be leading me down the road of much cleaner code, and certainly makes unit testing easier (I think), but I'm wondering if I'm way off with my approach.

Has anyone taken this approach before with large apps? Are there any pitfalls or land mines I'm about to stumble on? I realize a lot of this is style/preference, but I'm curious if I'm headed down a bad path.

XeroxDucati
  • 5,130
  • 2
  • 37
  • 66

1 Answers1

0

Is your singleton just holding collections of objects to use or is it also holding mutable global state? For object instantiation and easy testing you should take a look at a DI implementation like Objection. For test mocking check out OCMock.

Using Objection, you can inject a DataModelService into any controller that needs access to the data model for example.

mdatwood
  • 98
  • 1
  • 5
  • I'm not really sure if I'd call it mutable global state or not. I dumped the example I'm playing with here; https://gist.github.com/japhar81/618a732194afa0262f48 I've been looking at Objection, but I keep reading that DI in Objective-C is counter-productive since it's basically designed to not need it.. (ex: http://stackoverflow.com/questions/309711/dependency-injection-framework-for-cocoa) Is that not correct? – XeroxDucati Feb 23 '14 at 03:23
  • Your example is what I have done without a framework like Objection. As far as DI not being needed, I think it is something to be decided on a case by case basis (also DI is different to different people). In your specific example, singletons are easier to manage with DI in my opinion. – mdatwood Feb 23 '14 at 15:36