Now that my book on Ext JS 4 is over, here’s a list of books that I ordered today. I plan to read them in the next couple of months.
Now that my book on Ext JS 4 is over, here’s a list of books that I ordered today. I plan to read them in the next couple of months.
Developing Ext JS 4 applications using Eclipse is really painful. Period.
OO programmers who write code in Ext JS 4 really miss the intellisense feature. JavaScript being a dynamically typed language there’s little support from the IDE. Visual Studio supports intellisense in Ext JS 4, to an extent. Eclipse offers very little intellisense.
Copy-Paste is a most often used technology in any project. For some reason, when you try to select some lines of Ext JS 4 code to copy and paste it, Eclipse starts huffing and puffing and becomes unresponsive. In fact, I have seen several developers forced to kill eclipse.exe and restart it.
Working with MVC in Ext JS 4 leads to creating number of JS files. The build process of the web application gets too slow. The more number of JS files, the more time it takes for the build an application in Eclipse.
Though there’re tools that you can use to develop Ext JS 4 applications, I personally prefer Visual Studio. A trial version of the express web edition is adequate for this purpose. It’s lightweight and speeds up the development.
Eclipse is a not the right IDE for developing Ext JS 4 applications, but if your server application is a JEE-based one, it will hurt us.
Controllers in Spring MVC 3.x are singleton scoped. There’s only one instance of the Controller shared by all the requests. So it’s bad idea to have member variables in a Controller class.
If you want to inject a bean that’s in session scope to a Controller you have to do some extra work, because the Controller needs to know how to fetch the bean from a session scope. Therefore you need to inject a proxy of the bean into the Controller. The proxy will do the fetching-the-bean-from-session job for the Controller.
The bean in session scope needs to be configured with a proxyMode property of @Scope annotation. The proxyMode attribute can hold a value of a ScopedProxyMode enumeration. You can specify a ScopedProxyMode.INTERFACES or ScopedProxyMode.TARGET_CLASS based on the interface-based or class-based proxy to be generated. If you have a session scoped POJO, Home that needs to be injected to a Controller here’s how you configure the @Scope annotation.
@Component @Scope(value="session",proxyMode=ScopedProxyMode.TARGET_CLASS) public class Home{ } @Controller public class SampleController{ @Autowired private Home home; }
Last week in Norcross, GA we were creating a RESTful application in Spring 3.2. We injected a service to the RESTful controller. The Service talked to the database using a DAO.
One weird thing I noticed when the guys were coding was, after implementing the DAO they ran the code in the browser every time to test the methods in the DAO class. It’s not only frustrating but also time consuming to be testing your DAO operations by bringing up your controller in the browser.
The best way to handle this is throw in an unit test while you code the DAO class. So by the time you’re writing the controller DAO is all set and working fine. Here’s a DAO class called TopicDao and a corresponding TopicDaoTest class. The TopicDao uses a POJO Topic.
//Topic.java public class Topic { private int id; private String title; private int duration; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public Topic(int id, String title, int duration) { this.id = id; this.title = title; this.duration = duration; } public Topic() { } }
//TopicDao.java @Repository public class TopicDao { private JdbcTemplate jdbcTemplate; @Autowired public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public List<Topic> getAllTopics(){ List<Topic> topics = null; topics = jdbcTemplate.query("select * from topics", new RowMapper<Topic>(){ public Topic mapRow(ResultSet rs, int index) throws SQLException { Topic topic = new Topic(rs.getInt("id"),rs.getString("title"),rs.getInt("duration")); return topic; } }); return topics; } public Topic select(int id){ Topic topic = null; Map<String,Object> map = jdbcTemplate.queryForMap("select * from topics where id=?",id); if(map != null && map.size() == 3) topic = new Topic((Integer)map.get("id"),map.get("title").toString(),(Integer)map.get("duration")); return topic; } public int insert(String title,int duration){ return jdbcTemplate.update("insert into topics(title,duration) values(?,?)",title,duration); } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="file:WebContent/WEB-INF/applicationContext.xml") public class TopicDaoTest { private TopicDao topicDao; private ApplicationContext applicationContext; @Before public void tearDown(){ JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate",JdbcTemplate.class); jdbcTemplate.update("delete from topics"); } @Autowired public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @Autowired public void setTopicDao(TopicDao topicDao) { this.topicDao = topicDao; } @Test public void testTopicDaoNotNull() { assertTrue(topicDao != null); } @Test public void testLoadAllTopics() { JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate",JdbcTemplate.class); jdbcTemplate.update("insert into topics(title,duration) values(?,?)","Test1",30); jdbcTemplate.update("insert into topics(title,duration) values(?,?)","Test2",40); List<Topic> topics = topicDao.getAllTopics(); assertNotNull(topics.size() == 2); } @Test public void testLoadTopic() { JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate",JdbcTemplate.class); jdbcTemplate.update("insert into topics(title,duration) values(?,?)","Test1",30); int id = jdbcTemplate.queryForObject("select max(id) from topics",Integer.class); Topic topic = topicDao.select(id); assertNotNull(topic); } @Test public void testInsertTopic() { int num = topicDao.insert("Ruby", 50); assertTrue(num == 1); } }
You can notice the applicationContext.xml file injected using the @ContextConfiguration annotation. You can always create a separate test configuration file and configure a test database to run your tests.
Continuing with the functional style of coding in Scala, let’s implement the guessing game using Lazy Stream collections in Scala. Streams in Scala are lazily evaluated collections. The Stream collection is appended with the values as you go on and on. So it’s an infinite collection.
Here’s the code to implement the Guessing game using Stream.
val target = (Math.random * 100).toInt def playGame(ignoreParam: Boolean = true) = { print("Please enter your guess:") val guess = Console.readInt val compared = guess.compare(target) val (message, playAgain) = compared match { case -1 => ("Aim higher", true) case 0 => (s"You got it ", false) case 1 => ("Aim lower", true) } if(playAgain) println(message) playAgain } val play : Stream[Boolean] = Stream.iterate(true)(playGame) val attempts = play.takeWhile { playAgain => playAgain }.toList.size println(s"You got it in $attempts attempts")