Posts

Registering Primitives in the Spring Container

I just got back from attending EKON 19 in Köln, Germany. I had a really good time and learned a lot. In fact, I learned the most in one of the sessions I gave – a three hour workshop on Dependency Injection.  Stefan Glienke was there – he maintains and enhances the Spring Framework – and he showed me something that the Spring Container can do that I didn’t know it could do — resolve primitive values by name and an anonymous method.

Here’s how this works.  First, we’ll take a look at a simple class:

This is a simple class — so simple that I won’t bother showing you the implementation, which I know you can figure out.

What is cool, however, is that you can inject both Name and Age properties.  I’ll demo one as a constructor-injected parameter, and the other as a field-injected value.

First, we’ll register the Name property.  We’ll get the name property using the following function that gets the user’s Windows Name:

This is just a wrapper around the Windows API call GetUserName.  The real fun is here:

First, we register the TPerson class so that the container can resolve things for it. Then we register a string with the name ‘name’, and delegate its “construction” to an anonymous function that calls our GetLocalUserName function. The point here is that we now have a “handle” – the string ‘name’ – to a string value that can resolve at runtime. We do the very same thing for the FAge field. That might seem like over-controlling things, but in effect it is quite powerful.  We can use it to ensure that both interface and primitive values in a class are resolved with the Container.  Imagine a constructor that requires not only interface dependencies, but a string value as well.  You can let the container resolve the values for the entire class.

Now, all we need to do is to change the above class to have these attributes:

Now, the Name property of the TPerson class will automatically be filled with the Windows user name without actually writing any more code to make it do that. The FAge field will be set with our favorite number ’42’.

(The code for this application can be found here.)

Now, at this point I bet you are asking “Why would I do that?”

It does, on the surface, seem like a bit of overkill.  But, in the world of Dependency Injection, being able to resolve all the dependencies of a given class – interfaces, classes, and primitives  — is golden.  Not having to write code, and centralizing the resolution of constructor parameters, properties, and fields –no matter what the type — are very useful indeed.  Remember, the more you can decouple your code via the Container, the better.

And you don’t even need but the one call to the ServiceLocator.

Flotsam and Jetsam #110

  • I am doing two Embarcadero Partner Spotlights in the coming weeks.  Both will be 15-20 minute videos about chapters in my book.  The first will be on the Command Pattern, and the second will be on Aspect-oriented Programming.  You can sign up here.  There are some other good partner spotlights coming up as well, including Parnassus (which I talk about below.)  You can also watch all the past Partner Spotlights on YouTube.
  • I’ll be speaking at EKON 19 this year.  Topics include Operator Overloading, Parallel Programming, and a three hour tutorial on Dependency Injection.  I always have a great time at this event, and I always learn a lot.  Marco Cantu and Cary Jensen will be there.  I’d recommend attending – and you get early bird pricing before 01 October.
  • I’ll tell you what, the advertising team at Embarcadero is playing at the top of their game.  I can’t go anywhere on the web without seeing an ad for Delphi 10 Seattle.  Google ads is working quite nicely for them.  If you are a Delphi developer and use Google for searching, you have no excuse not to know DX Seattle exists.
  • I’m a big user and fan of Parnassus Bookmarks, and they have released a version for DX Seattle.  Highly recommended.  And I look forward to seeing what they have in store in the future.  David Millington is a great developer, and I expect some cool stuff in the IDE Plugin department from him.  You should also check out his Navigator tool.
  • The Embarcadero newsgroups are back up and running on new hardware and a new backend.  They’ve been reliable and speedy for about two weeks or so, now, so hopefully they will stay that way.  I think the “catch” is that you have to use SSL to connect.  But in any event, if you are still into using the newsgroups, they seem solidly back up now.

Flotsam and Jetsam # 109