The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 3,823,987
since: 19 Jan 2005

A NYC .NET Developer in Steve Jobs' Court - Day 2 (Core Data)

posted Mon 05 Feb 07

If you're as new to Core Data as I am, check out this video tutorial, I found it extremely useful.

I have been able to produce the currency converter sample in OS X Tiger and I was fairly pleased. I was surprised by how much of the plumbing took place in the NIB and how little took place in the code. I like that. If you're an old hat at .NET, you'll know that even with the advent of partial classes, you still have code lingering around somewhere that has hundreds of lines that look like this:

myControl.Click += new EventHandler(....blah );

I have no idea which I like better, though I can imagine how the loosely coupled message sending facilities of Objective-C on the Cocoa frameworks allows for some pretty reusable code as well as UI. In other words, I think it would be easier to take a GUI that performed a set of tasks and re-use it in another application because the NIB is not only isolated, but can be plugged into any xcode project. I'm sure all you Apple programmers are thinking, "*yawn*" , but hey - I'm new here.

For my next foray into the unknown (which is a pretty unusual spot for me to be in... I've been programming .NET for so long, I am unaccustomed to feeling lost and overwhelmed by a programming environment) I decided to take a whack at Core Data. Core Data is, I believe, something that was introduced in OS X 10.2 or 10.3. Case in point, the big SAMS book I have on Cocoa written for 10.1 doesn't have a single word referring to Core Data. I'm hoping the one someone from a previous post recommended (en route to me now) will have more information on Core Data.

It's tough to pinpoint a good comparison for Core Data. You could actually consider Core Data to be in the same category as the ADO.NET Entity Framework that is still in CTP status. The ADO.NET EF creates an abstraction layer above the physical data model and allows you to define entities, attributes, and relationships between entities. The EF will happily map entities to underlying SQL Server (potentially other providers will be available in the future). Core Data also allows you to define entities, entity relationships, and attributes on those entities. I think the main difference is that those entities are your data model and Core Data allows you to persist your model as binary, XML, or SQL.

Here's a sample scenario to compare the two:

You have some software like Quicken or MS Money. You have bank accounts, and you have a ledger that contains a list of transactions. Each transaction belongs to a given bank account, but also has been assigned a category. Anybody with any exposure to relational databases is probably already envisioning the data structure for this app.

  • In C#, using the ADO.NET Entity Framework, I would create three entities: Account, LedgerItem, and Category. In the account entity there's an AccountID column that is an autoincrement, indexed, primary key that serves as the source of a relationship to the LedgerItem entity. The relationship would probably be called LedgerItems so that I can refer to child items simply by doing myAccount.LedgerItems once I have called the method that pulls child rows (the EF is a lazy-loader, child rows are only obtained by the parent by explicit method calling or by requiring their presence in an eSQL or LINQ query). Accordingly, there is a AccountID attribute in the LedgerItem entity. Under the hood, there are three different XML files that create the entity mapping and persistence information. There's the schema layer XML file that defines the actual underlying physical storage mechanism (e.g. the SQL tables and their structures). There's the mapping XML file that maps the physical data in the underlying database to the entities, and finally there is the entity XML which defines the entities themselves. The entities defined in the entity layer are then compiled into classes that can be used as the source for LINQ queries as well as eSQL (Entity SQL) queries, or even hit the "old fashioned way" and turned into DataSets, etc. Some of the advantages here are that in a big enterprise environment where you've got a truly remote back-end, this kind of abstraction on top of stored procedures and raw data is extremely useful and makes for some really clean-looking back-end code, which is always good for maintenance and enhancement.
  • When I set this same scenario up in Core Data, I suffered from some serious culture shock. First thing you do is set up the entities and their attributes (e.g. Account, LedgerItem, and Category). Here's where I screwed up big-time: I created individual ID columns just like you would in a relational database. So, I created an accountId, a ledgerItemId, etc. And I kept trying to create relationships based on that column and Interface Builder was having none of that. It rejected me like a bad organ transplant. Eventually I realized that because Core Data does all three: persistence, retrieval, and entity mapping - it doesn't need the artificial ID columns to link related items. It can manage that information internally when related items are added through the relationship itself. So, I ended up defining three entities with only the attributes that applied to them. Then, I created a ledgerItems relationship to get ledger items for a given account, and linked the inverse relationship so that I can use account to get back to the parent account. Also created a category relationship to get the category for a given ledger item. It was all extremely smooth and I found the process of designing my data model directly within my code tool to be extremely enjoyable.

At this point I was thinking I was looking at really two different takes on the same concept - object-relational mapping. The Entity Framework from Microsoft and Core Data from Apple. It also seemed pretty obvious that the Entity Framework is designed specifically for dealing with data contained externally in relational databases while Core Data is designed specifically for storing data on the local disk for desktop applications. I think its hard to say which one is "better" since they both tackle two different problems. However, I did love the fact that I didn't have to set up this completely arbitrary identifier columns that served only to link related entities - Core Data links them for me without creating excess "baggage" on the entity.

The next thing I did was create some GUI that was bound to my objects. I did this both in Leopard and in Tiger. I can't discuss the Leopard experience, but it hasn't changed so radically that my description of the Tiger experience is invalid. Basically I used an NSTableView, created an Array Controller, bound the array controller to the list of Account entities and then bound the table view to the array controller. For each column, I set the value binding to a model expression like accountName for the account name, etc. Within about 5 minutes, I had a grid in my window and was able to click my "Add" and "Remove" buttons to actually create new accounts and manipulate their data. You can do the same thing in C# 2005 with the data navigation control, object data providers, and a couple lines of code rigged up to the navigator's add and remove buttons.

Here's what I liked about the Cocoa experience more than the C# experience:

  • Everything was contained either in the NIB file or in the data model file. It all felt very neat, clean, and separated.
  • Because I had created a 'document' style project, I immediately got save and load to/from disk file functionality for free, in Binary, XML, and SQL format.

So, in short, my experience with Core Data was pleasant. The whole thing just felt very smooth and I got the feeling that it would feel as smooth for an application with 40 entities as it does for an application with 2 entities. You can do pretty much everything in C# 2005 with the EF that you can do with Core Data, but there is really no compelling object-relational system for "desktop applications" (aside from targeting MSDE in your entity mapping schema) that is built-in "out of the box" in .NET, whereas Core Data fills that need but lacks in its ability to communicate with remote relational databases.

Anyway - I am still looking for showstoppers or blockbusters - things that either make me want to run screaming away from Cocoa programming or things that make me want to abandon C# programming. So far I have found neither and will keep looking as I learn more Objective-C and Cocoa.

tags:              

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Rob left...
Mon 05 Feb 07 11:16 am

It's been interesting reading about your OSX dev experiences. Since I know you'v worked with WPF, I would like to see some discussion of how interface builder and databinding compare to the same functionality in WPF, using tools like Expression Blend in combination with VS. I think this is only fair, since WPF is the choice for Windows development moving forward. Also, I would like to mention that Microsoft just released SQL Server Compact Edition, which is an rdbms that is fully embeddable in applications; and its free. This certainly addresses some of the functionality in Core Data, especially when combined with an O/R mapper like NHibernate or the upcoming EF.


2. Chris Hanson left...
Mon 05 Feb 07 11:18 am :: http://chanson.livejournal.com/

Core Data was introduced with Mac OS X Tiger 10.4, not 10.2 or 10.3. However, its "spiritual ancestor" - the Enterprise Objects Framework - has been available on Mac OS X as part of WebObjects for many years, and was originally shipped by NeXT in the early-mid 1990s.

I'm glad you're enjoying working with it!


3. Chad left...
Mon 05 Feb 07 1:03 pm

How would BaseTen compare to ADO.NET EF? http://www.karppinen.fi/baseten/


4. Kevin Hoffman left...
Mon 05 Feb 07 2:17 pm

Well, since I'm a complete newbie to the OS X world as well as to Cocoa development, I had no idea that Baseten existed. If Baseten utilizes the same feel and syntax that Core Data uses, but it allows the persistence to be remote in PostgreSQL databases with Stored procs (does PostgreSQL do procs? I'm not sure...) then I'd say Baseten fills the remote relational database gap left by the predominantly desktop-based Core Data.


5. Kevin Hoffman left...
Mon 05 Feb 07 2:18 pm

Rob,

  • Thanks for the comment. I think I will do an upcoming blog post comparing binding syntax and procedure with WPF/XAML and Core Data. I'd have to do a programmatic binding sample to compare Apples vs Apples (har har) because Core Data binding can be done entirely through the GUI without writing a line of code, whereas all of the WPF binding is done through XAML (or should be, if you're a XAML purist like myself)


6. Miro left...
Mon 05 Feb 07 2:53 pm :: http://thinkwrap.wordpress.com/

Hi Kevin thanks for sharing your experience. I am getting ready to do the same thing - as soon as Leopard is out: get a Mac Pro and give Objective-C serious look. I have similar background C#, ex-Java, ex-C++, ex-C. What you wrote so far sounds very encouraging. Please keep on writing.

Miro


7. Peter Farnell left...
Tue 06 Feb 07 8:29 am

To improve the look & feel of CoreData you may also want to check out mogenerator (short for Managed Object Creator, http://www.rentzsch.com/code/mogenerator_v1.1) . To add some dynamical features see http://www.2pi.dk/tech/cocoa/.

Anyway, nice to see CoreData from a different perspective. An informative read! Thanks!


8. Scott Stevenson left...
Tue 06 Feb 07 10:36 am :: http://theocacao.com/

Jon Rentzsch did a great job on the videos. You might also find these useful: http://www.cocoadevcentral.com/articles/000085.php http://www.cocoadevcentral.com/articles/000086.php

The second one in particular might fill in some of the conceptual gaps.


9. Matt Drance left...
Tue 06 Feb 07 12:36 pm :: http://developer.apple.com

It's been a blast reading all of this Kevin; I'm definitely staying tuned. Glad to see you moving so quickly!


10. Michael Stroeck left...
Thu 08 Feb 07 5:58 am :: http://www.stroeck.com

Don't feel bad about feeling lost in Cocoa, Carbon, etc. They are immensely huge. Just have fun exploring. Be sure to look at Leopard as soon as you can get your hands on. The publicly known trifecta of garbage collection, new syntax and Core Animation alone should be enough to blow your mind if you are into programming desktop apps. But there's also some stuff we NDA-ridden ADC members can't talk about, and it's pretty neat too!


11. Timothy Mowlem left...
Mon 12 Feb 07 7:51 pm

Hi Kevin, I am enjoying your blog describing your forays into the wonders of Cocoa land. I am also fairly new to Cocoa programming. Is it definitely the case that Core Data cannot be linked to a remote SQL database (Oracle, MySQL, Postgres, etc)? ODBC drivers are available for various databases I believe. Looking at the Cocoa Fundamentals guide section on Core Data there is a layer called the 'Persistent Object Store' which seems to be the interface to the datasource itself. I couldn't see a corresponding class in Foundation for such a thing but maybe there is one in a different framework? Can any experienced Cocoa coders comment please?


12. Karl left...
Tue 13 Feb 07 1:52 pm

Have a look at the ODBCKit framework and Andrew Satori's home page, http://www.druware.com/products/odbckit.html and http://web.mac.com/dru_satori/iWeb/Words%20of%20a%20Geek/ODBC/ODBC.html

In addition there is a Mac-specific ODBC faq: http://support.openlinksw.com/support/mac-faq.html


13. Andrew Lindesay left...
Fri 16 Feb 07 4:29 am

I've recently been doing some with using JSON-RPC infrastructure that knows about GID's and EO's to integrate a Cocoa client talking to a WebObjects/EOF application server system. It seems to be working out quite well.


14. Bill left...
Mon 19 Feb 07 7:28 pm

Kevin, Years ago when i was learning Obj-C on the NeXT, I was amazed to see how easily an application I had developed to analyze and plot the data from my research could be reused. Essentially, I had a graphical view of the data curves, interactively selected points for fitting curves to it and stored those points into a text file. It took me several months to learn OOP wiht NeXTStep, but it took about 3 hours to reuse the objects in a very similar but completely different application. That was power.


Tag Related Posts

Upgrading your Leopard install to Java SE 6 64-Bit

Mon 12 Jan 09 1:38 P GMT-05
tags:            

Apple drops the iPhone NDA for Released Software

Wed 01 Oct 08 3:54 P GMT-05
tags:          

Cappuccino, Objective-J, and You

Wed 10 Sep 08 6:14 P GMT-05

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05