Thursday, June 11, 2015

Geecon 2015

So last month I've attended Geecon and I really loved it. Geecon is a conference focused on Java and JVM technologies, hosted annually in Cracow (usually).
Geecon had a Steampunk theme
This year’s it was all about microservices, so it’s no surprise that I enjoyed Fred George’s “Challenges in implementing microservices” the most.

In his talk Fred George shared his experiences with replacing monolith applications with microservices. He explained how microservices facilitate a Service Oriented Architecture and “going faster” as shorter reaction time for the whole company.

What was most interesting to me was the comparison of asynchronous and synchronous approaches to microservices. He mentioned his friend Chad Fowler suggests synchronous communication on default mainly because it’s easier for most of the modern programmers. In contrast Fred George believes that asynchronous messaging gives so too much of an opportunity for robustness and graceful degradation to ignore in exchange for simpler implementation.

Sam Newman on the left, Konrad Malawski on the right and Fred George in the middle
Well, Fred George convinced me to his approach. But it might have something to do with him being a really cool guy and his grey, sage-like, hair - he joked about it a little.

Sam Newman's talk on microservices was also very compelling, even though he talked about his book an awful lot. Must have worked, because I got it for myself after the conference. ;)

If you'd like to know how my how my friends at Espeo Software enjoyed the conference you can do so here!

Sunday, February 1, 2015

Creation method in Mongoose.js, alternative to custom constructor or hooks

Creation method or factory method is a neat way of creating objects in the same way by using the object's well-named static method instead of constructor. While similar, creation method should not to be confused with factory method pattern, which involves the concrete implementation of created objects). If you'd like to know more about creation method's benefits and application I highly recommend Joshua Kerievsky's Refactoring to Patterns.

Here's how this alternative to custom constructor or hooks can work in Mongoose. Let's use an Article model in Mean.js app generated by Yeoman as an example:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
 Schema = mongoose.Schema;

/**
 * Article Schema
 */
var ArticleSchema = new Schema({
 created: {
  type: Date,
  default: Date.now
 },
 title: {
  type: String,
  default: '',
  trim: true,
  required: 'Title cannot be blank'
 },
 url: {
  type: String,
  trim: true,
  default: '',
  required: 'Url cannot be blank'
 },
 content: {
  type: String,
  default: '',
  trim: true
 },
 user: {
  type: Schema.ObjectId,
  ref: 'User'
 }
});

mongoose.model('Article', ArticleSchema);

Let's say we'd like article to have an url field, which we'd like to generate from the title. We'd like to avoid copying the url generation code everytime we'd like to create an article. One way to do this is to create a creation method which does that.

Since all static methods have to be defined before the model class is created creating a creation method the following won't work:

/** Schema definition. */

var Article = mongoose.model('Article', ArticleSchema);

ArticleSchema.schema.createInstance = function (user, title, content) {
 return new Article({
  user: user,
  title: title,
  content: content,
  url: title.replace(new RegExp(' ', 'g'), '-')
 });
};

Instead we can simply fetch the model using mongoose.model. Here's a working example of a creation method:

/** Schema definition. */

ArticleSchema.schema.createInstance = function (user, title, content) {
 var Article = mongoose.model('Article');
 return new Article({
  user: user,
  title: title,
  content: content,
  url: title.replace(new RegExp(' ', 'g'), '-')
 });
};

mongoose.model('Article', ArticleSchema);