The Bitter Coder Tutorials VIII: Referencing Instances By Key
Previous posts in the series:tutorial #8, Alex continues down his path of switching implementations around, but this time we are going to have multiple components that implement the same contract, and we’ll specify which one we want by id. The id we are talking about is the id we gave the component when we registered it with Windsor. Enough blah, blah, onto the code.
Here’s our class to read a file:Now, we’ll register two components with the container, each one reading a different file. Finally, the program code: Adding the text files and running this bad boy gives the output:
Default contents: This is the contents of file 1 File1 contents: This is the contents of file 1. File2 contents: This is the contents of file 2.
So, as Alex says, the default implementation is the first one in the file, just like using XML.
In his tutorial, Alex gives another example of why you would need to reference implementations by id, so if you’re interested, you can read it there (although, you shoulda already read that post…right?)
Next time, constructor injection…