objective-c consuming rails restful webservices
I’ve been studying ruby and objective-c for a while and found the ObjectiveResource project very interesting. Actually, its a good way to start developing some app for iphone consuming your rails RESTful webservices without worrying (so much) about objects serialization (to and from) or to use JSON/XML at your view controllers.
They have a great screencast for starters and there’s a objectiveresource group that will answer your doubts asap.
Quick start
Assuming you have RESTful webservices for some User domain and have imported the ObjectiveResource API to your xcode project, create an objective-c interface and implementation for User:
User.h
@interface User : NSObject {
NSNumber *userId;
NSString *name;
NSString *email;
}
@property (nonatomic, retain) NSNumber *userId;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@end
User.m
#import "User.h" @implementation User @synthesize userId, name, email; @end
Configure ObjectiveResource at applicationDidFinishLaunching method from AppDelegate using the ObjectiveResourceConfig class method setSite:
[ObjectiveResourceConfig setSite:@"http://localhost:3000"];
The response type can be defined using setResponseType: XmlResponse | JSONResponse.
You can use http basic auth as well:
[ObjectiveResourceConfig setUser:@"brunofuster"]; [ObjectiveResourceConfig setPassword:@"brunofuster"];
Now its easy to get your users:
import "ObjectiveResource.h"
...
-(NSArray) getUsers {
NSArray *users = [User findAllRemote];
return users;
}
This will consume /users.json|xml and serialize the response into an array of fresh new User objects.
There are methods for CRUD operations like saveRemote, destroyRemote, updateRemote and findRemote (based on ActiveResource), although you can create your own calls like “search_users_nearby” (check the getting started tutorial).
Important: ObjectiveResource calls aren’t async. You should use ConnectionManager (branch 1.1 at github). Check a sample at http://gist.github.com/66593
Quite interesting, uh ?
transfer object pattern & annotations (pt_BR)
Como o título já diz, este post demonstra algumas implementações para facilitar a cópia de objetos semelhantes.
Para menor acoplamento entre a camada de visualização (Adobe Flex) e o banco de dados, utilizamos DTOs que são (quase) espelhos de Entidades. Ganhamos flexibilidade para criar objetos mais produtivos para o front-end, o que torna(va) o back-end improdutivo ao tentar espelhar entidades.
A cópia de objetos pode ser feita com apenas uma linha de código (6) e mais algumas anotações.

Opniões e colaboração são bem-vindos.
Spring & JDBC Template
The Spring Framework makes JDBC development easier by using IoC to eliminate low-level code. If you don’t use an ORM framework, this API is highly recommended. Now let’s see a comparison sample between pure JDBC and JDBC with Spring JdbcTemplate class:
Example 1: Creating a list of objects with pure JDBC
public List<User> getUsers() {
PreparedStatement ps = connection.prepareStatement("Select name, email from t_users where status = ?");
ps.setInt(1, new Integer(1));
ResultSet rs = ps.executeQuery();
List users = new ArrayList();
while (rs.next()) {
User user = new User();
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
users.add(user);
}
return users;
}
Besides, you have to control connections opening and closing.
Example 2: Creating a list of objects with Spring and JDBC:
We have to use the JdbcTemplate class that can be instantied by injecting your datasource (new JdbcTemplate(dataSource)). Then you should create a new class that implements the RowMapper interface for the objects creation. After that, you can get the list of objects with just one line of code:
public List<User> getUsers() {
return (List) jdbcTemplate.query("Select name, email from t_users where status = ?", new Object[] { new Integer(1) }, new UserRowMapper());
}
RowMapper sample:
public class UserRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet rs, int numRow) throws SQLException {
User user = new User();
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
return user;
}
}
Complete reference: http://static.springframework.org/spring/docs/2.0.x/reference/jdbc.html
