Cocoa and REST: Should API details go in the model or in an API class?
I have a RESTful server and need to create the client in a Cocoa app.
I have a model called Resource. When I GET /resources, my server returns all resources in JSON.
I have a model called Client that owns many resources.
The Client has an instance method -(NSMutableArray*)resources
An NSArrayController manages the resources. The first time the above method is called, it asks the REST server for the client's resources.
My question is this: who should create the request, dispatch the request, and populate the array: the Client class or the Resource class (with something like -(NSMutableArray*)resourcesForClient:(Client*)client )? Or maybe neither, i开发者_开发知识库nstead there being an API class that receives the model name and some filters and returns the array?
Unless there is a good reason a resource should know about clients, or speak with servers, it is probably a good idea to keep its responsibility minimized. The client has a collection of resources, so it is ok for it to know what a resource is, and manage the requests and management of resources.
I would go with neither if both Client and Resource are part of the business model. Presumably a "Client" in this instance is a customer not a client in technical client-server speak.
Edit:
Your business model should be all about the rules and objects associated with the business. I would class the problem of getting the objects from a particular backing store as not part of the business model in much the same way as I would class getting the objects from a user as not part of the business model.
Therefore your "get from the server" API should be separate.
精彩评论