Sunday, December 30, 2012

Vaadin, Guice and Shiro

Formalising

Once I made some progress integrating Apache Shiro, I realised that what started as an experimental integration of Vaadin and Guice is becoming a rather more substantial project - something for me to use as a base for future applications, and maybe something others could use too.

So I have started formalising things a bit more.  I've started some documentation, partly to clarify my thoughts, but also to share with anyone who is interested.

The code is still in the same place, but I will start using a proper branching model so that the master branch becomes more stable  - until now I haven't been concerned about that.

Shiro

The integration is extremely rough at the moment, but it does demonstrate a login (hard-coded for now).   I think I will go for the URL based model provided by Shiro, provided I can get that to work with Vaadin.

Refactoring

I've done quite a bit of moving code around to try and separate what should be the base V7 reference, and the demo code

Friday, December 21, 2012

Vaadin 7 beta 11

Changes from beta 10

I noticed a couple of small changes from beta 10:

VaadinRequest parameters

The parameter names have changed.  I was using "loc" to return the location from Page, using a mocked VaadinRequest.  The parameter name has changed to "v-loc".  I notice the width and height parameters are now pre-fixed with "v-" as well.

Chameleon Button style

I was using the "big" style for buttons, but this no longer works, and neither does "small".   I've changed to using "tall" for now, and posted a question on the Vaadin forum

Tuesday, December 18, 2012

A working model

Well, I do have a working model now, and the code does most of what I wanted to do.  The disappointment was the amount of classes I had to override in order to get this working - caused mainly by the native Vaadin Navigator being a concrete class.

I've suggested to the Vaadin team that Navigator should be an interface to make things more flexible, and hopefully that will be taken up.

I also noted the recent post about CDI on the Vaadin forum - I probably should make sure that my effort aligns to CDI even if I don't use CDI directly.

Monday, December 10, 2012

Vaadin 7 Navigator with Guice

Following my earlier work, I started out thinking that I only had to make a few changes around the   Vaadin 7 Navigator to make it Guice friendly.

It seems that is not the case!

The standard Vaadin 7 Navigator class is very closely coupled to a number of other classes, and I have ended up developing a parallel navigation scheme.  It was not what I intended, but it will allow me to make more use of Guice functionality - which in most ways Vaadin is very well suited to, as it is a component based framework.

So - work is in progress.

Monday, December 3, 2012

Vaadin 7 navigation and parameters

The good bit

URI parameter handling

Adding handling for URI parameters to the sample app was straightforward, the new Vaadin 7 architecture makes it easy. 

Error View

Putting in an error view, to deal with invalid URIs was equally simple.

Appearances

I have tidied up the appearance of the app - it won't win any design prizes but it does look a bit better.  More importantly it demonstrates more clearly some of the basics I was trying to establish.


The Not So Good Bit

Concrete classes

The use of some concrete classes (as opposed to interfaces) in the navigation scheme is getting in the way of some options I would like to use with Guice.  The Navigator itself is a concrete class, with constructor parameters, and although it is possible to get round this it would have been much easier if there had been a Navigator interface, and say, a DefaultNavigator implementation.   

Sunday, November 25, 2012

Vaadin 7 and Guice custom scope

In a previous project using Vaadin 6 and Navigator7, we found it helpful to have a Guice scope which matched a "browser tab".  This was for things like message bars, toolbars and breadcrumbs and made good use of the dependency injection paradigm.

In Vaadin 7, the nearest scope to this would be UI - so I  have added a UIScope to the sample app.  It seems to work, but still needs proper testing, so it resides in a separate branch for now.

One of the problems with this is that when a new UI is created it will almost certainly require injection of UIScope components - but the UI itself has not yet been constructed so cannot be used as a scope reference.

To get round this I use a surrogate key (an instance of UIKey), and hold it in CurrentInstance, a ThreadLocal store provided by Vaadin 7.

Thursday, November 22, 2012

Vaadin 7 Navigation and Parameters

Parameter handling

The code in the sample app is still  ugly in places, but is starting to take shape.  

URI Decoder

I have used a more strict interpretation of the UriFragment than Vaadin does by default, and made it a requirement that the URI structure is of the form:

http://example.com/domain#!finance/report/risk/id=1223/year=2012

where:
 
finance/report/risk/

is a "virtual page path" and is represented by a View implementation, and everything after it is paired parameters.

This is not as flexible as the standard Vaadin approach - but I just prefer it.  It could easily be changed by using a different implementation of URIDecoder.

Site Map 

The approach I have taken with the Guice based View provider means that I can put all the mapping of View name to View class in one place - in effect, a site map.  At the moment it is part of the provider, but needs to be separated out.

Monday, November 19, 2012

Vaadin 7 Navigation with Guice

Vaadin 7 Navigation


The new Vaadin 7 architecture has really made "page" control so much easier.  Of course, being an AJAX environment there is really only one page, but the user doesn't know that.

The switching of views to make it appear to the user that they are using multiple pages is really quite neat, and the sample app has been updated to use it with Guice.

No back button problems, no nasty "out of sync" red boxes.  I like it.

Testing Pages

I have also added some JUnit testing of Vaadin "pages" - it won't take you as as far as Vaadin TestBench of course, but it will allow some quite reasonable testing within JUnit before you start on the UI driven tests.

Sunday, November 18, 2012

Guice Injection

Guice Injection - Field or Constructor?


I was just starting to look at the the task of creating views in Vaadin 7, using Guice.

This has been made a lot easier by the new Vaadin architecture, but it prompted me to look again at  some Guice best practices, and specifically which type of injection to use.

The clarity and simplicity of field injection is always so appealing ...

Which Injection?

Generally the choice is between constructor and field injection (method injection is usually only required under specific circumstances, although some espouse the virtue of setter injection).  

Field injection

is very easy to write, and the intent of the code very clear - but does not result in immutability.  Note this comment from the Guice team:

"Never inject final fields; the JVM doesn't guarantee that the injected value will be visible to all threads." 

Constructor injection will always happen before field inject, which would mean constructor code would not have access to field values if field injection was used.


The only point I would disagree with is the suggestion that field injection is not testable.  Provided you use Guice in your test harness, as the sample app does using Mycila, testing injected fields is entirely possible.

Constructor injection 

provides immutability, but the code is a lot less clear when there are multiple injections, especially when injecting providers.  As the Guice team describe it, the code is cumbersome.  A change to a sub-class constructor with a lot of injections is a real pain and can lead to mistakes.


Originally I used constructor injection for immutability, and experienced some lengthy and cumbersome code as a consequence.  The clarity of field injection is so tempting (which is why it tends to be used for code examples) ... that I decided to re-assess my thinking.

So which to go for?  

Ultimately it is up to you of course, and there are use cases for all of them.

After reading quite a few posts, with many opinions, I concluded that constructor injection is generally the best.  The main reasons:
  • immutability.  As something which is generally considered good practice, this is fairly strong argument in its own right ... final fields cannot be injected reliably as mentioned above, although Guice will not raise any errors.
  • consistency.  Although not essential, it may be helpful to default to one technique.  You can usually use constructor injection where field injection is used.  (There are exceptions, for example in servlets which are required to have a parameterless constructor). The reverse is less often true, if, for example, the constructor code needs to access the field values.
  • clarity.  If a constructor gets too long, I should probably refactor the class anyway, and avoiding  deep class hierarchies has other benefits.
  • risk.  Not a huge risk admittedly,  but it would be easy to forget why an injected field is not marked private, and automatically generate a setter for it.  Of course then the setter could be called elsewhere and displace the injected value.  That's fine if it is intentional, but it would be fairly easy to do accidentally.
     
 ... but the decision is not a religious one, so if I come across a scenario where field injection (or method injection) would do a better job, then I'd use it.  The default for me, though, is constructor injection, so I will update the sample app to reflect that.

But

It would be really good if someone could come up with a DI solution which combines the best of constructor and field injection ... :-)

Thursday, November 15, 2012

Vaadin 7 Beta 9 and Guice

Updated to Beta 9


I have updated the sample app to use beta 9, and nothing broke.

Next task is to look at the new "page" structure with Views, and use Guice for injecting those too.

Vaadin 7 with Guice

Vaadin 7 with Guice

Vaadin 6 is an excellent UI development framework.  Vaadin 7 looks like being even better.

I am most looking forward most to the new navigation features - the logic seems much clearer, and appears to support the idea of multiple Ajax pages (from the user's point of view that is).  No more out of sync errors in multiple tabs!

It is a shame in some ways that dependency injection is not included as standard, but I can understand why - it would mean backing a particular DI  provider and perhaps shutting out a lot of developers. 

We made extensive use of Guice in an earlier project on Vaadin 6, and I became a real fan of the combination. Then I found this blog, which gave me an excellent start (thanks Christian) for integrating Vaadin 7 and Guice.  

I have slightly modified the example he gives.  I have taken out the GAE element, which I don't use, and added in JUnit testing using Guice.  My sample app doesn't do much, but if you open it in multiple tabs it demonstrates the use of Guice and multiple UI instances. 

I've also included Guice in the Ivy dependency management, another new feature of Vaadin 7.  I had intended to use Gradle as a much simpler alternative to Maven, but haven't got round to doing that - and I may just leave it with Ivy.

The sample code is currently written against Vaadin 7 beta 8, and I will try and update it as the Vaadin releases progress.